blob: 91da0d564000b75e04cac33420b5a2c708198291 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
Prabir Pradhanc14266f2021-05-12 15:56:24 -070019#include <InputFlingerProperties.sysprop.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <InputMapper.h>
21#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080022#include <InputReaderBase.h>
23#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070024#include <KeyboardInputMapper.h>
25#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070026#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070027#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <SingleTouchInputMapper.h>
29#include <SwitchInputMapper.h>
30#include <TestInputListener.h>
31#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080032#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000033#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070034#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050036#include <gui/constants.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080037#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <math.h>
39
Michael Wright17db18e2020-06-26 20:51:44 +010040#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080041#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000042#include "input/DisplayViewport.h"
43#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045namespace android {
46
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070047using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070048using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070049
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Michael Wrightd02c5b62014-02-10 15:10:22 -080082// Error tolerance for floating point assertions.
83static const float EPSILON = 0.001f;
84
85template<typename T>
86static inline T min(T a, T b) {
87 return a < b ? a : b;
88}
89
90static inline float avg(float x, float y) {
91 return (x + y) / 2;
92}
93
Chris Ye3fdbfef2021-01-06 18:45:18 -080094// Mapping for light color name and the light color
95const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
96 {"green", LightColor::GREEN},
97 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
Prabir Pradhanc14266f2021-05-12 15:56:24 -070099static int32_t getInverseRotation(int32_t orientation) {
100 switch (orientation) {
101 case DISPLAY_ORIENTATION_90:
102 return DISPLAY_ORIENTATION_270;
103 case DISPLAY_ORIENTATION_270:
104 return DISPLAY_ORIENTATION_90;
105 default:
106 return orientation;
107 }
108}
109
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110// --- FakePointerController ---
111
112class FakePointerController : public PointerControllerInterface {
113 bool mHaveBounds;
114 float mMinX, mMinY, mMaxX, mMaxY;
115 float mX, mY;
116 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800117 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119public:
120 FakePointerController() :
121 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800122 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 }
124
Michael Wright17db18e2020-06-26 20:51:44 +0100125 virtual ~FakePointerController() {}
126
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127 void setBounds(float minX, float minY, float maxX, float maxY) {
128 mHaveBounds = true;
129 mMinX = minX;
130 mMinY = minY;
131 mMaxX = maxX;
132 mMaxY = maxY;
133 }
134
Chris Yea52ade12020-08-27 16:49:20 -0700135 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136 mX = x;
137 mY = y;
138 }
139
Chris Yea52ade12020-08-27 16:49:20 -0700140 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141
Chris Yea52ade12020-08-27 16:49:20 -0700142 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143
Chris Yea52ade12020-08-27 16:49:20 -0700144 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 *outX = mX;
146 *outY = mY;
147 }
148
Chris Yea52ade12020-08-27 16:49:20 -0700149 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800150
Chris Yea52ade12020-08-27 16:49:20 -0700151 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800152 mDisplayId = viewport.displayId;
153 }
154
Arthur Hung7c645402019-01-25 17:45:42 +0800155 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
156 return mSpotsByDisplay;
157 }
158
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159private:
Chris Yea52ade12020-08-27 16:49:20 -0700160 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 *outMinX = mMinX;
162 *outMinY = mMinY;
163 *outMaxX = mMaxX;
164 *outMaxY = mMaxY;
165 return mHaveBounds;
166 }
167
Chris Yea52ade12020-08-27 16:49:20 -0700168 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169 mX += deltaX;
170 if (mX < mMinX) mX = mMinX;
171 if (mX > mMaxX) mX = mMaxX;
172 mY += deltaY;
173 if (mY < mMinY) mY = mMinY;
174 if (mY > mMaxY) mY = mMaxY;
175 }
176
Chris Yea52ade12020-08-27 16:49:20 -0700177 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178
Chris Yea52ade12020-08-27 16:49:20 -0700179 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180
Chris Yea52ade12020-08-27 16:49:20 -0700181 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182
Chris Yea52ade12020-08-27 16:49:20 -0700183 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
184 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800185 std::vector<int32_t> newSpots;
186 // Add spots for fingers that are down.
187 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
188 uint32_t id = idBits.clearFirstMarkedBit();
189 newSpots.push_back(id);
190 }
191
192 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800196
197 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198};
199
200
201// --- FakeInputReaderPolicy ---
202
203class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700204 std::mutex mLock;
205 std::condition_variable mDevicesChangedCondition;
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100208 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
210 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100211 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700212 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213
214protected:
Chris Yea52ade12020-08-27 16:49:20 -0700215 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
217public:
218 FakeInputReaderPolicy() {
219 }
220
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800222 waitForInputDevices([](bool devicesChanged) {
223 if (!devicesChanged) {
224 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
225 }
226 });
227 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700228
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800229 void assertInputDevicesNotChanged() {
230 waitForInputDevices([](bool devicesChanged) {
231 if (devicesChanged) {
232 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
233 }
234 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700235 }
236
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700237 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100239 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700240 }
241
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700242 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
243 return mConfig.getDisplayViewportByUniqueId(uniqueId);
244 }
245 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
246 return mConfig.getDisplayViewportByType(type);
247 }
248
249 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
250 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700251 }
252
253 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000254 bool isActive, const std::string& uniqueId,
255 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
256 const DisplayViewport viewport =
257 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
258 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700259 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100260 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 }
262
Arthur Hung6cd19a42019-08-30 19:04:12 +0800263 bool updateViewport(const DisplayViewport& viewport) {
264 size_t count = mViewports.size();
265 for (size_t i = 0; i < count; i++) {
266 const DisplayViewport& currentViewport = mViewports[i];
267 if (currentViewport.displayId == viewport.displayId) {
268 mViewports[i] = viewport;
269 mConfig.setDisplayViewports(mViewports);
270 return true;
271 }
272 }
273 // no viewport found.
274 return false;
275 }
276
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100277 void addExcludedDeviceName(const std::string& deviceName) {
278 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 }
280
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700281 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
282 mConfig.portAssociations.insert({inputPort, displayPort});
283 }
284
Christine Franks1ba71cc2021-04-07 14:37:42 -0700285 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
286 const std::string& displayUniqueId) {
287 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
288 }
289
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000290 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700291
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000292 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700293
Michael Wright17db18e2020-06-26 20:51:44 +0100294 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
295 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 }
297
298 const InputReaderConfiguration* getReaderConfiguration() const {
299 return &mConfig;
300 }
301
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800302 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 return mInputDevices;
304 }
305
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100306 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700307 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700308 return transform;
309 }
310
311 void setTouchAffineTransformation(const TouchAffineTransformation t) {
312 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800313 }
314
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800315 void setPointerCapture(bool enabled) {
316 mConfig.pointerCapture = enabled;
317 }
318
Arthur Hung7c645402019-01-25 17:45:42 +0800319 void setShowTouches(bool enabled) {
320 mConfig.showTouches = enabled;
321 }
322
Garfield Tan888a6a42020-01-09 11:39:16 -0800323 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
324 mConfig.defaultPointerDisplayId = pointerDisplayId;
325 }
326
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800327 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
328
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700330 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000331 int32_t orientation, bool isActive,
332 const std::string& uniqueId,
333 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700334 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
335 || orientation == DISPLAY_ORIENTATION_270);
336 DisplayViewport v;
337 v.displayId = displayId;
338 v.orientation = orientation;
339 v.logicalLeft = 0;
340 v.logicalTop = 0;
341 v.logicalRight = isRotated ? height : width;
342 v.logicalBottom = isRotated ? width : height;
343 v.physicalLeft = 0;
344 v.physicalTop = 0;
345 v.physicalRight = isRotated ? height : width;
346 v.physicalBottom = isRotated ? width : height;
347 v.deviceWidth = isRotated ? height : width;
348 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000349 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700350 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700351 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100352 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700353 return v;
354 }
355
Chris Yea52ade12020-08-27 16:49:20 -0700356 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357 *outConfig = mConfig;
358 }
359
Chris Yea52ade12020-08-27 16:49:20 -0700360 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100361 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800362 }
363
Chris Yea52ade12020-08-27 16:49:20 -0700364 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700365 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700367 mInputDevicesChanged = true;
368 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369 }
370
Chris Yea52ade12020-08-27 16:49:20 -0700371 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
372 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700373 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374 }
375
Chris Yea52ade12020-08-27 16:49:20 -0700376 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800377
378 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
379 std::unique_lock<std::mutex> lock(mLock);
380 base::ScopedLockAssertion assumeLocked(mLock);
381
382 const bool devicesChanged =
383 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
384 return mInputDevicesChanged;
385 });
386 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
387 mInputDevicesChanged = false;
388 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389};
390
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391// --- FakeEventHub ---
392
393class FakeEventHub : public EventHubInterface {
394 struct KeyInfo {
395 int32_t keyCode;
396 uint32_t flags;
397 };
398
Chris Yef59a2f42020-10-16 12:55:26 -0700399 struct SensorInfo {
400 InputDeviceSensorType sensorType;
401 int32_t sensorDataIndex;
402 };
403
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404 struct Device {
405 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700406 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 PropertyMap configuration;
408 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
409 KeyedVector<int, bool> relativeAxes;
410 KeyedVector<int32_t, int32_t> keyCodeStates;
411 KeyedVector<int32_t, int32_t> scanCodeStates;
412 KeyedVector<int32_t, int32_t> switchStates;
413 KeyedVector<int32_t, int32_t> absoluteAxisValue;
414 KeyedVector<int32_t, KeyInfo> keysByScanCode;
415 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
416 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700417 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
418 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800419 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700420 bool enabled;
421
422 status_t enable() {
423 enabled = true;
424 return OK;
425 }
426
427 status_t disable() {
428 enabled = false;
429 return OK;
430 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431
Chris Ye1b0c7342020-07-28 21:57:03 -0700432 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433 };
434
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700435 std::mutex mLock;
436 std::condition_variable mEventsCondition;
437
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100439 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000440 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600441 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000442 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800443 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
444 // Simulates a device light brightness, from light id to light brightness.
445 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
446 // Simulates a device light intensities, from light id to light intensities map.
447 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
448 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700450public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451 virtual ~FakeEventHub() {
452 for (size_t i = 0; i < mDevices.size(); i++) {
453 delete mDevices.valueAt(i);
454 }
455 }
456
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457 FakeEventHub() { }
458
Chris Ye1b0c7342020-07-28 21:57:03 -0700459 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 Device* device = new Device(classes);
461 device->identifier.name = name;
462 mDevices.add(deviceId, device);
463
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000464 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465 }
466
467 void removeDevice(int32_t deviceId) {
468 delete mDevices.valueFor(deviceId);
469 mDevices.removeItem(deviceId);
470
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000471 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800472 }
473
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700474 bool isDeviceEnabled(int32_t deviceId) {
475 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700476 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700477 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
478 return false;
479 }
480 return device->enabled;
481 }
482
483 status_t enableDevice(int32_t deviceId) {
484 status_t result;
485 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700486 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700487 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
488 return BAD_VALUE;
489 }
490 if (device->enabled) {
491 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
492 return OK;
493 }
494 result = device->enable();
495 return result;
496 }
497
498 status_t disableDevice(int32_t deviceId) {
499 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700500 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700501 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
502 return BAD_VALUE;
503 }
504 if (!device->enabled) {
505 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
506 return OK;
507 }
508 return device->disable();
509 }
510
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000512 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513 }
514
515 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
516 Device* device = getDevice(deviceId);
517 device->configuration.addProperty(key, value);
518 }
519
520 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
521 Device* device = getDevice(deviceId);
522 device->configuration.addAll(configuration);
523 }
524
525 void addAbsoluteAxis(int32_t deviceId, int axis,
526 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
527 Device* device = getDevice(deviceId);
528
529 RawAbsoluteAxisInfo info;
530 info.valid = true;
531 info.minValue = minValue;
532 info.maxValue = maxValue;
533 info.flat = flat;
534 info.fuzz = fuzz;
535 info.resolution = resolution;
536 device->absoluteAxes.add(axis, info);
537 }
538
539 void addRelativeAxis(int32_t deviceId, int32_t axis) {
540 Device* device = getDevice(deviceId);
541 device->relativeAxes.add(axis, true);
542 }
543
544 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
545 Device* device = getDevice(deviceId);
546 device->keyCodeStates.replaceValueFor(keyCode, state);
547 }
548
549 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
550 Device* device = getDevice(deviceId);
551 device->scanCodeStates.replaceValueFor(scanCode, state);
552 }
553
554 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
555 Device* device = getDevice(deviceId);
556 device->switchStates.replaceValueFor(switchCode, state);
557 }
558
559 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
560 Device* device = getDevice(deviceId);
561 device->absoluteAxisValue.replaceValueFor(axis, value);
562 }
563
564 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
565 int32_t keyCode, uint32_t flags) {
566 Device* device = getDevice(deviceId);
567 KeyInfo info;
568 info.keyCode = keyCode;
569 info.flags = flags;
570 if (scanCode) {
571 device->keysByScanCode.add(scanCode, info);
572 }
573 if (usageCode) {
574 device->keysByUsageCode.add(usageCode, info);
575 }
576 }
577
578 void addLed(int32_t deviceId, int32_t led, bool initialState) {
579 Device* device = getDevice(deviceId);
580 device->leds.add(led, initialState);
581 }
582
Chris Yef59a2f42020-10-16 12:55:26 -0700583 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
584 int32_t sensorDataIndex) {
585 Device* device = getDevice(deviceId);
586 SensorInfo info;
587 info.sensorType = sensorType;
588 info.sensorDataIndex = sensorDataIndex;
589 device->sensorsByAbsCode.emplace(absCode, info);
590 }
591
592 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
593 Device* device = getDevice(deviceId);
594 typename BitArray<MSC_MAX>::Buffer buffer;
595 buffer[mscEvent / 32] = 1 << mscEvent % 32;
596 device->mscBitmask.loadFromBuffer(buffer);
597 }
598
Chris Ye3fdbfef2021-01-06 18:45:18 -0800599 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
600 mRawLightInfos.emplace(rawId, std::move(info));
601 }
602
603 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
604 mLightBrightness.emplace(rawId, brightness);
605 }
606
607 void fakeLightIntensities(int32_t rawId,
608 const std::unordered_map<LightColor, int32_t> intensities) {
609 mLightIntensities.emplace(rawId, std::move(intensities));
610 }
611
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 bool getLedState(int32_t deviceId, int32_t led) {
613 Device* device = getDevice(deviceId);
614 return device->leds.valueFor(led);
615 }
616
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100617 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 return mExcludedDevices;
619 }
620
621 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
622 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800623 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800624 }
625
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000626 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
627 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700628 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 RawEvent event;
630 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000631 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 event.deviceId = deviceId;
633 event.type = type;
634 event.code = code;
635 event.value = value;
636 mEvents.push_back(event);
637
638 if (type == EV_ABS) {
639 setAbsoluteAxisValue(deviceId, code, value);
640 }
641 }
642
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600643 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
644 std::vector<TouchVideoFrame>> videoFrames) {
645 mVideoFrames = std::move(videoFrames);
646 }
647
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700649 std::unique_lock<std::mutex> lock(mLock);
650 base::ScopedLockAssertion assumeLocked(mLock);
651 const bool queueIsEmpty =
652 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
653 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
654 if (!queueIsEmpty) {
655 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
656 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 }
658
659private:
660 Device* getDevice(int32_t deviceId) const {
661 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100662 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 }
664
Chris Yea52ade12020-08-27 16:49:20 -0700665 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700667 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 }
669
Chris Yea52ade12020-08-27 16:49:20 -0700670 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 Device* device = getDevice(deviceId);
672 return device ? device->identifier : InputDeviceIdentifier();
673 }
674
Chris Yea52ade12020-08-27 16:49:20 -0700675 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676
Chris Yea52ade12020-08-27 16:49:20 -0700677 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678 Device* device = getDevice(deviceId);
679 if (device) {
680 *outConfiguration = device->configuration;
681 }
682 }
683
Chris Yea52ade12020-08-27 16:49:20 -0700684 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
685 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800687 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 ssize_t index = device->absoluteAxes.indexOfKey(axis);
689 if (index >= 0) {
690 *outAxisInfo = device->absoluteAxes.valueAt(index);
691 return OK;
692 }
693 }
694 outAxisInfo->clear();
695 return -1;
696 }
697
Chris Yea52ade12020-08-27 16:49:20 -0700698 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699 Device* device = getDevice(deviceId);
700 if (device) {
701 return device->relativeAxes.indexOfKey(axis) >= 0;
702 }
703 return false;
704 }
705
Chris Yea52ade12020-08-27 16:49:20 -0700706 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707
Chris Yef59a2f42020-10-16 12:55:26 -0700708 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
709 Device* device = getDevice(deviceId);
710 if (device) {
711 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
712 }
713 return false;
714 }
715
Chris Yea52ade12020-08-27 16:49:20 -0700716 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
717 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 Device* device = getDevice(deviceId);
719 if (device) {
720 const KeyInfo* key = getKey(device, scanCode, usageCode);
721 if (key) {
722 if (outKeycode) {
723 *outKeycode = key->keyCode;
724 }
725 if (outFlags) {
726 *outFlags = key->flags;
727 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700728 if (outMetaState) {
729 *outMetaState = metaState;
730 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 return OK;
732 }
733 }
734 return NAME_NOT_FOUND;
735 }
736
737 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
738 if (usageCode) {
739 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
740 if (index >= 0) {
741 return &device->keysByUsageCode.valueAt(index);
742 }
743 }
744 if (scanCode) {
745 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
746 if (index >= 0) {
747 return &device->keysByScanCode.valueAt(index);
748 }
749 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700750 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751 }
752
Chris Yea52ade12020-08-27 16:49:20 -0700753 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754
Chris Yef59a2f42020-10-16 12:55:26 -0700755 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
756 int32_t absCode) {
757 Device* device = getDevice(deviceId);
758 if (!device) {
759 return Errorf("Sensor device not found.");
760 }
761 auto it = device->sensorsByAbsCode.find(absCode);
762 if (it == device->sensorsByAbsCode.end()) {
763 return Errorf("Sensor map not found.");
764 }
765 const SensorInfo& info = it->second;
766 return std::make_pair(info.sensorType, info.sensorDataIndex);
767 }
768
Chris Yea52ade12020-08-27 16:49:20 -0700769 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 mExcludedDevices = devices;
771 }
772
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000773 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
774 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800775
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000776 const size_t filledSize = std::min(mEvents.size(), bufferSize);
777 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
778
779 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700780 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000781 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800782 }
783
Chris Yea52ade12020-08-27 16:49:20 -0700784 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600785 auto it = mVideoFrames.find(deviceId);
786 if (it != mVideoFrames.end()) {
787 std::vector<TouchVideoFrame> frames = std::move(it->second);
788 mVideoFrames.erase(deviceId);
789 return frames;
790 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800791 return {};
792 }
793
Chris Yea52ade12020-08-27 16:49:20 -0700794 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795 Device* device = getDevice(deviceId);
796 if (device) {
797 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
798 if (index >= 0) {
799 return device->scanCodeStates.valueAt(index);
800 }
801 }
802 return AKEY_STATE_UNKNOWN;
803 }
804
Chris Yea52ade12020-08-27 16:49:20 -0700805 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806 Device* device = getDevice(deviceId);
807 if (device) {
808 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
809 if (index >= 0) {
810 return device->keyCodeStates.valueAt(index);
811 }
812 }
813 return AKEY_STATE_UNKNOWN;
814 }
815
Chris Yea52ade12020-08-27 16:49:20 -0700816 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 Device* device = getDevice(deviceId);
818 if (device) {
819 ssize_t index = device->switchStates.indexOfKey(sw);
820 if (index >= 0) {
821 return device->switchStates.valueAt(index);
822 }
823 }
824 return AKEY_STATE_UNKNOWN;
825 }
826
Chris Yea52ade12020-08-27 16:49:20 -0700827 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
828 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 Device* device = getDevice(deviceId);
830 if (device) {
831 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
832 if (index >= 0) {
833 *outValue = device->absoluteAxisValue.valueAt(index);
834 return OK;
835 }
836 }
837 *outValue = 0;
838 return -1;
839 }
840
Chris Yea52ade12020-08-27 16:49:20 -0700841 // Return true if the device has non-empty key layout.
842 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
843 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 bool result = false;
845 Device* device = getDevice(deviceId);
846 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700847 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848 for (size_t i = 0; i < numCodes; i++) {
849 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
850 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
851 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852 }
853 }
854 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
855 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
856 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857 }
858 }
859 }
860 }
861 return result;
862 }
863
Chris Yea52ade12020-08-27 16:49:20 -0700864 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865 Device* device = getDevice(deviceId);
866 if (device) {
867 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
868 return index >= 0;
869 }
870 return false;
871 }
872
Chris Yea52ade12020-08-27 16:49:20 -0700873 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874 Device* device = getDevice(deviceId);
875 return device && device->leds.indexOfKey(led) >= 0;
876 }
877
Chris Yea52ade12020-08-27 16:49:20 -0700878 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879 Device* device = getDevice(deviceId);
880 if (device) {
881 ssize_t index = device->leds.indexOfKey(led);
882 if (index >= 0) {
883 device->leds.replaceValueAt(led, on);
884 } else {
885 ADD_FAILURE()
886 << "Attempted to set the state of an LED that the EventHub declared "
887 "was not present. led=" << led;
888 }
889 }
890 }
891
Chris Yea52ade12020-08-27 16:49:20 -0700892 void getVirtualKeyDefinitions(
893 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894 outVirtualKeys.clear();
895
896 Device* device = getDevice(deviceId);
897 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800898 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899 }
900 }
901
Chris Yea52ade12020-08-27 16:49:20 -0700902 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700903 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 }
905
Chris Yea52ade12020-08-27 16:49:20 -0700906 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907 return false;
908 }
909
Chris Yea52ade12020-08-27 16:49:20 -0700910 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911
Chris Yea52ade12020-08-27 16:49:20 -0700912 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913
Chris Ye87143712020-11-10 05:05:58 +0000914 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
915
Chris Yee2b1e5c2021-03-10 22:45:12 -0800916 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
917 return BATTERY_CAPACITY;
918 }
Kim Low03ea0352020-11-06 12:45:07 -0800919
Chris Yee2b1e5c2021-03-10 22:45:12 -0800920 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
921 return BATTERY_STATUS;
922 }
923
924 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
925
926 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
927 return std::nullopt;
928 }
Kim Low03ea0352020-11-06 12:45:07 -0800929
Chris Ye3fdbfef2021-01-06 18:45:18 -0800930 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
931 std::vector<int32_t> ids;
932 for (const auto& [rawId, info] : mRawLightInfos) {
933 ids.push_back(rawId);
934 }
935 return ids;
936 }
937
938 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
939 auto it = mRawLightInfos.find(lightId);
940 if (it == mRawLightInfos.end()) {
941 return std::nullopt;
942 }
943 return it->second;
944 }
945
946 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
947 mLightBrightness.emplace(lightId, brightness);
948 }
949
950 void setLightIntensities(int32_t deviceId, int32_t lightId,
951 std::unordered_map<LightColor, int32_t> intensities) override {
952 mLightIntensities.emplace(lightId, intensities);
953 };
954
955 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
956 auto lightIt = mLightBrightness.find(lightId);
957 if (lightIt == mLightBrightness.end()) {
958 return std::nullopt;
959 }
960 return lightIt->second;
961 }
962
963 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
964 int32_t deviceId, int32_t lightId) override {
965 auto lightIt = mLightIntensities.find(lightId);
966 if (lightIt == mLightIntensities.end()) {
967 return std::nullopt;
968 }
969 return lightIt->second;
970 };
971
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100972 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973 return false;
974 }
975
Chris Yea52ade12020-08-27 16:49:20 -0700976 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977
Chris Yea52ade12020-08-27 16:49:20 -0700978 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979
Chris Yea52ade12020-08-27 16:49:20 -0700980 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Chris Yea52ade12020-08-27 16:49:20 -0700982 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983};
984
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985// --- FakeInputMapper ---
986
987class FakeInputMapper : public InputMapper {
988 uint32_t mSources;
989 int32_t mKeyboardType;
990 int32_t mMetaState;
991 KeyedVector<int32_t, int32_t> mKeyCodeStates;
992 KeyedVector<int32_t, int32_t> mScanCodeStates;
993 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800994 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 std::mutex mLock;
997 std::condition_variable mStateChangedCondition;
998 bool mConfigureWasCalled GUARDED_BY(mLock);
999 bool mResetWasCalled GUARDED_BY(mLock);
1000 bool mProcessWasCalled GUARDED_BY(mLock);
1001 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002
Arthur Hungc23540e2018-11-29 20:42:11 +08001003 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001005 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1006 : InputMapper(deviceContext),
1007 mSources(sources),
1008 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001010 mConfigureWasCalled(false),
1011 mResetWasCalled(false),
1012 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013
Chris Yea52ade12020-08-27 16:49:20 -07001014 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015
1016 void setKeyboardType(int32_t keyboardType) {
1017 mKeyboardType = keyboardType;
1018 }
1019
1020 void setMetaState(int32_t metaState) {
1021 mMetaState = metaState;
1022 }
1023
1024 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001025 std::unique_lock<std::mutex> lock(mLock);
1026 base::ScopedLockAssertion assumeLocked(mLock);
1027 const bool configureCalled =
1028 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1029 return mConfigureWasCalled;
1030 });
1031 if (!configureCalled) {
1032 FAIL() << "Expected configure() to have been called.";
1033 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034 mConfigureWasCalled = false;
1035 }
1036
1037 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001038 std::unique_lock<std::mutex> lock(mLock);
1039 base::ScopedLockAssertion assumeLocked(mLock);
1040 const bool resetCalled =
1041 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1042 return mResetWasCalled;
1043 });
1044 if (!resetCalled) {
1045 FAIL() << "Expected reset() to have been called.";
1046 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047 mResetWasCalled = false;
1048 }
1049
Yi Kong9b14ac62018-07-17 13:48:38 -07001050 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001051 std::unique_lock<std::mutex> lock(mLock);
1052 base::ScopedLockAssertion assumeLocked(mLock);
1053 const bool processCalled =
1054 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1055 return mProcessWasCalled;
1056 });
1057 if (!processCalled) {
1058 FAIL() << "Expected process() to have been called.";
1059 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001060 if (outLastEvent) {
1061 *outLastEvent = mLastEvent;
1062 }
1063 mProcessWasCalled = false;
1064 }
1065
1066 void setKeyCodeState(int32_t keyCode, int32_t state) {
1067 mKeyCodeStates.replaceValueFor(keyCode, state);
1068 }
1069
1070 void setScanCodeState(int32_t scanCode, int32_t state) {
1071 mScanCodeStates.replaceValueFor(scanCode, state);
1072 }
1073
1074 void setSwitchState(int32_t switchCode, int32_t state) {
1075 mSwitchStates.replaceValueFor(switchCode, state);
1076 }
1077
1078 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001079 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080 }
1081
1082private:
Chris Yea52ade12020-08-27 16:49:20 -07001083 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084
Chris Yea52ade12020-08-27 16:49:20 -07001085 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086 InputMapper::populateDeviceInfo(deviceInfo);
1087
1088 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1089 deviceInfo->setKeyboardType(mKeyboardType);
1090 }
1091 }
1092
Chris Yea52ade12020-08-27 16:49:20 -07001093 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001094 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001096
1097 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001098 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001099 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1100 mViewport = config->getDisplayViewportByPort(*displayPort);
1101 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001102
1103 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104 }
1105
Chris Yea52ade12020-08-27 16:49:20 -07001106 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001107 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001109 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110 }
1111
Chris Yea52ade12020-08-27 16:49:20 -07001112 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001113 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001114 mLastEvent = *rawEvent;
1115 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001116 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117 }
1118
Chris Yea52ade12020-08-27 16:49:20 -07001119 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1121 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1122 }
1123
Chris Yea52ade12020-08-27 16:49:20 -07001124 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001125 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1126 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1127 }
1128
Chris Yea52ade12020-08-27 16:49:20 -07001129 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1131 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1132 }
1133
Chris Yea52ade12020-08-27 16:49:20 -07001134 // Return true if the device has non-empty key layout.
1135 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1136 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001137 for (size_t i = 0; i < numCodes; i++) {
1138 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1139 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1140 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141 }
1142 }
1143 }
Chris Yea52ade12020-08-27 16:49:20 -07001144 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001145 return result;
1146 }
1147
1148 virtual int32_t getMetaState() {
1149 return mMetaState;
1150 }
1151
1152 virtual void fadePointer() {
1153 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001154
1155 virtual std::optional<int32_t> getAssociatedDisplay() {
1156 if (mViewport) {
1157 return std::make_optional(mViewport->displayId);
1158 }
1159 return std::nullopt;
1160 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161};
1162
1163
1164// --- InstrumentedInputReader ---
1165
1166class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001167 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168
1169public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001170 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1171 const sp<InputReaderPolicyInterface>& policy,
1172 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001173 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001175 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001177 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001179 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001180 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181 InputDeviceIdentifier identifier;
1182 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001183 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001185 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 }
1187
Prabir Pradhan28efc192019-11-05 01:10:04 +00001188 // Make the protected loopOnce method accessible to tests.
1189 using InputReader::loopOnce;
1190
Michael Wrightd02c5b62014-02-10 15:10:22 -08001191protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001192 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1193 const InputDeviceIdentifier& identifier)
1194 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001195 if (!mNextDevices.empty()) {
1196 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1197 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001198 return device;
1199 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001200 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001201 }
1202
arthurhungdcef2dc2020-08-11 14:47:50 +08001203 // --- FakeInputReaderContext ---
1204 class FakeInputReaderContext : public ContextImpl {
1205 int32_t mGlobalMetaState;
1206 bool mUpdateGlobalMetaStateWasCalled;
1207 int32_t mGeneration;
1208
1209 public:
1210 FakeInputReaderContext(InputReader* reader)
1211 : ContextImpl(reader),
1212 mGlobalMetaState(0),
1213 mUpdateGlobalMetaStateWasCalled(false),
1214 mGeneration(1) {}
1215
1216 virtual ~FakeInputReaderContext() {}
1217
1218 void assertUpdateGlobalMetaStateWasCalled() {
1219 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1220 << "Expected updateGlobalMetaState() to have been called.";
1221 mUpdateGlobalMetaStateWasCalled = false;
1222 }
1223
1224 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1225
1226 uint32_t getGeneration() { return mGeneration; }
1227
1228 void updateGlobalMetaState() override {
1229 mUpdateGlobalMetaStateWasCalled = true;
1230 ContextImpl::updateGlobalMetaState();
1231 }
1232
1233 int32_t getGlobalMetaState() override {
1234 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1235 }
1236
1237 int32_t bumpGeneration() override {
1238 mGeneration = ContextImpl::bumpGeneration();
1239 return mGeneration;
1240 }
1241 } mFakeContext;
1242
Michael Wrightd02c5b62014-02-10 15:10:22 -08001243 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001244
1245public:
1246 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247};
1248
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001249// --- InputReaderPolicyTest ---
1250class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001251protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001252 sp<FakeInputReaderPolicy> mFakePolicy;
1253
Chris Yea52ade12020-08-27 16:49:20 -07001254 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1255 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001256};
1257
1258/**
1259 * Check that empty set of viewports is an acceptable configuration.
1260 * Also try to get internal viewport two different ways - by type and by uniqueId.
1261 *
1262 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1263 * Such configuration is not currently allowed.
1264 */
1265TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001266 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001267
1268 // We didn't add any viewports yet, so there shouldn't be any.
1269 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001270 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001271 ASSERT_FALSE(internalViewport);
1272
1273 // Add an internal viewport, then clear it
1274 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001275 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001276 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001277
1278 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001279 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001280 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001281 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001282
1283 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001284 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001285 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001286 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001287
1288 mFakePolicy->clearViewports();
1289 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001290 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001291 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001292 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001293 ASSERT_FALSE(internalViewport);
1294}
1295
1296TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1297 const std::string internalUniqueId = "local:0";
1298 const std::string externalUniqueId = "local:1";
1299 const std::string virtualUniqueId1 = "virtual:2";
1300 const std::string virtualUniqueId2 = "virtual:3";
1301 constexpr int32_t virtualDisplayId1 = 2;
1302 constexpr int32_t virtualDisplayId2 = 3;
1303
1304 // Add an internal viewport
1305 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001306 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1307 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001308 // Add an external viewport
1309 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001310 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1311 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001312 // Add an virtual viewport
1313 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001314 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1315 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001316 // Add another virtual viewport
1317 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001318 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1319 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001320
1321 // Check matching by type for internal
1322 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001323 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001324 ASSERT_TRUE(internalViewport);
1325 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1326
1327 // Check matching by type for external
1328 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001329 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330 ASSERT_TRUE(externalViewport);
1331 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1332
1333 // Check matching by uniqueId for virtual viewport #1
1334 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001335 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001336 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001337 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001338 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1339 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1340
1341 // Check matching by uniqueId for virtual viewport #2
1342 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001343 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001344 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001345 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1347 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1348}
1349
1350
1351/**
1352 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1353 * that lookup works by checking display id.
1354 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1355 */
1356TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1357 const std::string uniqueId1 = "uniqueId1";
1358 const std::string uniqueId2 = "uniqueId2";
1359 constexpr int32_t displayId1 = 2;
1360 constexpr int32_t displayId2 = 3;
1361
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001362 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1363 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364 for (const ViewportType& type : types) {
1365 mFakePolicy->clearViewports();
1366 // Add a viewport
1367 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001368 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1369 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370 // Add another viewport
1371 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001372 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1373 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374
1375 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001376 std::optional<DisplayViewport> viewport1 =
1377 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001378 ASSERT_TRUE(viewport1);
1379 ASSERT_EQ(displayId1, viewport1->displayId);
1380 ASSERT_EQ(type, viewport1->type);
1381
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001382 std::optional<DisplayViewport> viewport2 =
1383 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001384 ASSERT_TRUE(viewport2);
1385 ASSERT_EQ(displayId2, viewport2->displayId);
1386 ASSERT_EQ(type, viewport2->type);
1387
1388 // When there are multiple viewports of the same kind, and uniqueId is not specified
1389 // in the call to getDisplayViewport, then that situation is not supported.
1390 // The viewports can be stored in any order, so we cannot rely on the order, since that
1391 // is just implementation detail.
1392 // However, we can check that it still returns *a* viewport, we just cannot assert
1393 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001394 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001395 ASSERT_TRUE(someViewport);
1396 }
1397}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001398
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001399/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001400 * When we have multiple internal displays make sure we always return the default display when
1401 * querying by type.
1402 */
1403TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1404 const std::string uniqueId1 = "uniqueId1";
1405 const std::string uniqueId2 = "uniqueId2";
1406 constexpr int32_t nonDefaultDisplayId = 2;
1407 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1408 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1409
1410 // Add the default display first and ensure it gets returned.
1411 mFakePolicy->clearViewports();
1412 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001413 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001414 ViewportType::INTERNAL);
1415 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001416 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001417 ViewportType::INTERNAL);
1418
1419 std::optional<DisplayViewport> viewport =
1420 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1421 ASSERT_TRUE(viewport);
1422 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1423 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1424
1425 // Add the default display second to make sure order doesn't matter.
1426 mFakePolicy->clearViewports();
1427 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001428 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001429 ViewportType::INTERNAL);
1430 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001431 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001432 ViewportType::INTERNAL);
1433
1434 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1435 ASSERT_TRUE(viewport);
1436 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1437 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1438}
1439
1440/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001441 * Check getDisplayViewportByPort
1442 */
1443TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001444 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001445 const std::string uniqueId1 = "uniqueId1";
1446 const std::string uniqueId2 = "uniqueId2";
1447 constexpr int32_t displayId1 = 1;
1448 constexpr int32_t displayId2 = 2;
1449 const uint8_t hdmi1 = 0;
1450 const uint8_t hdmi2 = 1;
1451 const uint8_t hdmi3 = 2;
1452
1453 mFakePolicy->clearViewports();
1454 // Add a viewport that's associated with some display port that's not of interest.
1455 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001456 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1457 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001458 // Add another viewport, connected to HDMI1 port
1459 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001460 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1461 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001462
1463 // Check that correct display viewport was returned by comparing the display ports.
1464 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1465 ASSERT_TRUE(hdmi1Viewport);
1466 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1467 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1468
1469 // Check that we can still get the same viewport using the uniqueId
1470 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1471 ASSERT_TRUE(hdmi1Viewport);
1472 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1473 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1474 ASSERT_EQ(type, hdmi1Viewport->type);
1475
1476 // Check that we cannot find a port with "HDMI2", because we never added one
1477 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1478 ASSERT_FALSE(hdmi2Viewport);
1479}
1480
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481// --- InputReaderTest ---
1482
1483class InputReaderTest : public testing::Test {
1484protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001485 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001486 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001487 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001488 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001489
Chris Yea52ade12020-08-27 16:49:20 -07001490 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001491 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001492 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001493 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001494
Prabir Pradhan28efc192019-11-05 01:10:04 +00001495 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1496 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497 }
1498
Chris Yea52ade12020-08-27 16:49:20 -07001499 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001500 mFakeListener.clear();
1501 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001502 }
1503
Chris Ye1b0c7342020-07-28 21:57:03 -07001504 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001505 const PropertyMap* configuration) {
1506 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001507
1508 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001509 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001510 }
1511 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001512 mReader->loopOnce();
1513 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001514 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1515 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516 }
1517
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001518 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001519 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001520 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001521 }
1522
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001523 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001524 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001525 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001526 }
1527
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001528 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001529 const std::string& name,
1530 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001531 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001532 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1533 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001534 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001535 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001536 return mapper;
1537 }
1538};
1539
Chris Ye98d3f532020-10-01 21:48:59 -07001540TEST_F(InputReaderTest, PolicyGetInputDevices) {
1541 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1542 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1543 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544
1545 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001546 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001547 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001548 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001549 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001550 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1551 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001552 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553}
1554
Chris Yee7310032020-09-22 15:36:28 -07001555TEST_F(InputReaderTest, GetMergedInputDevices) {
1556 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1557 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1558 // Add two subdevices to device
1559 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1560 // Must add at least one mapper or the device will be ignored!
1561 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1562 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1563
1564 // Push same device instance for next device to be added, so they'll have same identifier.
1565 mReader->pushNextDevice(device);
1566 mReader->pushNextDevice(device);
1567 ASSERT_NO_FATAL_FAILURE(
1568 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1569 ASSERT_NO_FATAL_FAILURE(
1570 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1571
1572 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001573 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001574}
1575
Chris Yee14523a2020-12-19 13:46:00 -08001576TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1577 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1578 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1579 // Add two subdevices to device
1580 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1581 // Must add at least one mapper or the device will be ignored!
1582 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1583 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1584
1585 // Push same device instance for next device to be added, so they'll have same identifier.
1586 mReader->pushNextDevice(device);
1587 mReader->pushNextDevice(device);
1588 // Sensor device is initially disabled
1589 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1590 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1591 nullptr));
1592 // Device is disabled because the only sub device is a sensor device and disabled initially.
1593 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1594 ASSERT_FALSE(device->isEnabled());
1595 ASSERT_NO_FATAL_FAILURE(
1596 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1597 // The merged device is enabled if any sub device is enabled
1598 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1599 ASSERT_TRUE(device->isEnabled());
1600}
1601
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001602TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001603 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001604 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 constexpr int32_t eventHubId = 1;
1606 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001607 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001608 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001609 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001610 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001611
Yi Kong9b14ac62018-07-17 13:48:38 -07001612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001613
1614 NotifyDeviceResetArgs resetArgs;
1615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001616 ASSERT_EQ(deviceId, resetArgs.deviceId);
1617
1618 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001619 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001620 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001621
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001623 ASSERT_EQ(deviceId, resetArgs.deviceId);
1624 ASSERT_EQ(device->isEnabled(), false);
1625
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001626 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001627 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001630 ASSERT_EQ(device->isEnabled(), false);
1631
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001632 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001633 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001635 ASSERT_EQ(deviceId, resetArgs.deviceId);
1636 ASSERT_EQ(device->isEnabled(), true);
1637}
1638
Michael Wrightd02c5b62014-02-10 15:10:22 -08001639TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001640 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001641 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001643 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001644 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001645 AINPUT_SOURCE_KEYBOARD, nullptr);
1646 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001647
1648 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1649 AINPUT_SOURCE_ANY, AKEYCODE_A))
1650 << "Should return unknown when the device id is >= 0 but unknown.";
1651
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001652 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1653 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1654 << "Should return unknown when the device id is valid but the sources are not "
1655 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001656
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001657 ASSERT_EQ(AKEY_STATE_DOWN,
1658 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1659 AKEYCODE_A))
1660 << "Should return value provided by mapper when device id is valid and the device "
1661 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001662
1663 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1664 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1665 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1666
1667 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1668 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1669 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1670}
1671
1672TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001673 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001674 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001675 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001676 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001677 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001678 AINPUT_SOURCE_KEYBOARD, nullptr);
1679 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001680
1681 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1682 AINPUT_SOURCE_ANY, KEY_A))
1683 << "Should return unknown when the device id is >= 0 but unknown.";
1684
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1686 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1687 << "Should return unknown when the device id is valid but the sources are not "
1688 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001689
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001690 ASSERT_EQ(AKEY_STATE_DOWN,
1691 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1692 KEY_A))
1693 << "Should return value provided by mapper when device id is valid and the device "
1694 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001695
1696 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1697 AINPUT_SOURCE_TRACKBALL, KEY_A))
1698 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1699
1700 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1701 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1702 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1703}
1704
1705TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001707 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001709 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001711 AINPUT_SOURCE_KEYBOARD, nullptr);
1712 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001713
1714 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1715 AINPUT_SOURCE_ANY, SW_LID))
1716 << "Should return unknown when the device id is >= 0 but unknown.";
1717
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001718 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1719 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1720 << "Should return unknown when the device id is valid but the sources are not "
1721 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001722
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001723 ASSERT_EQ(AKEY_STATE_DOWN,
1724 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1725 SW_LID))
1726 << "Should return value provided by mapper when device id is valid and the device "
1727 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001728
1729 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1730 AINPUT_SOURCE_TRACKBALL, SW_LID))
1731 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1732
1733 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1734 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1735 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1736}
1737
1738TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001739 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001740 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001741 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001742 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001743 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001744 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001746 mapper.addSupportedKeyCode(AKEYCODE_A);
1747 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001748
1749 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1750 uint8_t flags[4] = { 0, 0, 0, 1 };
1751
1752 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1753 << "Should return false when device id is >= 0 but unknown.";
1754 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1755
1756 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001757 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1758 << "Should return false when device id is valid but the sources are not supported by "
1759 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001760 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1761
1762 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001763 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1764 keyCodes, flags))
1765 << "Should return value provided by mapper when device id is valid and the device "
1766 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001767 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1768
1769 flags[3] = 1;
1770 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1771 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1772 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1773
1774 flags[3] = 1;
1775 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1776 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1777 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1778}
1779
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001780TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001781 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001782 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001783
1784 NotifyConfigurationChangedArgs args;
1785
1786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1787 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1788}
1789
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001790TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001791 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001792 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001793 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001794 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001795 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001796 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001797 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001798 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001800 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001801 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001802 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1803
1804 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001805 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001806 ASSERT_EQ(when, event.when);
1807 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809 ASSERT_EQ(EV_KEY, event.type);
1810 ASSERT_EQ(KEY_A, event.code);
1811 ASSERT_EQ(1, event.value);
1812}
1813
Garfield Tan1c7bc862020-01-28 13:24:04 -08001814TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001815 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001816 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001817 constexpr int32_t eventHubId = 1;
1818 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001819 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001820 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001821 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001822 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001823
1824 NotifyDeviceResetArgs resetArgs;
1825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001826 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001828 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001829 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001831 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001832 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001833
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001834 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001835 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001837 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001838 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001839
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001840 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001841 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001843 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001844 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001845}
1846
Garfield Tan1c7bc862020-01-28 13:24:04 -08001847TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1848 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001849 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001850 constexpr int32_t eventHubId = 1;
1851 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1852 // Must add at least one mapper or the device will be ignored!
1853 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001854 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001855 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1856
1857 NotifyDeviceResetArgs resetArgs;
1858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1859 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1860}
1861
Arthur Hungc23540e2018-11-29 20:42:11 +08001862TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001863 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001864 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001865 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001866 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001867 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1868 FakeInputMapper& mapper =
1869 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001870 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001871
1872 const uint8_t hdmi1 = 1;
1873
1874 // Associated touch screen with second display.
1875 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1876
1877 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001878 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001879 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001880 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001881 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001882 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001883 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001884 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001885 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001886 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001887
1888 // Add the device, and make sure all of the callbacks are triggered.
1889 // The device is added after the input port associations are processed since
1890 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001891 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001894 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001895
Arthur Hung2c9a3342019-07-23 14:18:59 +08001896 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001897 ASSERT_EQ(deviceId, device->getId());
1898 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1899 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001900
1901 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001902 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001903 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001904 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001905}
1906
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001907TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1908 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1909 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1910 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1911 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1912 // Must add at least one mapper or the device will be ignored!
1913 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1914 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1915 mReader->pushNextDevice(device);
1916 mReader->pushNextDevice(device);
1917 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1918 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1919
1920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1921
1922 NotifyDeviceResetArgs resetArgs;
1923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1924 ASSERT_EQ(deviceId, resetArgs.deviceId);
1925 ASSERT_TRUE(device->isEnabled());
1926 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1927 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1928
1929 disableDevice(deviceId);
1930 mReader->loopOnce();
1931
1932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1933 ASSERT_EQ(deviceId, resetArgs.deviceId);
1934 ASSERT_FALSE(device->isEnabled());
1935 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1936 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1937
1938 enableDevice(deviceId);
1939 mReader->loopOnce();
1940
1941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1942 ASSERT_EQ(deviceId, resetArgs.deviceId);
1943 ASSERT_TRUE(device->isEnabled());
1944 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1945 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1946}
1947
1948TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1949 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1950 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1951 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1952 // Add two subdevices to device
1953 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1954 FakeInputMapper& mapperDevice1 =
1955 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1956 FakeInputMapper& mapperDevice2 =
1957 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1958 mReader->pushNextDevice(device);
1959 mReader->pushNextDevice(device);
1960 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1961 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1962
1963 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1964 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1965
1966 ASSERT_EQ(AKEY_STATE_DOWN,
1967 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1968 ASSERT_EQ(AKEY_STATE_DOWN,
1969 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1970 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1971 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1972}
1973
Prabir Pradhan7e186182020-11-10 13:56:45 -08001974TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1975 NotifyPointerCaptureChangedArgs args;
1976
1977 mFakePolicy->setPointerCapture(true);
1978 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1979 mReader->loopOnce();
1980 mFakeListener->assertNotifyCaptureWasCalled(&args);
1981 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1982
1983 mFakePolicy->setPointerCapture(false);
1984 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1985 mReader->loopOnce();
1986 mFakeListener->assertNotifyCaptureWasCalled(&args);
1987 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1988
1989 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1990 // does not change.
1991 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1992 mReader->loopOnce();
1993 mFakeListener->assertNotifyCaptureWasCalled(&args);
1994 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1995}
1996
Chris Ye87143712020-11-10 05:05:58 +00001997class FakeVibratorInputMapper : public FakeInputMapper {
1998public:
1999 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2000 : FakeInputMapper(deviceContext, sources) {}
2001
2002 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2003};
2004
2005TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2006 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2007 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2008 constexpr int32_t eventHubId = 1;
2009 const char* DEVICE_LOCATION = "BLUETOOTH";
2010 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2011 FakeVibratorInputMapper& mapper =
2012 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2013 mReader->pushNextDevice(device);
2014
2015 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2016 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2017
2018 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2019 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2020}
2021
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002022// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002023
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002024class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002025public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002026 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002027
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002028 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002029
2030 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2031
2032 void dump(std::string& dump) override {}
2033
2034 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2035 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002036 }
2037
Chris Yee2b1e5c2021-03-10 22:45:12 -08002038 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2039 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002040 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002041
2042 bool setLightColor(int32_t lightId, int32_t color) override {
2043 getDeviceContext().setLightBrightness(lightId, color >> 24);
2044 return true;
2045 }
2046
2047 std::optional<int32_t> getLightColor(int32_t lightId) override {
2048 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2049 if (!result.has_value()) {
2050 return std::nullopt;
2051 }
2052 return result.value() << 24;
2053 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002054
2055 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2056
2057 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2058
2059private:
2060 InputDeviceContext& mDeviceContext;
2061 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2062 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002063};
2064
Chris Yee2b1e5c2021-03-10 22:45:12 -08002065TEST_F(InputReaderTest, BatteryGetCapacity) {
2066 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2067 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2068 constexpr int32_t eventHubId = 1;
2069 const char* DEVICE_LOCATION = "BLUETOOTH";
2070 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002071 FakePeripheralController& controller =
2072 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002073 mReader->pushNextDevice(device);
2074
2075 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2076
2077 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2078 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2079}
2080
2081TEST_F(InputReaderTest, BatteryGetStatus) {
2082 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2083 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2084 constexpr int32_t eventHubId = 1;
2085 const char* DEVICE_LOCATION = "BLUETOOTH";
2086 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002087 FakePeripheralController& controller =
2088 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002089 mReader->pushNextDevice(device);
2090
2091 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2092
2093 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2094 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2095}
2096
Chris Ye3fdbfef2021-01-06 18:45:18 -08002097TEST_F(InputReaderTest, LightGetColor) {
2098 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2099 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2100 constexpr int32_t eventHubId = 1;
2101 const char* DEVICE_LOCATION = "BLUETOOTH";
2102 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002103 FakePeripheralController& controller =
2104 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002105 mReader->pushNextDevice(device);
2106 RawLightInfo info = {.id = 1,
2107 .name = "Mono",
2108 .maxBrightness = 255,
2109 .flags = InputLightClass::BRIGHTNESS,
2110 .path = ""};
2111 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2112 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2113
2114 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002115
Chris Yee2b1e5c2021-03-10 22:45:12 -08002116 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2117 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002118 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2119 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2120}
2121
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002122// --- InputReaderIntegrationTest ---
2123
2124// These tests create and interact with the InputReader only through its interface.
2125// The InputReader is started during SetUp(), which starts its processing in its own
2126// thread. The tests use linux uinput to emulate input devices.
2127// NOTE: Interacting with the physical device while these tests are running may cause
2128// the tests to fail.
2129class InputReaderIntegrationTest : public testing::Test {
2130protected:
2131 sp<TestInputListener> mTestListener;
2132 sp<FakeInputReaderPolicy> mFakePolicy;
2133 sp<InputReaderInterface> mReader;
2134
Chris Yea52ade12020-08-27 16:49:20 -07002135 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002136 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002137 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2138 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002139
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002140 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002141 ASSERT_EQ(mReader->start(), OK);
2142
2143 // Since this test is run on a real device, all the input devices connected
2144 // to the test device will show up in mReader. We wait for those input devices to
2145 // show up before beginning the tests.
2146 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2147 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2148 }
2149
Chris Yea52ade12020-08-27 16:49:20 -07002150 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002151 ASSERT_EQ(mReader->stop(), OK);
2152 mTestListener.clear();
2153 mFakePolicy.clear();
2154 }
2155};
2156
2157TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2158 // An invalid input device that is only used for this test.
2159 class InvalidUinputDevice : public UinputDevice {
2160 public:
2161 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2162
2163 private:
2164 void configureDevice(int fd, uinput_user_dev* device) override {}
2165 };
2166
2167 const size_t numDevices = mFakePolicy->getInputDevices().size();
2168
2169 // UinputDevice does not set any event or key bits, so InputReader should not
2170 // consider it as a valid device.
2171 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2172 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2173 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2174 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2175
2176 invalidDevice.reset();
2177 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2178 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2179 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2180}
2181
2182TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2183 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2184
2185 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2186 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2187 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2188 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2189
2190 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002191 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002192 const auto& it =
2193 std::find_if(inputDevices.begin(), inputDevices.end(),
2194 [&keyboard](const InputDeviceInfo& info) {
2195 return info.getIdentifier().name == keyboard->getName();
2196 });
2197
2198 ASSERT_NE(it, inputDevices.end());
2199 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2200 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2201 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002202
2203 keyboard.reset();
2204 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2205 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2206 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2207}
2208
2209TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2210 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2211 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2212
2213 NotifyConfigurationChangedArgs configChangedArgs;
2214 ASSERT_NO_FATAL_FAILURE(
2215 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002216 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002217 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2218
2219 NotifyKeyArgs keyArgs;
2220 keyboard->pressAndReleaseHomeKey();
2221 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2222 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002223 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002224 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002225 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002226 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002227 prevTimestamp = keyArgs.eventTime;
2228
2229 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2230 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002231 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002232 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002233 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002234}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002235
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002236/**
2237 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2238 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2239 * are passed to the listener.
2240 */
2241static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2242TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2243 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2244 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2245 NotifyKeyArgs keyArgs;
2246
2247 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2248 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2249 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2250 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2251
2252 controller->pressAndReleaseKey(BTN_GEAR_UP);
2253 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2254 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2255 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2256}
2257
Arthur Hungaab25622020-01-16 11:22:11 +08002258// --- TouchProcessTest ---
2259class TouchIntegrationTest : public InputReaderIntegrationTest {
2260protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002261 const std::string UNIQUE_ID = "local:0";
2262
Chris Yea52ade12020-08-27 16:49:20 -07002263 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002264 InputReaderIntegrationTest::SetUp();
2265 // At least add an internal display.
2266 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2267 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002268 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002269
2270 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2271 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2272 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2273 }
2274
2275 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2276 int32_t orientation, const std::string& uniqueId,
2277 std::optional<uint8_t> physicalPort,
2278 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002279 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2280 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002281 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2282 }
2283
2284 std::unique_ptr<UinputTouchScreen> mDevice;
2285};
2286
2287TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2288 NotifyMotionArgs args;
2289 const Point centerPoint = mDevice->getCenterPoint();
2290
2291 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002292 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002293 mDevice->sendDown(centerPoint);
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2296
2297 // ACTION_MOVE
2298 mDevice->sendMove(centerPoint + Point(1, 1));
2299 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2301
2302 // ACTION_UP
2303 mDevice->sendUp();
2304 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2305 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2306}
2307
2308TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2309 NotifyMotionArgs args;
2310 const Point centerPoint = mDevice->getCenterPoint();
2311
2312 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002313 mDevice->sendSlot(FIRST_SLOT);
2314 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002315 mDevice->sendDown(centerPoint);
2316 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2318
2319 // ACTION_POINTER_DOWN (Second slot)
2320 const Point secondPoint = centerPoint + Point(100, 100);
2321 mDevice->sendSlot(SECOND_SLOT);
2322 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2323 mDevice->sendDown(secondPoint + Point(1, 1));
2324 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2325 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2326 args.action);
2327
2328 // ACTION_MOVE (Second slot)
2329 mDevice->sendMove(secondPoint);
2330 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2332
2333 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002334 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002335 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002336 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002337 args.action);
2338
2339 // ACTION_UP
2340 mDevice->sendSlot(FIRST_SLOT);
2341 mDevice->sendUp();
2342 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2343 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2344}
2345
2346TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2347 NotifyMotionArgs args;
2348 const Point centerPoint = mDevice->getCenterPoint();
2349
2350 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002351 mDevice->sendSlot(FIRST_SLOT);
2352 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002353 mDevice->sendDown(centerPoint);
2354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2355 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2356
arthurhungcc7f9802020-04-30 17:55:40 +08002357 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002358 const Point secondPoint = centerPoint + Point(100, 100);
2359 mDevice->sendSlot(SECOND_SLOT);
2360 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2361 mDevice->sendDown(secondPoint);
2362 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2363 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2364 args.action);
2365
arthurhungcc7f9802020-04-30 17:55:40 +08002366 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002367 mDevice->sendMove(secondPoint + Point(1, 1));
2368 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2370
arthurhungcc7f9802020-04-30 17:55:40 +08002371 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2372 // a palm event.
2373 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002374 mDevice->sendToolType(MT_TOOL_PALM);
2375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002376 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2377 args.action);
2378 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002379
arthurhungcc7f9802020-04-30 17:55:40 +08002380 // Send up to second slot, expect first slot send moving.
2381 mDevice->sendPointerUp();
2382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002384
arthurhungcc7f9802020-04-30 17:55:40 +08002385 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002386 mDevice->sendSlot(FIRST_SLOT);
2387 mDevice->sendUp();
2388
arthurhungcc7f9802020-04-30 17:55:40 +08002389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2390 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002391}
2392
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394class InputDeviceTest : public testing::Test {
2395protected:
2396 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002397 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398 static const int32_t DEVICE_ID;
2399 static const int32_t DEVICE_GENERATION;
2400 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002401 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002402 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002404 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002406 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002407 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002408 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409
Chris Yea52ade12020-08-27 16:49:20 -07002410 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002411 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002413 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002414 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2415 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416 InputDeviceIdentifier identifier;
2417 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002418 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002419 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002420 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002421 mReader->pushNextDevice(mDevice);
2422 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2423 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424 }
2425
Chris Yea52ade12020-08-27 16:49:20 -07002426 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427 mFakeListener.clear();
2428 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002429 }
2430};
2431
2432const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002433const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002434const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2436const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002437const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2438 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002439const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002440
2441TEST_F(InputDeviceTest, ImmutableProperties) {
2442 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002443 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002444 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445}
2446
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002447TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2448 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002449}
2450
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2452 // Configuration.
2453 InputReaderConfiguration config;
2454 mDevice->configure(ARBITRARY_TIME, &config, 0);
2455
2456 // Reset.
2457 mDevice->reset(ARBITRARY_TIME);
2458
2459 NotifyDeviceResetArgs resetArgs;
2460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2461 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2462 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2463
2464 // Metadata.
2465 ASSERT_TRUE(mDevice->isIgnored());
2466 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2467
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002468 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002470 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2472 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2473
2474 // State queries.
2475 ASSERT_EQ(0, mDevice->getMetaState());
2476
2477 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2478 << "Ignored device should return unknown key code state.";
2479 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2480 << "Ignored device should return unknown scan code state.";
2481 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2482 << "Ignored device should return unknown switch state.";
2483
2484 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2485 uint8_t flags[2] = { 0, 1 };
2486 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2487 << "Ignored device should never mark any key codes.";
2488 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2489 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2490}
2491
2492TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2493 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002494 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002495
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002496 FakeInputMapper& mapper1 =
2497 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002498 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2499 mapper1.setMetaState(AMETA_ALT_ON);
2500 mapper1.addSupportedKeyCode(AKEYCODE_A);
2501 mapper1.addSupportedKeyCode(AKEYCODE_B);
2502 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2503 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2504 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2505 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2506 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002507
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002508 FakeInputMapper& mapper2 =
2509 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002510 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511
2512 InputReaderConfiguration config;
2513 mDevice->configure(ARBITRARY_TIME, &config, 0);
2514
2515 String8 propertyValue;
2516 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2517 << "Device should have read configuration during configuration phase.";
2518 ASSERT_STREQ("value", propertyValue.string());
2519
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002520 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2521 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522
2523 // Reset
2524 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002525 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2526 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002527
2528 NotifyDeviceResetArgs resetArgs;
2529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2530 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2531 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2532
2533 // Metadata.
2534 ASSERT_FALSE(mDevice->isIgnored());
2535 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2536
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002537 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002538 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002539 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2541 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2542
2543 // State queries.
2544 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2545 << "Should query mappers and combine meta states.";
2546
2547 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2548 << "Should return unknown key code state when source not supported.";
2549 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2550 << "Should return unknown scan code state when source not supported.";
2551 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2552 << "Should return unknown switch state when source not supported.";
2553
2554 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2555 << "Should query mapper when source is supported.";
2556 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2557 << "Should query mapper when source is supported.";
2558 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2559 << "Should query mapper when source is supported.";
2560
2561 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2562 uint8_t flags[4] = { 0, 0, 0, 1 };
2563 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2564 << "Should do nothing when source is unsupported.";
2565 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2566 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2567 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2568 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2569
2570 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2571 << "Should query mapper when source is supported.";
2572 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2573 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2574 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2575 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2576
2577 // Event handling.
2578 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002579 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580 mDevice->process(&event, 1);
2581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002582 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2583 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002584}
2585
Arthur Hung2c9a3342019-07-23 14:18:59 +08002586// A single input device is associated with a specific display. Check that:
2587// 1. Device is disabled if the viewport corresponding to the associated display is not found
2588// 2. Device is disabled when setEnabled API is called
2589TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002590 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002591
2592 // First Configuration.
2593 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2594
2595 // Device should be enabled by default.
2596 ASSERT_TRUE(mDevice->isEnabled());
2597
2598 // Prepare associated info.
2599 constexpr uint8_t hdmi = 1;
2600 const std::string UNIQUE_ID = "local:1";
2601
2602 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2603 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2604 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2605 // Device should be disabled because it is associated with a specific display via
2606 // input port <-> display port association, but the corresponding display is not found
2607 ASSERT_FALSE(mDevice->isEnabled());
2608
2609 // Prepare displays.
2610 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002611 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2612 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002613 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2614 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2615 ASSERT_TRUE(mDevice->isEnabled());
2616
2617 // Device should be disabled after set disable.
2618 mFakePolicy->addDisabledDevice(mDevice->getId());
2619 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2620 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2621 ASSERT_FALSE(mDevice->isEnabled());
2622
2623 // Device should still be disabled even found the associated display.
2624 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2625 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2626 ASSERT_FALSE(mDevice->isEnabled());
2627}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002628
Christine Franks1ba71cc2021-04-07 14:37:42 -07002629TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2630 // Device should be enabled by default.
2631 mFakePolicy->clearViewports();
2632 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2633 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2634 ASSERT_TRUE(mDevice->isEnabled());
2635
2636 // Device should be disabled because it is associated with a specific display, but the
2637 // corresponding display is not found.
2638 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2639 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2640 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2641 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2642 ASSERT_FALSE(mDevice->isEnabled());
2643
2644 // Device should be enabled when a display is found.
2645 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2646 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2647 NO_PORT, ViewportType::INTERNAL);
2648 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2649 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2650 ASSERT_TRUE(mDevice->isEnabled());
2651
2652 // Device should be disabled after set disable.
2653 mFakePolicy->addDisabledDevice(mDevice->getId());
2654 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2655 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2656 ASSERT_FALSE(mDevice->isEnabled());
2657
2658 // Device should still be disabled even found the associated display.
2659 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2660 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2661 ASSERT_FALSE(mDevice->isEnabled());
2662}
2663
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664// --- InputMapperTest ---
2665
2666class InputMapperTest : public testing::Test {
2667protected:
2668 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002669 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670 static const int32_t DEVICE_ID;
2671 static const int32_t DEVICE_GENERATION;
2672 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002673 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002674 static const int32_t EVENTHUB_ID;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002675 static const std::optional<bool> INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002677 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002679 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002680 std::unique_ptr<InstrumentedInputReader> mReader;
2681 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682
Chris Ye1b0c7342020-07-28 21:57:03 -07002683 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002684 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002686 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002687 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2688 mFakeListener);
2689 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 }
2691
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002692 void SetUp() override {
2693 // Ensure per_window_input_rotation is enabled.
2694 sysprop::InputFlingerProperties::per_window_input_rotation(true);
2695
2696 SetUp(DEVICE_CLASSES);
2697 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002698
Chris Yea52ade12020-08-27 16:49:20 -07002699 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700 mFakeListener.clear();
2701 mFakePolicy.clear();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002702
2703 sysprop::InputFlingerProperties::per_window_input_rotation(
2704 INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705 }
2706
2707 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002708 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709 }
2710
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002711 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002712 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002713 mReader->requestRefreshConfiguration(changes);
2714 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002715 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002716 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2717 }
2718
arthurhungdcef2dc2020-08-11 14:47:50 +08002719 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2720 const std::string& location, int32_t eventHubId,
2721 Flags<InputDeviceClass> classes) {
2722 InputDeviceIdentifier identifier;
2723 identifier.name = name;
2724 identifier.location = location;
2725 std::shared_ptr<InputDevice> device =
2726 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2727 identifier);
2728 mReader->pushNextDevice(device);
2729 mFakeEventHub->addDevice(eventHubId, name, classes);
2730 mReader->loopOnce();
2731 return device;
2732 }
2733
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002734 template <class T, typename... Args>
2735 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002736 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002737 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002739 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002740 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741 }
2742
2743 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002744 int32_t orientation, const std::string& uniqueId,
2745 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002746 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2747 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002748 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2749 }
2750
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002751 void clearViewports() {
2752 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753 }
2754
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002755 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2756 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002757 RawEvent event;
2758 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002759 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002760 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 event.type = type;
2762 event.code = code;
2763 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002764 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002765 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766 }
2767
2768 static void assertMotionRange(const InputDeviceInfo& info,
2769 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2770 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002771 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2773 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2774 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2775 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2776 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2777 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2778 }
2779
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002780 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2781 float size, float touchMajor, float touchMinor, float toolMajor,
2782 float toolMinor, float orientation, float distance,
2783 float scaledAxisEpsilon = 1.f) {
2784 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2785 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2787 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002788 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2789 scaledAxisEpsilon);
2790 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2791 scaledAxisEpsilon);
2792 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2793 scaledAxisEpsilon);
2794 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2795 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2797 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2798 }
2799
Michael Wright17db18e2020-06-26 20:51:44 +01002800 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002801 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002802 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803 ASSERT_NEAR(x, actualX, 1);
2804 ASSERT_NEAR(y, actualY, 1);
2805 }
2806};
2807
2808const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002809const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002810const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002811const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2812const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002813const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2814 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002815const int32_t InputMapperTest::EVENTHUB_ID = 1;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002816const std::optional<bool> InputMapperTest::INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE =
2817 sysprop::InputFlingerProperties::per_window_input_rotation();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818
2819// --- SwitchInputMapperTest ---
2820
2821class SwitchInputMapperTest : public InputMapperTest {
2822protected:
2823};
2824
2825TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002828 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829}
2830
2831TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002832 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002834 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002835 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002837 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002838 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839}
2840
2841TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002842 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848
2849 NotifySwitchArgs args;
2850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2851 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002852 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2853 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854 args.switchMask);
2855 ASSERT_EQ(uint32_t(0), args.policyFlags);
2856}
2857
Chris Ye87143712020-11-10 05:05:58 +00002858// --- VibratorInputMapperTest ---
2859class VibratorInputMapperTest : public InputMapperTest {
2860protected:
2861 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2862};
2863
2864TEST_F(VibratorInputMapperTest, GetSources) {
2865 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2866
2867 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2868}
2869
2870TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2871 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2872
2873 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2874}
2875
2876TEST_F(VibratorInputMapperTest, Vibrate) {
2877 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002878 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002879 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2880
2881 VibrationElement pattern(2);
2882 VibrationSequence sequence(2);
2883 pattern.duration = std::chrono::milliseconds(200);
2884 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2885 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2886 sequence.addElement(pattern);
2887 pattern.duration = std::chrono::milliseconds(500);
2888 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2889 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2890 sequence.addElement(pattern);
2891
2892 std::vector<int64_t> timings = {0, 1};
2893 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2894
2895 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002896 // Start vibrating
2897 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002898 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002899 // Verify vibrator state listener was notified.
2900 mReader->loopOnce();
2901 NotifyVibratorStateArgs args;
2902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2903 ASSERT_EQ(DEVICE_ID, args.deviceId);
2904 ASSERT_TRUE(args.isOn);
2905 // Stop vibrating
2906 mapper.cancelVibrate(VIBRATION_TOKEN);
2907 ASSERT_FALSE(mapper.isVibrating());
2908 // Verify vibrator state listener was notified.
2909 mReader->loopOnce();
2910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2911 ASSERT_EQ(DEVICE_ID, args.deviceId);
2912 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002913}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002914
Chris Yef59a2f42020-10-16 12:55:26 -07002915// --- SensorInputMapperTest ---
2916
2917class SensorInputMapperTest : public InputMapperTest {
2918protected:
2919 static const int32_t ACCEL_RAW_MIN;
2920 static const int32_t ACCEL_RAW_MAX;
2921 static const int32_t ACCEL_RAW_FUZZ;
2922 static const int32_t ACCEL_RAW_FLAT;
2923 static const int32_t ACCEL_RAW_RESOLUTION;
2924
2925 static const int32_t GYRO_RAW_MIN;
2926 static const int32_t GYRO_RAW_MAX;
2927 static const int32_t GYRO_RAW_FUZZ;
2928 static const int32_t GYRO_RAW_FLAT;
2929 static const int32_t GYRO_RAW_RESOLUTION;
2930
2931 static const float GRAVITY_MS2_UNIT;
2932 static const float DEGREE_RADIAN_UNIT;
2933
2934 void prepareAccelAxes();
2935 void prepareGyroAxes();
2936 void setAccelProperties();
2937 void setGyroProperties();
2938 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2939};
2940
2941const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2942const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2943const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2944const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2945const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2946
2947const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2948const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2949const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2950const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2951const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2952
2953const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2954const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2955
2956void SensorInputMapperTest::prepareAccelAxes() {
2957 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2958 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2959 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2960 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2961 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2962 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2963}
2964
2965void SensorInputMapperTest::prepareGyroAxes() {
2966 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2967 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2968 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2969 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2971 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2972}
2973
2974void SensorInputMapperTest::setAccelProperties() {
2975 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2976 /* sensorDataIndex */ 0);
2977 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2978 /* sensorDataIndex */ 1);
2979 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2980 /* sensorDataIndex */ 2);
2981 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2982 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2983 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2984 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2985 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2986}
2987
2988void SensorInputMapperTest::setGyroProperties() {
2989 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2990 /* sensorDataIndex */ 0);
2991 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2992 /* sensorDataIndex */ 1);
2993 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2994 /* sensorDataIndex */ 2);
2995 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2996 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2997 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2998 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2999 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3000}
3001
3002TEST_F(SensorInputMapperTest, GetSources) {
3003 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3004
3005 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3006}
3007
3008TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3009 setAccelProperties();
3010 prepareAccelAxes();
3011 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3012
3013 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3014 std::chrono::microseconds(10000),
3015 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003016 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003022
3023 NotifySensorArgs args;
3024 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3025 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3026 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3027
3028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3029 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3030 ASSERT_EQ(args.deviceId, DEVICE_ID);
3031 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3032 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3033 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3034 ASSERT_EQ(args.values, values);
3035 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3036}
3037
3038TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3039 setGyroProperties();
3040 prepareGyroAxes();
3041 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3042
3043 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3044 std::chrono::microseconds(10000),
3045 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003046 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003052
3053 NotifySensorArgs args;
3054 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3055 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3056 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3057
3058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3059 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3060 ASSERT_EQ(args.deviceId, DEVICE_ID);
3061 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3062 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3063 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3064 ASSERT_EQ(args.values, values);
3065 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3066}
3067
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068// --- KeyboardInputMapperTest ---
3069
3070class KeyboardInputMapperTest : public InputMapperTest {
3071protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003072 const std::string UNIQUE_ID = "local:0";
3073
3074 void prepareDisplay(int32_t orientation);
3075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003077 int32_t originalKeyCode, int32_t rotatedKeyCode,
3078 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079};
3080
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003081/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3082 * orientation.
3083 */
3084void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003085 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3086 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003087}
3088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003090 int32_t originalScanCode, int32_t originalKeyCode,
3091 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 NotifyKeyArgs args;
3093
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3096 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3097 ASSERT_EQ(originalScanCode, args.scanCode);
3098 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003099 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3103 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3104 ASSERT_EQ(originalScanCode, args.scanCode);
3105 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003106 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107}
3108
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003110 KeyboardInputMapper& mapper =
3111 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3112 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003114 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115}
3116
3117TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3118 const int32_t USAGE_A = 0x070004;
3119 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003120 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3121 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003122 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3123 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3124 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 KeyboardInputMapper& mapper =
3127 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3128 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003129 // Initial metastate to AMETA_NONE.
3130 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3131 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132
3133 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135 NotifyKeyArgs args;
3136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3137 ASSERT_EQ(DEVICE_ID, args.deviceId);
3138 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3139 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3140 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3141 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3142 ASSERT_EQ(KEY_HOME, args.scanCode);
3143 ASSERT_EQ(AMETA_NONE, args.metaState);
3144 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3145 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3146 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3147
3148 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003149 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3151 ASSERT_EQ(DEVICE_ID, args.deviceId);
3152 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3153 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3154 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3155 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3156 ASSERT_EQ(KEY_HOME, args.scanCode);
3157 ASSERT_EQ(AMETA_NONE, args.metaState);
3158 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3159 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3160 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3161
3162 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3166 ASSERT_EQ(DEVICE_ID, args.deviceId);
3167 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3168 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3169 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3170 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3171 ASSERT_EQ(0, args.scanCode);
3172 ASSERT_EQ(AMETA_NONE, args.metaState);
3173 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3174 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3175 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3176
3177 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3179 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3181 ASSERT_EQ(DEVICE_ID, args.deviceId);
3182 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3183 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3184 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3185 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3186 ASSERT_EQ(0, args.scanCode);
3187 ASSERT_EQ(AMETA_NONE, args.metaState);
3188 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3189 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3190 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3191
3192 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3196 ASSERT_EQ(DEVICE_ID, args.deviceId);
3197 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3198 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3199 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3200 ASSERT_EQ(0, args.keyCode);
3201 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3202 ASSERT_EQ(AMETA_NONE, args.metaState);
3203 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3204 ASSERT_EQ(0U, args.policyFlags);
3205 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3206
3207 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3209 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3211 ASSERT_EQ(DEVICE_ID, args.deviceId);
3212 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3213 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3214 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3215 ASSERT_EQ(0, args.keyCode);
3216 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3217 ASSERT_EQ(AMETA_NONE, args.metaState);
3218 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3219 ASSERT_EQ(0U, args.policyFlags);
3220 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3221}
3222
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003223/**
3224 * Ensure that the readTime is set to the time when the EV_KEY is received.
3225 */
3226TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3227 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3228
3229 KeyboardInputMapper& mapper =
3230 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3231 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3232 NotifyKeyArgs args;
3233
3234 // Key down
3235 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3237 ASSERT_EQ(12, args.readTime);
3238
3239 // Key up
3240 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3242 ASSERT_EQ(15, args.readTime);
3243}
3244
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003246 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3247 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003248 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3249 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3250 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 KeyboardInputMapper& mapper =
3253 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3254 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255
arthurhungc903df12020-08-11 15:08:42 +08003256 // Initial metastate to AMETA_NONE.
3257 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3258 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259
3260 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262 NotifyKeyArgs args;
3263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3264 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003266 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267
3268 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003269 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3271 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
3274 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003275 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3277 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279
3280 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003281 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3283 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003284 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003285 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286}
3287
3288TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003289 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3290 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3291 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3292 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003294 KeyboardInputMapper& mapper =
3295 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3296 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003298 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3300 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3301 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3302 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3303 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3304 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3305 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3306 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3307}
3308
3309TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003310 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3311 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3312 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3313 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003316 KeyboardInputMapper& mapper =
3317 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3318 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003320 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003321 ASSERT_NO_FATAL_FAILURE(
3322 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3323 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3324 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3325 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3326 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3327 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3328 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003330 clearViewports();
3331 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003332 ASSERT_NO_FATAL_FAILURE(
3333 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3334 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3335 AKEYCODE_DPAD_UP, DISPLAY_ID));
3336 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3337 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3338 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3339 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003341 clearViewports();
3342 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003343 ASSERT_NO_FATAL_FAILURE(
3344 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3345 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3346 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3347 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3348 AKEYCODE_DPAD_UP, DISPLAY_ID));
3349 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3350 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003352 clearViewports();
3353 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003354 ASSERT_NO_FATAL_FAILURE(
3355 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3356 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3357 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3358 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3359 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3360 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3361 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362
3363 // Special case: if orientation changes while key is down, we still emit the same keycode
3364 // in the key up as we did in the key down.
3365 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003366 clearViewports();
3367 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3370 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3371 ASSERT_EQ(KEY_UP, args.scanCode);
3372 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3373
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003374 clearViewports();
3375 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3378 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3379 ASSERT_EQ(KEY_UP, args.scanCode);
3380 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3381}
3382
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003383TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3384 // If the keyboard is not orientation aware,
3385 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003386 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003388 KeyboardInputMapper& mapper =
3389 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3390 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003391 NotifyKeyArgs args;
3392
3393 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3398 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3399
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003400 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3405 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3406}
3407
3408TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3409 // If the keyboard is orientation aware,
3410 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003411 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003412
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003413 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003414 KeyboardInputMapper& mapper =
3415 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3416 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003417 NotifyKeyArgs args;
3418
3419 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3420 // ^--- already checked by the previous test
3421
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003422 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003423 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3428 ASSERT_EQ(DISPLAY_ID, args.displayId);
3429
3430 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003431 clearViewports();
3432 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003433 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3438 ASSERT_EQ(newDisplayId, args.displayId);
3439}
3440
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003442 KeyboardInputMapper& mapper =
3443 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3444 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003446 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003447 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003449 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003450 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451}
3452
3453TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003454 KeyboardInputMapper& mapper =
3455 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3456 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003458 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003459 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003461 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003462 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463}
3464
3465TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003466 KeyboardInputMapper& mapper =
3467 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3468 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003470 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471
3472 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3473 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003474 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475 ASSERT_TRUE(flags[0]);
3476 ASSERT_FALSE(flags[1]);
3477}
3478
3479TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003480 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3481 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3482 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3483 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3484 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3485 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003487 KeyboardInputMapper& mapper =
3488 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3489 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003490 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003491 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3492 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493
3494 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003495 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3496 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3497 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498
3499 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003502 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3503 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3504 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003505 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506
3507 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003510 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3511 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3512 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003513 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
3515 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003518 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3519 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3520 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003521 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522
3523 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003526 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3527 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3528 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003529 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530
3531 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003534 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3535 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3536 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003537 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538
3539 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003542 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3543 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3544 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003545 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546}
3547
Chris Yea52ade12020-08-27 16:49:20 -07003548TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3549 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3550 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3551 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3552 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3553
3554 KeyboardInputMapper& mapper =
3555 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3556 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3557
3558 // Initial metastate should be AMETA_NONE as no meta keys added.
3559 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3560 // Meta state should be AMETA_NONE after reset
3561 mapper.reset(ARBITRARY_TIME);
3562 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3563 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3564 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3565 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3566
3567 NotifyKeyArgs args;
3568 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3571 ASSERT_EQ(AMETA_NONE, args.metaState);
3572 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3573 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3574 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3575
3576 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003577 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3579 ASSERT_EQ(AMETA_NONE, args.metaState);
3580 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3581 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3582 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3583}
3584
Arthur Hung2c9a3342019-07-23 14:18:59 +08003585TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3586 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003587 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3588 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3589 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3590 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003591
3592 // keyboard 2.
3593 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003594 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003595 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003596 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003597 std::shared_ptr<InputDevice> device2 =
3598 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3599 Flags<InputDeviceClass>(0));
3600
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003601 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3602 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3603 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3604 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003605
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003606 KeyboardInputMapper& mapper =
3607 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3608 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003609
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003610 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003611 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003612 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003613 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3614 device2->reset(ARBITRARY_TIME);
3615
3616 // Prepared displays and associated info.
3617 constexpr uint8_t hdmi1 = 0;
3618 constexpr uint8_t hdmi2 = 1;
3619 const std::string SECONDARY_UNIQUE_ID = "local:1";
3620
3621 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3622 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3623
3624 // No associated display viewport found, should disable the device.
3625 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3626 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3627 ASSERT_FALSE(device2->isEnabled());
3628
3629 // Prepare second display.
3630 constexpr int32_t newDisplayId = 2;
3631 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003632 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003633 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003634 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003635 // Default device will reconfigure above, need additional reconfiguration for another device.
3636 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3637 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3638
3639 // Device should be enabled after the associated display is found.
3640 ASSERT_TRUE(mDevice->isEnabled());
3641 ASSERT_TRUE(device2->isEnabled());
3642
3643 // Test pad key events
3644 ASSERT_NO_FATAL_FAILURE(
3645 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3646 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3647 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3648 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3649 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3650 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3651 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3652
3653 ASSERT_NO_FATAL_FAILURE(
3654 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3655 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3656 AKEYCODE_DPAD_RIGHT, newDisplayId));
3657 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3658 AKEYCODE_DPAD_DOWN, newDisplayId));
3659 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3660 AKEYCODE_DPAD_LEFT, newDisplayId));
3661}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662
arthurhungc903df12020-08-11 15:08:42 +08003663TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3664 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3665 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3666 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3667 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3668 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3669 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3670
3671 KeyboardInputMapper& mapper =
3672 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3673 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3674 // Initial metastate to AMETA_NONE.
3675 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3676 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3677
3678 // Initialization should have turned all of the lights off.
3679 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3680 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3681 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3682
3683 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003686 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3687 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3688
3689 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003692 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3693 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3694
3695 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003698 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3699 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3700
3701 mFakeEventHub->removeDevice(EVENTHUB_ID);
3702 mReader->loopOnce();
3703
3704 // keyboard 2 should default toggle keys.
3705 const std::string USB2 = "USB2";
3706 const std::string DEVICE_NAME2 = "KEYBOARD2";
3707 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3708 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3709 std::shared_ptr<InputDevice> device2 =
3710 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3711 Flags<InputDeviceClass>(0));
3712 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3713 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3714 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3715 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3716 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3717 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3718
arthurhung6fe95782020-10-05 22:41:16 +08003719 KeyboardInputMapper& mapper2 =
3720 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3721 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003722 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3723 device2->reset(ARBITRARY_TIME);
3724
3725 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3726 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3727 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003728 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3729 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003730}
3731
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003732// --- KeyboardInputMapperTest_ExternalDevice ---
3733
3734class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3735protected:
Chris Yea52ade12020-08-27 16:49:20 -07003736 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003737};
3738
3739TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003740 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3741 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003742
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003743 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3744 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3745 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3746 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003747
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003748 KeyboardInputMapper& mapper =
3749 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3750 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003751
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003753 NotifyKeyArgs args;
3754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3755 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3756
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003757 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3759 ASSERT_EQ(uint32_t(0), args.policyFlags);
3760
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3763 ASSERT_EQ(uint32_t(0), args.policyFlags);
3764
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003765 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3767 ASSERT_EQ(uint32_t(0), args.policyFlags);
3768
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3771 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3772
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003773 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3775 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3776}
3777
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003778TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003779 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003780
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003781 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3782 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3783 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003784
Powei Fengd041c5d2019-05-03 17:11:33 -07003785 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003786 KeyboardInputMapper& mapper =
3787 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3788 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003789
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003791 NotifyKeyArgs args;
3792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3793 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3794
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003795 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3797 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3798
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3801 ASSERT_EQ(uint32_t(0), args.policyFlags);
3802
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003803 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3805 ASSERT_EQ(uint32_t(0), args.policyFlags);
3806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3809 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3810
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003811 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3813 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3814}
3815
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816// --- CursorInputMapperTest ---
3817
3818class CursorInputMapperTest : public InputMapperTest {
3819protected:
3820 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3821
Michael Wright17db18e2020-06-26 20:51:44 +01003822 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823
Chris Yea52ade12020-08-27 16:49:20 -07003824 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003825 InputMapperTest::SetUp();
3826
Michael Wright17db18e2020-06-26 20:51:44 +01003827 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003828 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829 }
3830
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003831 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3832 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003833
3834 void prepareDisplay(int32_t orientation) {
3835 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003836 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003837 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3838 orientation, uniqueId, NO_PORT, viewportType);
3839 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003840
3841 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3842 float pressure) {
3843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3844 0.0f, 0.0f, 0.0f, EPSILON));
3845 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846};
3847
3848const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003850void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3851 int32_t originalY, int32_t rotatedX,
3852 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853 NotifyMotionArgs args;
3854
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003860 ASSERT_NO_FATAL_FAILURE(
3861 assertCursorPointerCoords(args.pointerCoords[0],
3862 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3863 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003864}
3865
3866TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003868 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003870 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871}
3872
3873TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003875 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003877 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878}
3879
3880TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003882 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883
3884 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886
3887 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003888 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3889 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3891 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3892
3893 // When the bounds are set, then there should be a valid motion range.
3894 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3895
3896 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003897 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898
3899 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3900 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3901 1, 800 - 1, 0.0f, 0.0f));
3902 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3903 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3904 2, 480 - 1, 0.0f, 0.0f));
3905 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3906 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3907 0.0f, 1.0f, 0.0f, 0.0f));
3908}
3909
3910TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003912 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913
3914 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003915 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916
3917 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3918 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3919 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3920 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3921 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3922 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3923 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3924 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3925 0.0f, 1.0f, 0.0f, 0.0f));
3926}
3927
3928TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003930 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931
arthurhungdcef2dc2020-08-11 14:47:50 +08003932 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933
3934 NotifyMotionArgs args;
3935
3936 // Button press.
3937 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003938 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3941 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3942 ASSERT_EQ(DEVICE_ID, args.deviceId);
3943 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3944 ASSERT_EQ(uint32_t(0), args.policyFlags);
3945 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3946 ASSERT_EQ(0, args.flags);
3947 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3948 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3949 ASSERT_EQ(0, args.edgeFlags);
3950 ASSERT_EQ(uint32_t(1), args.pointerCount);
3951 ASSERT_EQ(0, args.pointerProperties[0].id);
3952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003953 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3955 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3956 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3957
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3959 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3960 ASSERT_EQ(DEVICE_ID, args.deviceId);
3961 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3962 ASSERT_EQ(uint32_t(0), args.policyFlags);
3963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3964 ASSERT_EQ(0, args.flags);
3965 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3966 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3967 ASSERT_EQ(0, args.edgeFlags);
3968 ASSERT_EQ(uint32_t(1), args.pointerCount);
3969 ASSERT_EQ(0, args.pointerProperties[0].id);
3970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003971 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003972 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3973 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3974 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3975
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003977 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3978 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3980 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3981 ASSERT_EQ(DEVICE_ID, args.deviceId);
3982 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3983 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3985 ASSERT_EQ(0, args.flags);
3986 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3987 ASSERT_EQ(0, args.buttonState);
3988 ASSERT_EQ(0, args.edgeFlags);
3989 ASSERT_EQ(uint32_t(1), args.pointerCount);
3990 ASSERT_EQ(0, args.pointerProperties[0].id);
3991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003992 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003993 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3994 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3995 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3996
3997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3998 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3999 ASSERT_EQ(DEVICE_ID, args.deviceId);
4000 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4001 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4003 ASSERT_EQ(0, args.flags);
4004 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4005 ASSERT_EQ(0, args.buttonState);
4006 ASSERT_EQ(0, args.edgeFlags);
4007 ASSERT_EQ(uint32_t(1), args.pointerCount);
4008 ASSERT_EQ(0, args.pointerProperties[0].id);
4009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004010 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4012 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4013 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4014}
4015
4016TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004018 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019
4020 NotifyMotionArgs args;
4021
4022 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004027 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4028 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4029 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030
4031 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004036 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4037 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038}
4039
4040TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004042 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043
4044 NotifyMotionArgs args;
4045
4046 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4050 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004051 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004055 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004056
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004062 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004063
4064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004066 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067}
4068
4069TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004071 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072
4073 NotifyMotionArgs args;
4074
4075 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4081 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004082 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4083 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4084 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4087 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004088 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4089 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4090 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004091
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004098 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4099 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4100 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101
4102 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004107 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004108
4109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004111 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112}
4113
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004114TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004116 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4117 // need to be rotated.
4118 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004119 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004121 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4123 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4130}
4131
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004132TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004134 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4135 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004136 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004138 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4147
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004148 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4152 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4153 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004158 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4167
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004168 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177}
4178
4179TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004181 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182
4183 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4184 mFakePointerController->setPosition(100, 200);
4185 mFakePointerController->setButtonState(0);
4186
4187 NotifyMotionArgs motionArgs;
4188 NotifyKeyArgs keyArgs;
4189
4190 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4194 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4195 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4196 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004197 ASSERT_NO_FATAL_FAILURE(
4198 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4202 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4203 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004204 ASSERT_NO_FATAL_FAILURE(
4205 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004206
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211 ASSERT_EQ(0, motionArgs.buttonState);
4212 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004213 ASSERT_NO_FATAL_FAILURE(
4214 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215
4216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004217 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 ASSERT_EQ(0, motionArgs.buttonState);
4219 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004220 ASSERT_NO_FATAL_FAILURE(
4221 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004222
4223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004225 ASSERT_EQ(0, motionArgs.buttonState);
4226 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004227 ASSERT_NO_FATAL_FAILURE(
4228 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229
4230 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4235 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4236 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4237 motionArgs.buttonState);
4238 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4239 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004240 ASSERT_NO_FATAL_FAILURE(
4241 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4245 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4246 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4247 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004248 ASSERT_NO_FATAL_FAILURE(
4249 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004250
4251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4253 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4254 motionArgs.buttonState);
4255 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4256 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004257 ASSERT_NO_FATAL_FAILURE(
4258 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004263 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4265 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004266 ASSERT_NO_FATAL_FAILURE(
4267 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004268
4269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004271 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4272 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004273 ASSERT_NO_FATAL_FAILURE(
4274 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4280 ASSERT_EQ(0, motionArgs.buttonState);
4281 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004282 ASSERT_NO_FATAL_FAILURE(
4283 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286
4287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288 ASSERT_EQ(0, motionArgs.buttonState);
4289 ASSERT_EQ(0, mFakePointerController->getButtonState());
4290 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004291 ASSERT_NO_FATAL_FAILURE(
4292 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004293
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4295 ASSERT_EQ(0, motionArgs.buttonState);
4296 ASSERT_EQ(0, mFakePointerController->getButtonState());
4297 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004298 ASSERT_NO_FATAL_FAILURE(
4299 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300
4301 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4305 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4306 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004307
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4311 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004312 ASSERT_NO_FATAL_FAILURE(
4313 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004314
4315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4317 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4318 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004319 ASSERT_NO_FATAL_FAILURE(
4320 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 ASSERT_EQ(0, motionArgs.buttonState);
4327 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004328 ASSERT_NO_FATAL_FAILURE(
4329 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004330
4331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333 ASSERT_EQ(0, motionArgs.buttonState);
4334 ASSERT_EQ(0, mFakePointerController->getButtonState());
4335
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004336 ASSERT_NO_FATAL_FAILURE(
4337 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4339 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4340 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4341
4342 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4346 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4347 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4352 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004353 ASSERT_NO_FATAL_FAILURE(
4354 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4358 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004360 ASSERT_NO_FATAL_FAILURE(
4361 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 ASSERT_EQ(0, motionArgs.buttonState);
4368 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004369 ASSERT_NO_FATAL_FAILURE(
4370 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004371
4372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4373 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4374 ASSERT_EQ(0, motionArgs.buttonState);
4375 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(
4377 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4380 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4381 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4382
4383 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4387 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4388 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4393 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004394 ASSERT_NO_FATAL_FAILURE(
4395 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004396
4397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4399 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4400 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004401 ASSERT_NO_FATAL_FAILURE(
4402 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 ASSERT_EQ(0, motionArgs.buttonState);
4409 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004410 ASSERT_NO_FATAL_FAILURE(
4411 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412
4413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4415 ASSERT_EQ(0, motionArgs.buttonState);
4416 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004417 ASSERT_NO_FATAL_FAILURE(
4418 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004419
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4421 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4422 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4423
4424 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4428 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4429 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004430
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4434 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004435 ASSERT_NO_FATAL_FAILURE(
4436 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004437
4438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4440 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4441 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004442 ASSERT_NO_FATAL_FAILURE(
4443 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449 ASSERT_EQ(0, motionArgs.buttonState);
4450 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004451 ASSERT_NO_FATAL_FAILURE(
4452 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453
4454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4455 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4456 ASSERT_EQ(0, motionArgs.buttonState);
4457 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004458 ASSERT_NO_FATAL_FAILURE(
4459 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004460
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4462 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4463 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4464}
4465
4466TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004468 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469
4470 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4471 mFakePointerController->setPosition(100, 200);
4472 mFakePointerController->setButtonState(0);
4473
4474 NotifyMotionArgs args;
4475
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004480 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4481 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4483 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004484 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004485}
4486
4487TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004488 addConfigurationProperty("cursor.mode", "pointer");
4489 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004490 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004491
4492 NotifyDeviceResetArgs resetArgs;
4493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4494 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4495 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4496
4497 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4498 mFakePointerController->setPosition(100, 200);
4499 mFakePointerController->setButtonState(0);
4500
4501 NotifyMotionArgs args;
4502
4503 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4508 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4511 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004512 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004513
4514 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004515 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4518 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4519 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4521 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4523 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4526 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4527
4528 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004529 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4530 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4532 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4533 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4535 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4537 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4538 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4540 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4541
4542 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4547 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4550 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004551 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004552
4553 // Disable pointer capture and check that the device generation got bumped
4554 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004555 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004556 mFakePolicy->setPointerCapture(false);
4557 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004558 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004559
4560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4561 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4562 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4563
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4568 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4571 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004572 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573}
4574
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004575TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004576 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004577
Garfield Tan888a6a42020-01-09 11:39:16 -08004578 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004579 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004580 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4581 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004582 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4583 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004584 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4585 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4586
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004587 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4588 mFakePointerController->setPosition(100, 200);
4589 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004590
4591 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4596 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4597 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4599 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004600 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004601 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4602}
4603
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604// --- TouchInputMapperTest ---
4605
4606class TouchInputMapperTest : public InputMapperTest {
4607protected:
4608 static const int32_t RAW_X_MIN;
4609 static const int32_t RAW_X_MAX;
4610 static const int32_t RAW_Y_MIN;
4611 static const int32_t RAW_Y_MAX;
4612 static const int32_t RAW_TOUCH_MIN;
4613 static const int32_t RAW_TOUCH_MAX;
4614 static const int32_t RAW_TOOL_MIN;
4615 static const int32_t RAW_TOOL_MAX;
4616 static const int32_t RAW_PRESSURE_MIN;
4617 static const int32_t RAW_PRESSURE_MAX;
4618 static const int32_t RAW_ORIENTATION_MIN;
4619 static const int32_t RAW_ORIENTATION_MAX;
4620 static const int32_t RAW_DISTANCE_MIN;
4621 static const int32_t RAW_DISTANCE_MAX;
4622 static const int32_t RAW_TILT_MIN;
4623 static const int32_t RAW_TILT_MAX;
4624 static const int32_t RAW_ID_MIN;
4625 static const int32_t RAW_ID_MAX;
4626 static const int32_t RAW_SLOT_MIN;
4627 static const int32_t RAW_SLOT_MAX;
4628 static const float X_PRECISION;
4629 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004630 static const float X_PRECISION_VIRTUAL;
4631 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632
4633 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004634 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635
4636 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4637
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004638 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004639 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004640
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 enum Axes {
4642 POSITION = 1 << 0,
4643 TOUCH = 1 << 1,
4644 TOOL = 1 << 2,
4645 PRESSURE = 1 << 3,
4646 ORIENTATION = 1 << 4,
4647 MINOR = 1 << 5,
4648 ID = 1 << 6,
4649 DISTANCE = 1 << 7,
4650 TILT = 1 << 8,
4651 SLOT = 1 << 9,
4652 TOOL_TYPE = 1 << 10,
4653 };
4654
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004655 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4656 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004657 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004659 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 int32_t toRawX(float displayX);
4661 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004662 int32_t toRotatedRawX(float displayX);
4663 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004664 float toCookedX(float rawX, float rawY);
4665 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004667 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004669 float toDisplayY(int32_t rawY, int32_t displayHeight);
4670
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671};
4672
4673const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4674const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4675const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4676const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4677const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4678const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4679const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4680const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004681const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4682const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4684const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4685const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4686const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4687const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4688const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4689const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4690const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4691const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4692const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4693const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4694const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004695const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4696 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4697const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4698 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004699const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4700 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701
4702const float TouchInputMapperTest::GEOMETRIC_SCALE =
4703 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4704 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4705
4706const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4707 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4708 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4709};
4710
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004711void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004712 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4713 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004714}
4715
4716void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4717 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4718 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719}
4720
Santos Cordonfa5cf462017-04-05 10:37:00 -07004721void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004722 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4723 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4724 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004725}
4726
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004728 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4729 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4730 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4731 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732}
4733
Jason Gerecke489fda82012-09-07 17:19:40 -07004734void TouchInputMapperTest::prepareLocationCalibration() {
4735 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4736}
4737
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738int32_t TouchInputMapperTest::toRawX(float displayX) {
4739 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4740}
4741
4742int32_t TouchInputMapperTest::toRawY(float displayY) {
4743 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4744}
4745
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004746int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4747 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4748}
4749
4750int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4751 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4752}
4753
Jason Gerecke489fda82012-09-07 17:19:40 -07004754float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4755 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4756 return rawX;
4757}
4758
4759float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4760 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4761 return rawY;
4762}
4763
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004765 return toDisplayX(rawX, DISPLAY_WIDTH);
4766}
4767
4768float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4769 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770}
4771
4772float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004773 return toDisplayY(rawY, DISPLAY_HEIGHT);
4774}
4775
4776float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4777 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778}
4779
4780
4781// --- SingleTouchInputMapperTest ---
4782
4783class SingleTouchInputMapperTest : public TouchInputMapperTest {
4784protected:
4785 void prepareButtons();
4786 void prepareAxes(int axes);
4787
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004788 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4789 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4790 void processUp(SingleTouchInputMapper& mappery);
4791 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4792 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4793 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4794 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4795 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4796 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797};
4798
4799void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004800 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801}
4802
4803void SingleTouchInputMapperTest::prepareAxes(int axes) {
4804 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004805 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4806 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807 }
4808 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004809 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4810 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811 }
4812 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004813 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4814 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 }
4816 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004817 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4818 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 }
4820 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004821 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4822 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 }
4824}
4825
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004826void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830}
4831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004832void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835}
4836
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004837void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839}
4840
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004841void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843}
4844
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004845void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4846 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848}
4849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004850void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852}
4853
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004854void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4855 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858}
4859
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004860void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4861 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863}
4864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004865void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867}
4868
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870 prepareButtons();
4871 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004872 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004874 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875}
4876
4877TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004878 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4879 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880 prepareButtons();
4881 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004882 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004884 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885}
4886
4887TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888 prepareButtons();
4889 prepareAxes(POSITION);
4890 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004891 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004893 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894}
4895
4896TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897 prepareButtons();
4898 prepareAxes(POSITION);
4899 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004900 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004902 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903}
4904
4905TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906 addConfigurationProperty("touch.deviceType", "touchScreen");
4907 prepareDisplay(DISPLAY_ORIENTATION_0);
4908 prepareButtons();
4909 prepareAxes(POSITION);
4910 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004911 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912
4913 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004914 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915
4916 // Virtual key is down.
4917 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4918 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4919 processDown(mapper, x, y);
4920 processSync(mapper);
4921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4922
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004923 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924
4925 // Virtual key is up.
4926 processUp(mapper);
4927 processSync(mapper);
4928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4929
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004930 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931}
4932
4933TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934 addConfigurationProperty("touch.deviceType", "touchScreen");
4935 prepareDisplay(DISPLAY_ORIENTATION_0);
4936 prepareButtons();
4937 prepareAxes(POSITION);
4938 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004939 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940
4941 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004942 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943
4944 // Virtual key is down.
4945 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4946 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4947 processDown(mapper, x, y);
4948 processSync(mapper);
4949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4950
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004951 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952
4953 // Virtual key is up.
4954 processUp(mapper);
4955 processSync(mapper);
4956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4957
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004958 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959}
4960
4961TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962 addConfigurationProperty("touch.deviceType", "touchScreen");
4963 prepareDisplay(DISPLAY_ORIENTATION_0);
4964 prepareButtons();
4965 prepareAxes(POSITION);
4966 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004967 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968
4969 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4970 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004971 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972 ASSERT_TRUE(flags[0]);
4973 ASSERT_FALSE(flags[1]);
4974}
4975
4976TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977 addConfigurationProperty("touch.deviceType", "touchScreen");
4978 prepareDisplay(DISPLAY_ORIENTATION_0);
4979 prepareButtons();
4980 prepareAxes(POSITION);
4981 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004982 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983
arthurhungdcef2dc2020-08-11 14:47:50 +08004984 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985
4986 NotifyKeyArgs args;
4987
4988 // Press virtual key.
4989 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4990 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4991 processDown(mapper, x, y);
4992 processSync(mapper);
4993
4994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4995 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4996 ASSERT_EQ(DEVICE_ID, args.deviceId);
4997 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4998 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4999 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5000 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5001 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5002 ASSERT_EQ(KEY_HOME, args.scanCode);
5003 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5004 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5005
5006 // Release virtual key.
5007 processUp(mapper);
5008 processSync(mapper);
5009
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5011 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5012 ASSERT_EQ(DEVICE_ID, args.deviceId);
5013 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5014 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5015 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5016 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5017 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5018 ASSERT_EQ(KEY_HOME, args.scanCode);
5019 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5020 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5021
5022 // Should not have sent any motions.
5023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5024}
5025
5026TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027 addConfigurationProperty("touch.deviceType", "touchScreen");
5028 prepareDisplay(DISPLAY_ORIENTATION_0);
5029 prepareButtons();
5030 prepareAxes(POSITION);
5031 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005032 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033
arthurhungdcef2dc2020-08-11 14:47:50 +08005034 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035
5036 NotifyKeyArgs keyArgs;
5037
5038 // Press virtual key.
5039 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5040 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5041 processDown(mapper, x, y);
5042 processSync(mapper);
5043
5044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5045 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5046 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5047 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5048 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5049 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5050 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5051 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5052 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5053 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5054 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5055
5056 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5057 // into the display area.
5058 y -= 100;
5059 processMove(mapper, x, y);
5060 processSync(mapper);
5061
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5063 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5064 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5065 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5066 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5067 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5068 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5069 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5070 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5071 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5072 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5073 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5074
5075 NotifyMotionArgs motionArgs;
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5077 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5078 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5079 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5080 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5081 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5082 ASSERT_EQ(0, motionArgs.flags);
5083 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5084 ASSERT_EQ(0, motionArgs.buttonState);
5085 ASSERT_EQ(0, motionArgs.edgeFlags);
5086 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5087 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5088 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5090 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5091 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5092 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5093 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5094
5095 // Keep moving out of bounds. Should generate a pointer move.
5096 y -= 50;
5097 processMove(mapper, x, y);
5098 processSync(mapper);
5099
5100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5101 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5102 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5103 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5104 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5106 ASSERT_EQ(0, motionArgs.flags);
5107 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5108 ASSERT_EQ(0, motionArgs.buttonState);
5109 ASSERT_EQ(0, motionArgs.edgeFlags);
5110 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5111 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5114 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5115 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5116 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5117 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5118
5119 // Release out of bounds. Should generate a pointer up.
5120 processUp(mapper);
5121 processSync(mapper);
5122
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5124 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5125 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5126 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5127 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5128 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5129 ASSERT_EQ(0, motionArgs.flags);
5130 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5131 ASSERT_EQ(0, motionArgs.buttonState);
5132 ASSERT_EQ(0, motionArgs.edgeFlags);
5133 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5134 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5137 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5138 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5139 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5140 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5141
5142 // Should not have sent any more keys or motions.
5143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5145}
5146
5147TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148 addConfigurationProperty("touch.deviceType", "touchScreen");
5149 prepareDisplay(DISPLAY_ORIENTATION_0);
5150 prepareButtons();
5151 prepareAxes(POSITION);
5152 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005153 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005154
arthurhungdcef2dc2020-08-11 14:47:50 +08005155 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156
5157 NotifyMotionArgs motionArgs;
5158
5159 // Initially go down out of bounds.
5160 int32_t x = -10;
5161 int32_t y = -10;
5162 processDown(mapper, x, y);
5163 processSync(mapper);
5164
5165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5166
5167 // Move into the display area. Should generate a pointer down.
5168 x = 50;
5169 y = 75;
5170 processMove(mapper, x, y);
5171 processSync(mapper);
5172
5173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5174 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5175 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5176 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5177 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5178 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5179 ASSERT_EQ(0, motionArgs.flags);
5180 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5181 ASSERT_EQ(0, motionArgs.buttonState);
5182 ASSERT_EQ(0, motionArgs.edgeFlags);
5183 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5184 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5187 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5188 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5189 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5190 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5191
5192 // Release. Should generate a pointer up.
5193 processUp(mapper);
5194 processSync(mapper);
5195
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5197 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5198 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5199 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5200 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5201 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5202 ASSERT_EQ(0, motionArgs.flags);
5203 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5204 ASSERT_EQ(0, motionArgs.buttonState);
5205 ASSERT_EQ(0, motionArgs.edgeFlags);
5206 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5207 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5210 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5211 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5212 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5214
5215 // Should not have sent any more keys or motions.
5216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5218}
5219
Santos Cordonfa5cf462017-04-05 10:37:00 -07005220TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005221 addConfigurationProperty("touch.deviceType", "touchScreen");
5222 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5223
5224 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5225 prepareButtons();
5226 prepareAxes(POSITION);
5227 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005228 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005229
arthurhungdcef2dc2020-08-11 14:47:50 +08005230 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005231
5232 NotifyMotionArgs motionArgs;
5233
5234 // Down.
5235 int32_t x = 100;
5236 int32_t y = 125;
5237 processDown(mapper, x, y);
5238 processSync(mapper);
5239
5240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5241 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5242 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5243 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5244 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5245 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5247 ASSERT_EQ(0, motionArgs.flags);
5248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5249 ASSERT_EQ(0, motionArgs.buttonState);
5250 ASSERT_EQ(0, motionArgs.edgeFlags);
5251 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5252 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5255 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5256 1, 0, 0, 0, 0, 0, 0, 0));
5257 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5258 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5260
5261 // Move.
5262 x += 50;
5263 y += 75;
5264 processMove(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_MOVE, 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 // Up.
5289 processUp(mapper);
5290 processSync(mapper);
5291
5292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5295 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5296 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5297 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5298 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5299 ASSERT_EQ(0, motionArgs.flags);
5300 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5301 ASSERT_EQ(0, motionArgs.buttonState);
5302 ASSERT_EQ(0, motionArgs.edgeFlags);
5303 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5304 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5307 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5308 1, 0, 0, 0, 0, 0, 0, 0));
5309 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5310 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5312
5313 // Should not have sent any more keys or motions.
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5316}
5317
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319 addConfigurationProperty("touch.deviceType", "touchScreen");
5320 prepareDisplay(DISPLAY_ORIENTATION_0);
5321 prepareButtons();
5322 prepareAxes(POSITION);
5323 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005324 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325
arthurhungdcef2dc2020-08-11 14:47:50 +08005326 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327
5328 NotifyMotionArgs motionArgs;
5329
5330 // Down.
5331 int32_t x = 100;
5332 int32_t y = 125;
5333 processDown(mapper, x, y);
5334 processSync(mapper);
5335
5336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5338 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5339 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5340 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5341 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5342 ASSERT_EQ(0, motionArgs.flags);
5343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5344 ASSERT_EQ(0, motionArgs.buttonState);
5345 ASSERT_EQ(0, motionArgs.edgeFlags);
5346 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5347 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5350 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5351 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5352 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5354
5355 // Move.
5356 x += 50;
5357 y += 75;
5358 processMove(mapper, x, y);
5359 processSync(mapper);
5360
5361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5362 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5363 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5364 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5365 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5367 ASSERT_EQ(0, motionArgs.flags);
5368 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5369 ASSERT_EQ(0, motionArgs.buttonState);
5370 ASSERT_EQ(0, motionArgs.edgeFlags);
5371 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5372 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5375 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5376 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5377 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5379
5380 // Up.
5381 processUp(mapper);
5382 processSync(mapper);
5383
5384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5385 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5386 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5387 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5388 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5389 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5390 ASSERT_EQ(0, motionArgs.flags);
5391 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5392 ASSERT_EQ(0, motionArgs.buttonState);
5393 ASSERT_EQ(0, motionArgs.edgeFlags);
5394 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5395 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5398 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5399 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5400 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5401 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5402
5403 // Should not have sent any more keys or motions.
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5406}
5407
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005408TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409 addConfigurationProperty("touch.deviceType", "touchScreen");
5410 prepareButtons();
5411 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005412 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5413 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005414 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415
5416 NotifyMotionArgs args;
5417
5418 // Rotation 90.
5419 prepareDisplay(DISPLAY_ORIENTATION_90);
5420 processDown(mapper, toRawX(50), toRawY(75));
5421 processSync(mapper);
5422
5423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5424 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5425 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5426
5427 processUp(mapper);
5428 processSync(mapper);
5429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5430}
5431
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005432TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433 addConfigurationProperty("touch.deviceType", "touchScreen");
5434 prepareButtons();
5435 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005436 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5437 // orientation-aware are affected by display rotation.
5438 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005439 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440
5441 NotifyMotionArgs args;
5442
5443 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005444 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445 prepareDisplay(DISPLAY_ORIENTATION_0);
5446 processDown(mapper, toRawX(50), toRawY(75));
5447 processSync(mapper);
5448
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5450 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5451 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5452
5453 processUp(mapper);
5454 processSync(mapper);
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5456
5457 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005458 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005460 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461 processSync(mapper);
5462
5463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5464 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5465 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5466
5467 processUp(mapper);
5468 processSync(mapper);
5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5470
5471 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005472 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473 prepareDisplay(DISPLAY_ORIENTATION_180);
5474 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5475 processSync(mapper);
5476
5477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5478 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5479 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5480
5481 processUp(mapper);
5482 processSync(mapper);
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5484
5485 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005486 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005488 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489 processSync(mapper);
5490
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5492 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5493 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5494
5495 processUp(mapper);
5496 processSync(mapper);
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5498}
5499
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005500TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5501 addConfigurationProperty("touch.deviceType", "touchScreen");
5502 prepareButtons();
5503 prepareAxes(POSITION);
5504 addConfigurationProperty("touch.orientationAware", "1");
5505 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5506 clearViewports();
5507 prepareDisplay(DISPLAY_ORIENTATION_0);
5508 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5509 NotifyMotionArgs args;
5510
5511 // Orientation 0.
5512 processDown(mapper, toRawX(50), toRawY(75));
5513 processSync(mapper);
5514
5515 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5516 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5517 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5518
5519 processUp(mapper);
5520 processSync(mapper);
5521 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5522}
5523
5524TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5525 addConfigurationProperty("touch.deviceType", "touchScreen");
5526 prepareButtons();
5527 prepareAxes(POSITION);
5528 addConfigurationProperty("touch.orientationAware", "1");
5529 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5530 clearViewports();
5531 prepareDisplay(DISPLAY_ORIENTATION_0);
5532 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5533 NotifyMotionArgs args;
5534
5535 // Orientation 90.
5536 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5537 processSync(mapper);
5538
5539 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5540 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5541 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5542
5543 processUp(mapper);
5544 processSync(mapper);
5545 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5546}
5547
5548TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5549 addConfigurationProperty("touch.deviceType", "touchScreen");
5550 prepareButtons();
5551 prepareAxes(POSITION);
5552 addConfigurationProperty("touch.orientationAware", "1");
5553 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5554 clearViewports();
5555 prepareDisplay(DISPLAY_ORIENTATION_0);
5556 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5557 NotifyMotionArgs args;
5558
5559 // Orientation 180.
5560 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5561 processSync(mapper);
5562
5563 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5564 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5565 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5566
5567 processUp(mapper);
5568 processSync(mapper);
5569 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5570}
5571
5572TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5573 addConfigurationProperty("touch.deviceType", "touchScreen");
5574 prepareButtons();
5575 prepareAxes(POSITION);
5576 addConfigurationProperty("touch.orientationAware", "1");
5577 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5578 clearViewports();
5579 prepareDisplay(DISPLAY_ORIENTATION_0);
5580 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5581 NotifyMotionArgs args;
5582
5583 // Orientation 270.
5584 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5585 processSync(mapper);
5586
5587 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5588 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5589 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5590
5591 processUp(mapper);
5592 processSync(mapper);
5593 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5594}
5595
5596TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5597 addConfigurationProperty("touch.deviceType", "touchScreen");
5598 prepareButtons();
5599 prepareAxes(POSITION);
5600 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5601 // orientation-aware are affected by display rotation.
5602 addConfigurationProperty("touch.orientationAware", "0");
5603 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5604 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5605
5606 NotifyMotionArgs args;
5607
5608 // Orientation 90, Rotation 0.
5609 clearViewports();
5610 prepareDisplay(DISPLAY_ORIENTATION_0);
5611 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5612 processSync(mapper);
5613
5614 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5615 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5616 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5617
5618 processUp(mapper);
5619 processSync(mapper);
5620 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5621
5622 // Orientation 90, Rotation 90.
5623 clearViewports();
5624 prepareDisplay(DISPLAY_ORIENTATION_90);
5625 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5626 processSync(mapper);
5627
5628 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5629 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5630 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5631
5632 processUp(mapper);
5633 processSync(mapper);
5634 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5635
5636 // Orientation 90, Rotation 180.
5637 clearViewports();
5638 prepareDisplay(DISPLAY_ORIENTATION_180);
5639 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5640 processSync(mapper);
5641
5642 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5643 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5644 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5645
5646 processUp(mapper);
5647 processSync(mapper);
5648 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5649
5650 // Orientation 90, Rotation 270.
5651 clearViewports();
5652 prepareDisplay(DISPLAY_ORIENTATION_270);
5653 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5654 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5655 processSync(mapper);
5656
5657 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5658 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5659 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5660
5661 processUp(mapper);
5662 processSync(mapper);
5663 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5664}
5665
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667 addConfigurationProperty("touch.deviceType", "touchScreen");
5668 prepareDisplay(DISPLAY_ORIENTATION_0);
5669 prepareButtons();
5670 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005671 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005672
5673 // These calculations are based on the input device calibration documentation.
5674 int32_t rawX = 100;
5675 int32_t rawY = 200;
5676 int32_t rawPressure = 10;
5677 int32_t rawToolMajor = 12;
5678 int32_t rawDistance = 2;
5679 int32_t rawTiltX = 30;
5680 int32_t rawTiltY = 110;
5681
5682 float x = toDisplayX(rawX);
5683 float y = toDisplayY(rawY);
5684 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5685 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5686 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5687 float distance = float(rawDistance);
5688
5689 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5690 float tiltScale = M_PI / 180;
5691 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5692 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5693 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5694 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5695
5696 processDown(mapper, rawX, rawY);
5697 processPressure(mapper, rawPressure);
5698 processToolMajor(mapper, rawToolMajor);
5699 processDistance(mapper, rawDistance);
5700 processTilt(mapper, rawTiltX, rawTiltY);
5701 processSync(mapper);
5702
5703 NotifyMotionArgs args;
5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5706 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5707 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5708}
5709
Jason Gerecke489fda82012-09-07 17:19:40 -07005710TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005711 addConfigurationProperty("touch.deviceType", "touchScreen");
5712 prepareDisplay(DISPLAY_ORIENTATION_0);
5713 prepareLocationCalibration();
5714 prepareButtons();
5715 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005716 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005717
5718 int32_t rawX = 100;
5719 int32_t rawY = 200;
5720
5721 float x = toDisplayX(toCookedX(rawX, rawY));
5722 float y = toDisplayY(toCookedY(rawX, rawY));
5723
5724 processDown(mapper, rawX, rawY);
5725 processSync(mapper);
5726
5727 NotifyMotionArgs args;
5728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5730 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5731}
5732
Michael Wrightd02c5b62014-02-10 15:10:22 -08005733TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005734 addConfigurationProperty("touch.deviceType", "touchScreen");
5735 prepareDisplay(DISPLAY_ORIENTATION_0);
5736 prepareButtons();
5737 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005738 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005739
5740 NotifyMotionArgs motionArgs;
5741 NotifyKeyArgs keyArgs;
5742
5743 processDown(mapper, 100, 200);
5744 processSync(mapper);
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5746 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5747 ASSERT_EQ(0, motionArgs.buttonState);
5748
5749 // press BTN_LEFT, release BTN_LEFT
5750 processKey(mapper, BTN_LEFT, 1);
5751 processSync(mapper);
5752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5754 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5755
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5757 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5758 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5759
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760 processKey(mapper, BTN_LEFT, 0);
5761 processSync(mapper);
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005763 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005765
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005768 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769
5770 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5771 processKey(mapper, BTN_RIGHT, 1);
5772 processKey(mapper, BTN_MIDDLE, 1);
5773 processSync(mapper);
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5776 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5777 motionArgs.buttonState);
5778
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5780 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5781 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5782
5783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5784 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5785 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5786 motionArgs.buttonState);
5787
Michael Wrightd02c5b62014-02-10 15:10:22 -08005788 processKey(mapper, BTN_RIGHT, 0);
5789 processSync(mapper);
5790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005791 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005793
5794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005796 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005797
5798 processKey(mapper, BTN_MIDDLE, 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_BACK, release BTN_BACK
5809 processKey(mapper, BTN_BACK, 1);
5810 processSync(mapper);
5811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5812 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5813 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005814
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005817 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5818
5819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5820 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5821 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822
5823 processKey(mapper, BTN_BACK, 0);
5824 processSync(mapper);
5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005826 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005827 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005828
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005831 ASSERT_EQ(0, motionArgs.buttonState);
5832
Michael Wrightd02c5b62014-02-10 15:10:22 -08005833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5834 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5835 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5836
5837 // press BTN_SIDE, release BTN_SIDE
5838 processKey(mapper, BTN_SIDE, 1);
5839 processSync(mapper);
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5841 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5842 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005843
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005846 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5847
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5850 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851
5852 processKey(mapper, BTN_SIDE, 0);
5853 processSync(mapper);
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005855 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005857
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005860 ASSERT_EQ(0, motionArgs.buttonState);
5861
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5863 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5864 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5865
5866 // press BTN_FORWARD, release BTN_FORWARD
5867 processKey(mapper, BTN_FORWARD, 1);
5868 processSync(mapper);
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5870 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5871 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005872
Michael Wrightd02c5b62014-02-10 15:10:22 -08005873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005875 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5876
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5879 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005880
5881 processKey(mapper, BTN_FORWARD, 0);
5882 processSync(mapper);
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005884 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005886
5887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005889 ASSERT_EQ(0, motionArgs.buttonState);
5890
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5892 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5893 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5894
5895 // press BTN_EXTRA, release BTN_EXTRA
5896 processKey(mapper, BTN_EXTRA, 1);
5897 processSync(mapper);
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5899 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5900 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005901
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005904 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5905
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5907 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5908 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909
5910 processKey(mapper, BTN_EXTRA, 0);
5911 processSync(mapper);
5912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005915
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005918 ASSERT_EQ(0, motionArgs.buttonState);
5919
Michael Wrightd02c5b62014-02-10 15:10:22 -08005920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5921 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5922 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5923
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5925
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 // press BTN_STYLUS, release BTN_STYLUS
5927 processKey(mapper, BTN_STYLUS, 1);
5928 processSync(mapper);
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005931 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5932
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5935 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936
5937 processKey(mapper, BTN_STYLUS, 0);
5938 processSync(mapper);
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005942
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005945 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946
5947 // press BTN_STYLUS2, release BTN_STYLUS2
5948 processKey(mapper, BTN_STYLUS2, 1);
5949 processSync(mapper);
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005952 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5953
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5956 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957
5958 processKey(mapper, BTN_STYLUS2, 0);
5959 processSync(mapper);
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005962 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963
5964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005966 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967
5968 // release touch
5969 processUp(mapper);
5970 processSync(mapper);
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5972 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5973 ASSERT_EQ(0, motionArgs.buttonState);
5974}
5975
5976TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977 addConfigurationProperty("touch.deviceType", "touchScreen");
5978 prepareDisplay(DISPLAY_ORIENTATION_0);
5979 prepareButtons();
5980 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005981 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982
5983 NotifyMotionArgs motionArgs;
5984
5985 // default tool type is finger
5986 processDown(mapper, 100, 200);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5991
5992 // eraser
5993 processKey(mapper, BTN_TOOL_RUBBER, 1);
5994 processSync(mapper);
5995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5998
5999 // stylus
6000 processKey(mapper, BTN_TOOL_RUBBER, 0);
6001 processKey(mapper, BTN_TOOL_PEN, 1);
6002 processSync(mapper);
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6006
6007 // brush
6008 processKey(mapper, BTN_TOOL_PEN, 0);
6009 processKey(mapper, BTN_TOOL_BRUSH, 1);
6010 processSync(mapper);
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6014
6015 // pencil
6016 processKey(mapper, BTN_TOOL_BRUSH, 0);
6017 processKey(mapper, BTN_TOOL_PENCIL, 1);
6018 processSync(mapper);
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6021 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6022
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006023 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024 processKey(mapper, BTN_TOOL_PENCIL, 0);
6025 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6026 processSync(mapper);
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6030
6031 // mouse
6032 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6033 processKey(mapper, BTN_TOOL_MOUSE, 1);
6034 processSync(mapper);
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6038
6039 // lens
6040 processKey(mapper, BTN_TOOL_MOUSE, 0);
6041 processKey(mapper, BTN_TOOL_LENS, 1);
6042 processSync(mapper);
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6046
6047 // double-tap
6048 processKey(mapper, BTN_TOOL_LENS, 0);
6049 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6050 processSync(mapper);
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6054
6055 // triple-tap
6056 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6057 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6058 processSync(mapper);
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6062
6063 // quad-tap
6064 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6065 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6066 processSync(mapper);
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6070
6071 // finger
6072 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6073 processKey(mapper, BTN_TOOL_FINGER, 1);
6074 processSync(mapper);
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6078
6079 // stylus trumps finger
6080 processKey(mapper, BTN_TOOL_PEN, 1);
6081 processSync(mapper);
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6085
6086 // eraser trumps stylus
6087 processKey(mapper, BTN_TOOL_RUBBER, 1);
6088 processSync(mapper);
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6092
6093 // mouse trumps eraser
6094 processKey(mapper, BTN_TOOL_MOUSE, 1);
6095 processSync(mapper);
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6099
6100 // back to default tool type
6101 processKey(mapper, BTN_TOOL_MOUSE, 0);
6102 processKey(mapper, BTN_TOOL_RUBBER, 0);
6103 processKey(mapper, BTN_TOOL_PEN, 0);
6104 processKey(mapper, BTN_TOOL_FINGER, 0);
6105 processSync(mapper);
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6109}
6110
6111TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 addConfigurationProperty("touch.deviceType", "touchScreen");
6113 prepareDisplay(DISPLAY_ORIENTATION_0);
6114 prepareButtons();
6115 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006116 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006117 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118
6119 NotifyMotionArgs motionArgs;
6120
6121 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6122 processKey(mapper, BTN_TOOL_FINGER, 1);
6123 processMove(mapper, 100, 200);
6124 processSync(mapper);
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6126 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6128 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6129
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6131 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6132 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6133 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6134
6135 // move a little
6136 processMove(mapper, 150, 250);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6139 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6141 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6142
6143 // down when BTN_TOUCH is pressed, pressure defaults to 1
6144 processKey(mapper, BTN_TOUCH, 1);
6145 processSync(mapper);
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6147 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6149 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6150
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6154 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6155
6156 // up when BTN_TOUCH is released, hover restored
6157 processKey(mapper, BTN_TOUCH, 0);
6158 processSync(mapper);
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6160 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6162 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6163
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6167 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6168
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6170 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6172 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6173
6174 // exit hover when pointer goes away
6175 processKey(mapper, BTN_TOOL_FINGER, 0);
6176 processSync(mapper);
6177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6178 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6180 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6181}
6182
6183TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184 addConfigurationProperty("touch.deviceType", "touchScreen");
6185 prepareDisplay(DISPLAY_ORIENTATION_0);
6186 prepareButtons();
6187 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006188 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189
6190 NotifyMotionArgs motionArgs;
6191
6192 // initially hovering because pressure is 0
6193 processDown(mapper, 100, 200);
6194 processPressure(mapper, 0);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6199 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6200
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6202 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6204 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6205
6206 // move a little
6207 processMove(mapper, 150, 250);
6208 processSync(mapper);
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6210 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6212 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6213
6214 // down when pressure is non-zero
6215 processPressure(mapper, RAW_PRESSURE_MAX);
6216 processSync(mapper);
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6218 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6220 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6221
6222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6223 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6225 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6226
6227 // up when pressure becomes 0, hover restored
6228 processPressure(mapper, 0);
6229 processSync(mapper);
6230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6231 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6233 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6234
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6238 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6239
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6241 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6243 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6244
6245 // exit hover when pointer goes away
6246 processUp(mapper);
6247 processSync(mapper);
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6249 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6251 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6252}
6253
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254// --- MultiTouchInputMapperTest ---
6255
6256class MultiTouchInputMapperTest : public TouchInputMapperTest {
6257protected:
6258 void prepareAxes(int axes);
6259
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006260 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6261 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6262 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6263 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6264 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6265 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6266 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6267 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6268 void processId(MultiTouchInputMapper& mapper, int32_t id);
6269 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6270 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6271 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6272 void processMTSync(MultiTouchInputMapper& mapper);
6273 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274};
6275
6276void MultiTouchInputMapperTest::prepareAxes(int axes) {
6277 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006278 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6279 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280 }
6281 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006282 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6283 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006285 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6286 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287 }
6288 }
6289 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006290 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6291 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006292 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006293 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6294 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295 }
6296 }
6297 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006298 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6299 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300 }
6301 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006302 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6303 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 }
6305 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006306 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6307 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 }
6309 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006310 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6311 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312 }
6313 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006314 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6315 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 }
6317 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006318 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 }
6320}
6321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006322void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6323 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006326}
6327
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006328void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6329 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331}
6332
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006333void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6334 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336}
6337
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006338void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340}
6341
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006342void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344}
6345
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006346void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6347 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349}
6350
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006351void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353}
6354
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006355void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357}
6358
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006359void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361}
6362
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006363void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365}
6366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006367void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006369}
6370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006371void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6372 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374}
6375
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006376void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378}
6379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006380void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382}
6383
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 addConfigurationProperty("touch.deviceType", "touchScreen");
6386 prepareDisplay(DISPLAY_ORIENTATION_0);
6387 prepareAxes(POSITION);
6388 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006389 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390
arthurhungdcef2dc2020-08-11 14:47:50 +08006391 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392
6393 NotifyMotionArgs motionArgs;
6394
6395 // Two fingers down at once.
6396 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6397 processPosition(mapper, x1, y1);
6398 processMTSync(mapper);
6399 processPosition(mapper, x2, y2);
6400 processMTSync(mapper);
6401 processSync(mapper);
6402
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6405 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6406 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6407 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6409 ASSERT_EQ(0, motionArgs.flags);
6410 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6411 ASSERT_EQ(0, motionArgs.buttonState);
6412 ASSERT_EQ(0, motionArgs.edgeFlags);
6413 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6414 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6417 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6418 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6419 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6421
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6424 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6425 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6426 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6428 motionArgs.action);
6429 ASSERT_EQ(0, motionArgs.flags);
6430 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6431 ASSERT_EQ(0, motionArgs.buttonState);
6432 ASSERT_EQ(0, motionArgs.edgeFlags);
6433 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6434 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6436 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6439 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6441 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6442 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6443 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6444 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6445
6446 // Move.
6447 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6448 processPosition(mapper, x1, y1);
6449 processMTSync(mapper);
6450 processPosition(mapper, x2, y2);
6451 processMTSync(mapper);
6452 processSync(mapper);
6453
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6456 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6457 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6458 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6460 ASSERT_EQ(0, motionArgs.flags);
6461 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6462 ASSERT_EQ(0, motionArgs.buttonState);
6463 ASSERT_EQ(0, motionArgs.edgeFlags);
6464 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6465 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6467 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6470 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6472 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6473 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6474 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6476
6477 // First finger up.
6478 x2 += 15; y2 -= 20;
6479 processPosition(mapper, x2, y2);
6480 processMTSync(mapper);
6481 processSync(mapper);
6482
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6484 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6485 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6486 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6487 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6488 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6489 motionArgs.action);
6490 ASSERT_EQ(0, motionArgs.flags);
6491 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6492 ASSERT_EQ(0, motionArgs.buttonState);
6493 ASSERT_EQ(0, motionArgs.edgeFlags);
6494 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6495 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6497 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6500 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6502 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6503 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6504 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6505 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6506
6507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6508 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6509 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6510 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6511 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6513 ASSERT_EQ(0, motionArgs.flags);
6514 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6515 ASSERT_EQ(0, motionArgs.buttonState);
6516 ASSERT_EQ(0, motionArgs.edgeFlags);
6517 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6518 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6521 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6522 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6523 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6524 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6525
6526 // Move.
6527 x2 += 20; y2 -= 25;
6528 processPosition(mapper, x2, y2);
6529 processMTSync(mapper);
6530 processSync(mapper);
6531
6532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6533 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6534 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6535 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6536 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6538 ASSERT_EQ(0, motionArgs.flags);
6539 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6540 ASSERT_EQ(0, motionArgs.buttonState);
6541 ASSERT_EQ(0, motionArgs.edgeFlags);
6542 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6543 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6546 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6547 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6548 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6549 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6550
6551 // New finger down.
6552 int32_t x3 = 700, y3 = 300;
6553 processPosition(mapper, x2, y2);
6554 processMTSync(mapper);
6555 processPosition(mapper, x3, y3);
6556 processMTSync(mapper);
6557 processSync(mapper);
6558
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6560 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6561 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6562 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6563 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6564 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6565 motionArgs.action);
6566 ASSERT_EQ(0, motionArgs.flags);
6567 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6568 ASSERT_EQ(0, motionArgs.buttonState);
6569 ASSERT_EQ(0, motionArgs.edgeFlags);
6570 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6571 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6573 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6576 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6578 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6579 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6580 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6581 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6582
6583 // Second finger up.
6584 x3 += 30; y3 -= 20;
6585 processPosition(mapper, x3, y3);
6586 processMTSync(mapper);
6587 processSync(mapper);
6588
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6591 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6592 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6593 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6595 motionArgs.action);
6596 ASSERT_EQ(0, motionArgs.flags);
6597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6598 ASSERT_EQ(0, motionArgs.buttonState);
6599 ASSERT_EQ(0, motionArgs.edgeFlags);
6600 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6603 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6606 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6608 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6609 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6610 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6611 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6612
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6614 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6615 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6616 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6617 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6619 ASSERT_EQ(0, motionArgs.flags);
6620 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6621 ASSERT_EQ(0, motionArgs.buttonState);
6622 ASSERT_EQ(0, motionArgs.edgeFlags);
6623 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6624 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6627 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6628 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6629 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6630 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6631
6632 // Last finger up.
6633 processMTSync(mapper);
6634 processSync(mapper);
6635
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6637 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6638 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6639 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6640 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6641 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6642 ASSERT_EQ(0, motionArgs.flags);
6643 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6644 ASSERT_EQ(0, motionArgs.buttonState);
6645 ASSERT_EQ(0, motionArgs.edgeFlags);
6646 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6647 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6650 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6651 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6652 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6653 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6654
6655 // Should not have sent any more keys or motions.
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6658}
6659
6660TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006661 addConfigurationProperty("touch.deviceType", "touchScreen");
6662 prepareDisplay(DISPLAY_ORIENTATION_0);
6663 prepareAxes(POSITION | ID);
6664 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006665 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006666
arthurhungdcef2dc2020-08-11 14:47:50 +08006667 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006668
6669 NotifyMotionArgs motionArgs;
6670
6671 // Two fingers down at once.
6672 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6673 processPosition(mapper, x1, y1);
6674 processId(mapper, 1);
6675 processMTSync(mapper);
6676 processPosition(mapper, x2, y2);
6677 processId(mapper, 2);
6678 processMTSync(mapper);
6679 processSync(mapper);
6680
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6683 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6684 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6687 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6688
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6691 motionArgs.action);
6692 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6693 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6695 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6698 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6700 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6701
6702 // Move.
6703 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6704 processPosition(mapper, x1, y1);
6705 processId(mapper, 1);
6706 processMTSync(mapper);
6707 processPosition(mapper, x2, y2);
6708 processId(mapper, 2);
6709 processMTSync(mapper);
6710 processSync(mapper);
6711
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6714 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6715 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6717 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6720 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6722 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6723
6724 // First finger up.
6725 x2 += 15; y2 -= 20;
6726 processPosition(mapper, x2, y2);
6727 processId(mapper, 2);
6728 processMTSync(mapper);
6729 processSync(mapper);
6730
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6732 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6733 motionArgs.action);
6734 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6735 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6737 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6740 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6742 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6743
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6746 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6747 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6751
6752 // Move.
6753 x2 += 20; y2 -= 25;
6754 processPosition(mapper, x2, y2);
6755 processId(mapper, 2);
6756 processMTSync(mapper);
6757 processSync(mapper);
6758
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6761 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6762 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6765 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6766
6767 // New finger down.
6768 int32_t x3 = 700, y3 = 300;
6769 processPosition(mapper, x2, y2);
6770 processId(mapper, 2);
6771 processMTSync(mapper);
6772 processPosition(mapper, x3, y3);
6773 processId(mapper, 3);
6774 processMTSync(mapper);
6775 processSync(mapper);
6776
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6778 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6779 motionArgs.action);
6780 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6781 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6783 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6786 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6788 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6789
6790 // Second finger up.
6791 x3 += 30; y3 -= 20;
6792 processPosition(mapper, x3, y3);
6793 processId(mapper, 3);
6794 processMTSync(mapper);
6795 processSync(mapper);
6796
6797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6798 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6799 motionArgs.action);
6800 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6801 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6803 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6804 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6806 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6808 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6809
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6812 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6813 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6816 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6817
6818 // Last finger up.
6819 processMTSync(mapper);
6820 processSync(mapper);
6821
6822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6823 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6824 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6825 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6828 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6829
6830 // Should not have sent any more keys or motions.
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6833}
6834
6835TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006836 addConfigurationProperty("touch.deviceType", "touchScreen");
6837 prepareDisplay(DISPLAY_ORIENTATION_0);
6838 prepareAxes(POSITION | ID | SLOT);
6839 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006840 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006841
arthurhungdcef2dc2020-08-11 14:47:50 +08006842 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006843
6844 NotifyMotionArgs motionArgs;
6845
6846 // Two fingers down at once.
6847 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6848 processPosition(mapper, x1, y1);
6849 processId(mapper, 1);
6850 processSlot(mapper, 1);
6851 processPosition(mapper, x2, y2);
6852 processId(mapper, 2);
6853 processSync(mapper);
6854
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6856 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6857 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6858 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6861 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6862
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6864 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6865 motionArgs.action);
6866 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6867 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6869 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6870 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6872 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6874 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6875
6876 // Move.
6877 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6878 processSlot(mapper, 0);
6879 processPosition(mapper, x1, y1);
6880 processSlot(mapper, 1);
6881 processPosition(mapper, x2, y2);
6882 processSync(mapper);
6883
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6886 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6887 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6889 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6892 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6894 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6895
6896 // First finger up.
6897 x2 += 15; y2 -= 20;
6898 processSlot(mapper, 0);
6899 processId(mapper, -1);
6900 processSlot(mapper, 1);
6901 processPosition(mapper, x2, y2);
6902 processSync(mapper);
6903
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6905 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6906 motionArgs.action);
6907 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6908 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6910 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6913 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6915 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6916
6917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6919 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6920 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6921 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6923 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6924
6925 // Move.
6926 x2 += 20; y2 -= 25;
6927 processPosition(mapper, x2, y2);
6928 processSync(mapper);
6929
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6932 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6933 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6934 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6936 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6937
6938 // New finger down.
6939 int32_t x3 = 700, y3 = 300;
6940 processPosition(mapper, x2, y2);
6941 processSlot(mapper, 0);
6942 processId(mapper, 3);
6943 processPosition(mapper, x3, y3);
6944 processSync(mapper);
6945
6946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6947 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6948 motionArgs.action);
6949 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6950 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6951 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6952 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6955 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6957 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6958
6959 // Second finger up.
6960 x3 += 30; y3 -= 20;
6961 processSlot(mapper, 1);
6962 processId(mapper, -1);
6963 processSlot(mapper, 0);
6964 processPosition(mapper, x3, y3);
6965 processSync(mapper);
6966
6967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6968 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6969 motionArgs.action);
6970 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6971 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6972 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6973 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6976 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6978 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6979
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6982 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6983 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6986 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6987
6988 // Last finger up.
6989 processId(mapper, -1);
6990 processSync(mapper);
6991
6992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6993 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6994 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6995 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6996 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6998 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6999
7000 // Should not have sent any more keys or motions.
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7003}
7004
7005TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007006 addConfigurationProperty("touch.deviceType", "touchScreen");
7007 prepareDisplay(DISPLAY_ORIENTATION_0);
7008 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007009 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007010
7011 // These calculations are based on the input device calibration documentation.
7012 int32_t rawX = 100;
7013 int32_t rawY = 200;
7014 int32_t rawTouchMajor = 7;
7015 int32_t rawTouchMinor = 6;
7016 int32_t rawToolMajor = 9;
7017 int32_t rawToolMinor = 8;
7018 int32_t rawPressure = 11;
7019 int32_t rawDistance = 0;
7020 int32_t rawOrientation = 3;
7021 int32_t id = 5;
7022
7023 float x = toDisplayX(rawX);
7024 float y = toDisplayY(rawY);
7025 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7026 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7027 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7028 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7029 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7030 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7031 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7032 float distance = float(rawDistance);
7033
7034 processPosition(mapper, rawX, rawY);
7035 processTouchMajor(mapper, rawTouchMajor);
7036 processTouchMinor(mapper, rawTouchMinor);
7037 processToolMajor(mapper, rawToolMajor);
7038 processToolMinor(mapper, rawToolMinor);
7039 processPressure(mapper, rawPressure);
7040 processOrientation(mapper, rawOrientation);
7041 processDistance(mapper, rawDistance);
7042 processId(mapper, id);
7043 processMTSync(mapper);
7044 processSync(mapper);
7045
7046 NotifyMotionArgs args;
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7048 ASSERT_EQ(0, args.pointerProperties[0].id);
7049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7050 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7051 orientation, distance));
7052}
7053
7054TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007055 addConfigurationProperty("touch.deviceType", "touchScreen");
7056 prepareDisplay(DISPLAY_ORIENTATION_0);
7057 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7058 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007059 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007060
7061 // These calculations are based on the input device calibration documentation.
7062 int32_t rawX = 100;
7063 int32_t rawY = 200;
7064 int32_t rawTouchMajor = 140;
7065 int32_t rawTouchMinor = 120;
7066 int32_t rawToolMajor = 180;
7067 int32_t rawToolMinor = 160;
7068
7069 float x = toDisplayX(rawX);
7070 float y = toDisplayY(rawY);
7071 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7072 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7073 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7074 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7075 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7076
7077 processPosition(mapper, rawX, rawY);
7078 processTouchMajor(mapper, rawTouchMajor);
7079 processTouchMinor(mapper, rawTouchMinor);
7080 processToolMajor(mapper, rawToolMajor);
7081 processToolMinor(mapper, rawToolMinor);
7082 processMTSync(mapper);
7083 processSync(mapper);
7084
7085 NotifyMotionArgs args;
7086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7088 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7089}
7090
7091TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007092 addConfigurationProperty("touch.deviceType", "touchScreen");
7093 prepareDisplay(DISPLAY_ORIENTATION_0);
7094 prepareAxes(POSITION | TOUCH | TOOL);
7095 addConfigurationProperty("touch.size.calibration", "diameter");
7096 addConfigurationProperty("touch.size.scale", "10");
7097 addConfigurationProperty("touch.size.bias", "160");
7098 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007099 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100
7101 // These calculations are based on the input device calibration documentation.
7102 // Note: We only provide a single common touch/tool value because the device is assumed
7103 // not to emit separate values for each pointer (isSummed = 1).
7104 int32_t rawX = 100;
7105 int32_t rawY = 200;
7106 int32_t rawX2 = 150;
7107 int32_t rawY2 = 250;
7108 int32_t rawTouchMajor = 5;
7109 int32_t rawToolMajor = 8;
7110
7111 float x = toDisplayX(rawX);
7112 float y = toDisplayY(rawY);
7113 float x2 = toDisplayX(rawX2);
7114 float y2 = toDisplayY(rawY2);
7115 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7116 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7117 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7118
7119 processPosition(mapper, rawX, rawY);
7120 processTouchMajor(mapper, rawTouchMajor);
7121 processToolMajor(mapper, rawToolMajor);
7122 processMTSync(mapper);
7123 processPosition(mapper, rawX2, rawY2);
7124 processTouchMajor(mapper, rawTouchMajor);
7125 processToolMajor(mapper, rawToolMajor);
7126 processMTSync(mapper);
7127 processSync(mapper);
7128
7129 NotifyMotionArgs args;
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7131 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7132
7133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7134 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7135 args.action);
7136 ASSERT_EQ(size_t(2), args.pointerCount);
7137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7138 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7140 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7141}
7142
7143TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144 addConfigurationProperty("touch.deviceType", "touchScreen");
7145 prepareDisplay(DISPLAY_ORIENTATION_0);
7146 prepareAxes(POSITION | TOUCH | TOOL);
7147 addConfigurationProperty("touch.size.calibration", "area");
7148 addConfigurationProperty("touch.size.scale", "43");
7149 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007150 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151
7152 // These calculations are based on the input device calibration documentation.
7153 int32_t rawX = 100;
7154 int32_t rawY = 200;
7155 int32_t rawTouchMajor = 5;
7156 int32_t rawToolMajor = 8;
7157
7158 float x = toDisplayX(rawX);
7159 float y = toDisplayY(rawY);
7160 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7161 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7162 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7163
7164 processPosition(mapper, rawX, rawY);
7165 processTouchMajor(mapper, rawTouchMajor);
7166 processToolMajor(mapper, rawToolMajor);
7167 processMTSync(mapper);
7168 processSync(mapper);
7169
7170 NotifyMotionArgs args;
7171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7173 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7174}
7175
7176TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177 addConfigurationProperty("touch.deviceType", "touchScreen");
7178 prepareDisplay(DISPLAY_ORIENTATION_0);
7179 prepareAxes(POSITION | PRESSURE);
7180 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7181 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007182 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007183
Michael Wrightaa449c92017-12-13 21:21:43 +00007184 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007185 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007186 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7187 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7188 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7189
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190 // These calculations are based on the input device calibration documentation.
7191 int32_t rawX = 100;
7192 int32_t rawY = 200;
7193 int32_t rawPressure = 60;
7194
7195 float x = toDisplayX(rawX);
7196 float y = toDisplayY(rawY);
7197 float pressure = float(rawPressure) * 0.01f;
7198
7199 processPosition(mapper, rawX, rawY);
7200 processPressure(mapper, rawPressure);
7201 processMTSync(mapper);
7202 processSync(mapper);
7203
7204 NotifyMotionArgs args;
7205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7207 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7208}
7209
7210TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211 addConfigurationProperty("touch.deviceType", "touchScreen");
7212 prepareDisplay(DISPLAY_ORIENTATION_0);
7213 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007214 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215
7216 NotifyMotionArgs motionArgs;
7217 NotifyKeyArgs keyArgs;
7218
7219 processId(mapper, 1);
7220 processPosition(mapper, 100, 200);
7221 processSync(mapper);
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7223 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7224 ASSERT_EQ(0, motionArgs.buttonState);
7225
7226 // press BTN_LEFT, release BTN_LEFT
7227 processKey(mapper, BTN_LEFT, 1);
7228 processSync(mapper);
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7231 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7232
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7234 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7235 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7236
Michael Wrightd02c5b62014-02-10 15:10:22 -08007237 processKey(mapper, BTN_LEFT, 0);
7238 processSync(mapper);
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007242
7243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007245 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246
7247 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7248 processKey(mapper, BTN_RIGHT, 1);
7249 processKey(mapper, BTN_MIDDLE, 1);
7250 processSync(mapper);
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7253 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7254 motionArgs.buttonState);
7255
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7257 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7258 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7259
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7262 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7263 motionArgs.buttonState);
7264
Michael Wrightd02c5b62014-02-10 15:10:22 -08007265 processKey(mapper, BTN_RIGHT, 0);
7266 processSync(mapper);
7267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007269 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007270
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007273 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007274
7275 processKey(mapper, BTN_MIDDLE, 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_BACK, release BTN_BACK
7286 processKey(mapper, BTN_BACK, 1);
7287 processSync(mapper);
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7289 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7290 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007291
Michael Wrightd02c5b62014-02-10 15:10:22 -08007292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007294 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7295
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7298 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007299
7300 processKey(mapper, BTN_BACK, 0);
7301 processSync(mapper);
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007304 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007305
7306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007308 ASSERT_EQ(0, motionArgs.buttonState);
7309
Michael Wrightd02c5b62014-02-10 15:10:22 -08007310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7311 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7312 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7313
7314 // press BTN_SIDE, release BTN_SIDE
7315 processKey(mapper, BTN_SIDE, 1);
7316 processSync(mapper);
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7318 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7319 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007320
Michael Wrightd02c5b62014-02-10 15:10:22 -08007321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007323 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7324
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7326 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7327 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007328
7329 processKey(mapper, BTN_SIDE, 0);
7330 processSync(mapper);
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007332 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007333 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007334
7335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007337 ASSERT_EQ(0, motionArgs.buttonState);
7338
Michael Wrightd02c5b62014-02-10 15:10:22 -08007339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7340 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7341 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7342
7343 // press BTN_FORWARD, release BTN_FORWARD
7344 processKey(mapper, BTN_FORWARD, 1);
7345 processSync(mapper);
7346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7347 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7348 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007349
Michael Wrightd02c5b62014-02-10 15:10:22 -08007350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007352 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7353
7354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7355 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7356 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007357
7358 processKey(mapper, BTN_FORWARD, 0);
7359 processSync(mapper);
7360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007361 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007362 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007363
7364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007366 ASSERT_EQ(0, motionArgs.buttonState);
7367
Michael Wrightd02c5b62014-02-10 15:10:22 -08007368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7369 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7370 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7371
7372 // press BTN_EXTRA, release BTN_EXTRA
7373 processKey(mapper, BTN_EXTRA, 1);
7374 processSync(mapper);
7375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7376 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7377 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007378
Michael Wrightd02c5b62014-02-10 15:10:22 -08007379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007381 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7382
7383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7384 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7385 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007386
7387 processKey(mapper, BTN_EXTRA, 0);
7388 processSync(mapper);
7389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007390 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007391 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007392
7393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007395 ASSERT_EQ(0, motionArgs.buttonState);
7396
Michael Wrightd02c5b62014-02-10 15:10:22 -08007397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7398 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7399 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7400
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7402
Michael Wrightd02c5b62014-02-10 15:10:22 -08007403 // press BTN_STYLUS, release BTN_STYLUS
7404 processKey(mapper, BTN_STYLUS, 1);
7405 processSync(mapper);
7406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007408 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7409
7410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7412 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007413
7414 processKey(mapper, BTN_STYLUS, 0);
7415 processSync(mapper);
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007417 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007418 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007419
7420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007422 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007423
7424 // press BTN_STYLUS2, release BTN_STYLUS2
7425 processKey(mapper, BTN_STYLUS2, 1);
7426 processSync(mapper);
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007429 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7430
7431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7433 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007434
7435 processKey(mapper, BTN_STYLUS2, 0);
7436 processSync(mapper);
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007439 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007440
7441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007443 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007444
7445 // release touch
7446 processId(mapper, -1);
7447 processSync(mapper);
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7449 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7450 ASSERT_EQ(0, motionArgs.buttonState);
7451}
7452
7453TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007454 addConfigurationProperty("touch.deviceType", "touchScreen");
7455 prepareDisplay(DISPLAY_ORIENTATION_0);
7456 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007457 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007458
7459 NotifyMotionArgs motionArgs;
7460
7461 // default tool type is finger
7462 processId(mapper, 1);
7463 processPosition(mapper, 100, 200);
7464 processSync(mapper);
7465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7466 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7468
7469 // eraser
7470 processKey(mapper, BTN_TOOL_RUBBER, 1);
7471 processSync(mapper);
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7475
7476 // stylus
7477 processKey(mapper, BTN_TOOL_RUBBER, 0);
7478 processKey(mapper, BTN_TOOL_PEN, 1);
7479 processSync(mapper);
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7483
7484 // brush
7485 processKey(mapper, BTN_TOOL_PEN, 0);
7486 processKey(mapper, BTN_TOOL_BRUSH, 1);
7487 processSync(mapper);
7488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7491
7492 // pencil
7493 processKey(mapper, BTN_TOOL_BRUSH, 0);
7494 processKey(mapper, BTN_TOOL_PENCIL, 1);
7495 processSync(mapper);
7496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7499
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007500 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007501 processKey(mapper, BTN_TOOL_PENCIL, 0);
7502 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7503 processSync(mapper);
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7507
7508 // mouse
7509 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7510 processKey(mapper, BTN_TOOL_MOUSE, 1);
7511 processSync(mapper);
7512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7515
7516 // lens
7517 processKey(mapper, BTN_TOOL_MOUSE, 0);
7518 processKey(mapper, BTN_TOOL_LENS, 1);
7519 processSync(mapper);
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7523
7524 // double-tap
7525 processKey(mapper, BTN_TOOL_LENS, 0);
7526 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7527 processSync(mapper);
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7529 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7531
7532 // triple-tap
7533 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7534 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7535 processSync(mapper);
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7539
7540 // quad-tap
7541 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7542 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7543 processSync(mapper);
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7547
7548 // finger
7549 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7550 processKey(mapper, BTN_TOOL_FINGER, 1);
7551 processSync(mapper);
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7555
7556 // stylus trumps finger
7557 processKey(mapper, BTN_TOOL_PEN, 1);
7558 processSync(mapper);
7559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7562
7563 // eraser trumps stylus
7564 processKey(mapper, BTN_TOOL_RUBBER, 1);
7565 processSync(mapper);
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7569
7570 // mouse trumps eraser
7571 processKey(mapper, BTN_TOOL_MOUSE, 1);
7572 processSync(mapper);
7573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7576
7577 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7578 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7579 processSync(mapper);
7580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7583
7584 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7585 processToolType(mapper, MT_TOOL_PEN);
7586 processSync(mapper);
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7590
7591 // back to default tool type
7592 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7593 processKey(mapper, BTN_TOOL_MOUSE, 0);
7594 processKey(mapper, BTN_TOOL_RUBBER, 0);
7595 processKey(mapper, BTN_TOOL_PEN, 0);
7596 processKey(mapper, BTN_TOOL_FINGER, 0);
7597 processSync(mapper);
7598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7601}
7602
7603TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007604 addConfigurationProperty("touch.deviceType", "touchScreen");
7605 prepareDisplay(DISPLAY_ORIENTATION_0);
7606 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007607 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007608 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007609
7610 NotifyMotionArgs motionArgs;
7611
7612 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7613 processId(mapper, 1);
7614 processPosition(mapper, 100, 200);
7615 processSync(mapper);
7616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7617 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7619 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7620
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7622 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7624 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7625
7626 // move a little
7627 processPosition(mapper, 150, 250);
7628 processSync(mapper);
7629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7630 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7632 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7633
7634 // down when BTN_TOUCH is pressed, pressure defaults to 1
7635 processKey(mapper, BTN_TOUCH, 1);
7636 processSync(mapper);
7637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7638 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7640 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7641
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7643 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7645 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7646
7647 // up when BTN_TOUCH is released, hover restored
7648 processKey(mapper, BTN_TOUCH, 0);
7649 processSync(mapper);
7650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7651 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7653 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7654
7655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7656 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7658 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7659
7660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7663 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7664
7665 // exit hover when pointer goes away
7666 processId(mapper, -1);
7667 processSync(mapper);
7668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7671 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7672}
7673
7674TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675 addConfigurationProperty("touch.deviceType", "touchScreen");
7676 prepareDisplay(DISPLAY_ORIENTATION_0);
7677 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007678 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007679
7680 NotifyMotionArgs motionArgs;
7681
7682 // initially hovering because pressure is 0
7683 processId(mapper, 1);
7684 processPosition(mapper, 100, 200);
7685 processPressure(mapper, 0);
7686 processSync(mapper);
7687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7688 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7690 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7691
7692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7693 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7695 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7696
7697 // move a little
7698 processPosition(mapper, 150, 250);
7699 processSync(mapper);
7700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7703 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7704
7705 // down when pressure becomes non-zero
7706 processPressure(mapper, RAW_PRESSURE_MAX);
7707 processSync(mapper);
7708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7709 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7711 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7712
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7714 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7716 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7717
7718 // up when pressure becomes 0, hover restored
7719 processPressure(mapper, 0);
7720 processSync(mapper);
7721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7722 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7724 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7725
7726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7727 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7729 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7730
7731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7732 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7734 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7735
7736 // exit hover when pointer goes away
7737 processId(mapper, -1);
7738 processSync(mapper);
7739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7742 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7743}
7744
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007745/**
7746 * Set the input device port <--> display port associations, and check that the
7747 * events are routed to the display that matches the display port.
7748 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7749 */
7750TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007751 const std::string usb2 = "USB2";
7752 const uint8_t hdmi1 = 0;
7753 const uint8_t hdmi2 = 1;
7754 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007755 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007756
7757 addConfigurationProperty("touch.deviceType", "touchScreen");
7758 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007759 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007760
7761 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7762 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7763
7764 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7765 // for this input device is specified, and the matching viewport is not present,
7766 // the input device should be disabled (at the mapper level).
7767
7768 // Add viewport for display 2 on hdmi2
7769 prepareSecondaryDisplay(type, hdmi2);
7770 // Send a touch event
7771 processPosition(mapper, 100, 100);
7772 processSync(mapper);
7773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7774
7775 // Add viewport for display 1 on hdmi1
7776 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7777 // Send a touch event again
7778 processPosition(mapper, 100, 100);
7779 processSync(mapper);
7780
7781 NotifyMotionArgs args;
7782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7783 ASSERT_EQ(DISPLAY_ID, args.displayId);
7784}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007785
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007786TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007787 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007788 std::shared_ptr<FakePointerController> fakePointerController =
7789 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007790 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007791 fakePointerController->setPosition(100, 200);
7792 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007793 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7794
Garfield Tan888a6a42020-01-09 11:39:16 -08007795 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007796 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007797
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007798 prepareDisplay(DISPLAY_ORIENTATION_0);
7799 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007800 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007801
7802 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007803 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007804
7805 NotifyMotionArgs motionArgs;
7806 processPosition(mapper, 100, 100);
7807 processSync(mapper);
7808
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7810 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7811 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7812}
7813
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007814/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007815 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7816 */
7817TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7818 addConfigurationProperty("touch.deviceType", "touchScreen");
7819 prepareAxes(POSITION);
7820 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7821
7822 prepareDisplay(DISPLAY_ORIENTATION_0);
7823 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7824 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7825 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7826 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7827
7828 NotifyMotionArgs args;
7829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7830 ASSERT_EQ(26, args.readTime);
7831
7832 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7833 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7834 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7835
7836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7837 ASSERT_EQ(33, args.readTime);
7838}
7839
7840/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007841 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7842 * events should not be delivered to the listener.
7843 */
7844TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7845 addConfigurationProperty("touch.deviceType", "touchScreen");
7846 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7847 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7848 ViewportType::INTERNAL);
7849 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7850 prepareAxes(POSITION);
7851 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7852
7853 NotifyMotionArgs motionArgs;
7854 processPosition(mapper, 100, 100);
7855 processSync(mapper);
7856
7857 mFakeListener->assertNotifyMotionWasNotCalled();
7858}
7859
Garfield Tanc734e4f2021-01-15 20:01:39 -08007860TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7861 addConfigurationProperty("touch.deviceType", "touchScreen");
7862 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7863 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7864 ViewportType::INTERNAL);
7865 std::optional<DisplayViewport> optionalDisplayViewport =
7866 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7867 ASSERT_TRUE(optionalDisplayViewport.has_value());
7868 DisplayViewport displayViewport = *optionalDisplayViewport;
7869
7870 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7871 prepareAxes(POSITION);
7872 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7873
7874 // Finger down
7875 int32_t x = 100, y = 100;
7876 processPosition(mapper, x, y);
7877 processSync(mapper);
7878
7879 NotifyMotionArgs motionArgs;
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7881 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7882
7883 // Deactivate display viewport
7884 displayViewport.isActive = false;
7885 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7886 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7887
7888 // Finger move
7889 x += 10, y += 10;
7890 processPosition(mapper, x, y);
7891 processSync(mapper);
7892
7893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7894 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7895
7896 // Reactivate display viewport
7897 displayViewport.isActive = true;
7898 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7899 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7900
7901 // Finger move again
7902 x += 10, y += 10;
7903 processPosition(mapper, x, y);
7904 processSync(mapper);
7905
7906 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7907 // no pointer on the touch device.
7908 mFakeListener->assertNotifyMotionWasNotCalled();
7909}
7910
Arthur Hung7c645402019-01-25 17:45:42 +08007911TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7912 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007913 prepareAxes(POSITION | ID | SLOT);
7914 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007915 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007916
7917 // Create the second touch screen device, and enable multi fingers.
7918 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007919 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007920 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007921 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007922 std::shared_ptr<InputDevice> device2 =
7923 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7924 Flags<InputDeviceClass>(0));
7925
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007926 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7927 0 /*flat*/, 0 /*fuzz*/);
7928 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7929 0 /*flat*/, 0 /*fuzz*/);
7930 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7931 0 /*flat*/, 0 /*fuzz*/);
7932 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7933 0 /*flat*/, 0 /*fuzz*/);
7934 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7935 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7936 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007937
7938 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007939 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007940 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7941 device2->reset(ARBITRARY_TIME);
7942
7943 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007944 std::shared_ptr<FakePointerController> fakePointerController =
7945 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007946 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7947 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7948
7949 // Setup policy for associated displays and show touches.
7950 const uint8_t hdmi1 = 0;
7951 const uint8_t hdmi2 = 1;
7952 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7953 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7954 mFakePolicy->setShowTouches(true);
7955
7956 // Create displays.
7957 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007958 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007959
7960 // Default device will reconfigure above, need additional reconfiguration for another device.
7961 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007962 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007963
7964 // Two fingers down at default display.
7965 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7966 processPosition(mapper, x1, y1);
7967 processId(mapper, 1);
7968 processSlot(mapper, 1);
7969 processPosition(mapper, x2, y2);
7970 processId(mapper, 2);
7971 processSync(mapper);
7972
7973 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7974 fakePointerController->getSpots().find(DISPLAY_ID);
7975 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7976 ASSERT_EQ(size_t(2), iter->second.size());
7977
7978 // Two fingers down at second display.
7979 processPosition(mapper2, x1, y1);
7980 processId(mapper2, 1);
7981 processSlot(mapper2, 1);
7982 processPosition(mapper2, x2, y2);
7983 processId(mapper2, 2);
7984 processSync(mapper2);
7985
7986 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7987 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7988 ASSERT_EQ(size_t(2), iter->second.size());
7989}
7990
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007991TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007992 prepareAxes(POSITION);
7993 addConfigurationProperty("touch.deviceType", "touchScreen");
7994 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007995 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007996
7997 NotifyMotionArgs motionArgs;
7998 // Unrotated video frame
7999 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8000 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008001 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008002 processPosition(mapper, 100, 200);
8003 processSync(mapper);
8004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8005 ASSERT_EQ(frames, motionArgs.videoFrames);
8006
8007 // Subsequent touch events should not have any videoframes
8008 // This is implemented separately in FakeEventHub,
8009 // but that should match the behaviour of TouchVideoDevice.
8010 processPosition(mapper, 200, 200);
8011 processSync(mapper);
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8013 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8014}
8015
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008016TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008017 prepareAxes(POSITION);
8018 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008019 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008020 // Unrotated video frame
8021 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8022 NotifyMotionArgs motionArgs;
8023
8024 // Test all 4 orientations
8025 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008026 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8027 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8028 clearViewports();
8029 prepareDisplay(orientation);
8030 std::vector<TouchVideoFrame> frames{frame};
8031 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8032 processPosition(mapper, 100, 200);
8033 processSync(mapper);
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8035 ASSERT_EQ(frames, motionArgs.videoFrames);
8036 }
8037}
8038
8039TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8040 prepareAxes(POSITION);
8041 addConfigurationProperty("touch.deviceType", "touchScreen");
8042 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8043 // orientation-aware are affected by display rotation.
8044 addConfigurationProperty("touch.orientationAware", "0");
8045 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8046 // Unrotated video frame
8047 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8048 NotifyMotionArgs motionArgs;
8049
8050 // Test all 4 orientations
8051 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008052 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8053 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8054 clearViewports();
8055 prepareDisplay(orientation);
8056 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008057 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008058 processPosition(mapper, 100, 200);
8059 processSync(mapper);
8060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008061 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8062 // compared to the display. This is so that when the window transform (which contains the
8063 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8064 // window's coordinate space.
8065 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008066 ASSERT_EQ(frames, motionArgs.videoFrames);
8067 }
8068}
8069
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008070TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008071 prepareAxes(POSITION);
8072 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008073 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008074 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8075 // so mix these.
8076 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8077 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8078 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8079 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8080 NotifyMotionArgs motionArgs;
8081
8082 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008083 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008084 processPosition(mapper, 100, 200);
8085 processSync(mapper);
8086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008087 ASSERT_EQ(frames, motionArgs.videoFrames);
8088}
8089
8090TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8091 prepareAxes(POSITION);
8092 addConfigurationProperty("touch.deviceType", "touchScreen");
8093 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8094 // orientation-aware are affected by display rotation.
8095 addConfigurationProperty("touch.orientationAware", "0");
8096 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8097 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8098 // so mix these.
8099 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8100 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8101 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8102 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8103 NotifyMotionArgs motionArgs;
8104
8105 prepareDisplay(DISPLAY_ORIENTATION_90);
8106 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8107 processPosition(mapper, 100, 200);
8108 processSync(mapper);
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8110 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8111 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8112 // compared to the display. This is so that when the window transform (which contains the
8113 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8114 // window's coordinate space.
8115 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8116 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008117 ASSERT_EQ(frames, motionArgs.videoFrames);
8118}
8119
Arthur Hung9da14732019-09-02 16:16:58 +08008120/**
8121 * If we had defined port associations, but the viewport is not ready, the touch device would be
8122 * expected to be disabled, and it should be enabled after the viewport has found.
8123 */
8124TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008125 constexpr uint8_t hdmi2 = 1;
8126 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008127 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008128
8129 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8130
8131 addConfigurationProperty("touch.deviceType", "touchScreen");
8132 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008133 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008134
8135 ASSERT_EQ(mDevice->isEnabled(), false);
8136
8137 // Add display on hdmi2, the device should be enabled and can receive touch event.
8138 prepareSecondaryDisplay(type, hdmi2);
8139 ASSERT_EQ(mDevice->isEnabled(), true);
8140
8141 // Send a touch event.
8142 processPosition(mapper, 100, 100);
8143 processSync(mapper);
8144
8145 NotifyMotionArgs args;
8146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8147 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8148}
8149
Arthur Hung421eb1c2020-01-16 00:09:42 +08008150TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008151 addConfigurationProperty("touch.deviceType", "touchScreen");
8152 prepareDisplay(DISPLAY_ORIENTATION_0);
8153 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008154 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008155
8156 NotifyMotionArgs motionArgs;
8157
8158 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8159 // finger down
8160 processId(mapper, 1);
8161 processPosition(mapper, x1, y1);
8162 processSync(mapper);
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8164 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8166
8167 // finger move
8168 processId(mapper, 1);
8169 processPosition(mapper, x2, y2);
8170 processSync(mapper);
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8174
8175 // finger up.
8176 processId(mapper, -1);
8177 processSync(mapper);
8178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8179 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8181
8182 // new finger down
8183 processId(mapper, 1);
8184 processPosition(mapper, x3, y3);
8185 processSync(mapper);
8186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8187 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8189}
8190
8191/**
arthurhungcc7f9802020-04-30 17:55:40 +08008192 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8193 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008194 */
arthurhungcc7f9802020-04-30 17:55:40 +08008195TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008196 addConfigurationProperty("touch.deviceType", "touchScreen");
8197 prepareDisplay(DISPLAY_ORIENTATION_0);
8198 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008199 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008200
8201 NotifyMotionArgs motionArgs;
8202
8203 // default tool type is finger
8204 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008205 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008206 processPosition(mapper, x1, y1);
8207 processSync(mapper);
8208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8209 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8210 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8211
8212 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8213 processToolType(mapper, MT_TOOL_PALM);
8214 processSync(mapper);
8215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8216 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8217
8218 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008219 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008220 processPosition(mapper, x2, y2);
8221 processSync(mapper);
8222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8223
8224 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008225 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008226 processSync(mapper);
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8228
8229 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008230 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008231 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008232 processPosition(mapper, x3, y3);
8233 processSync(mapper);
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8235 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8237}
8238
arthurhungbf89a482020-04-17 17:37:55 +08008239/**
arthurhungcc7f9802020-04-30 17:55:40 +08008240 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8241 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008242 */
arthurhungcc7f9802020-04-30 17:55:40 +08008243TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008244 addConfigurationProperty("touch.deviceType", "touchScreen");
8245 prepareDisplay(DISPLAY_ORIENTATION_0);
8246 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8247 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8248
8249 NotifyMotionArgs motionArgs;
8250
8251 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008252 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8253 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008254 processPosition(mapper, x1, y1);
8255 processSync(mapper);
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8259
8260 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008261 processSlot(mapper, SECOND_SLOT);
8262 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008263 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008264 processSync(mapper);
8265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8266 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8267 motionArgs.action);
8268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8269
8270 // If the tool type of the first finger changes to MT_TOOL_PALM,
8271 // we expect to receive ACTION_POINTER_UP with cancel flag.
8272 processSlot(mapper, FIRST_SLOT);
8273 processId(mapper, FIRST_TRACKING_ID);
8274 processToolType(mapper, MT_TOOL_PALM);
8275 processSync(mapper);
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8277 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8278 motionArgs.action);
8279 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8280
8281 // The following MOVE events of second finger should be processed.
8282 processSlot(mapper, SECOND_SLOT);
8283 processId(mapper, SECOND_TRACKING_ID);
8284 processPosition(mapper, x2 + 1, y2 + 1);
8285 processSync(mapper);
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8288 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8289
8290 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8291 // it. Second finger receive move.
8292 processSlot(mapper, FIRST_SLOT);
8293 processId(mapper, INVALID_TRACKING_ID);
8294 processSync(mapper);
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8297 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8298
8299 // Second finger keeps moving.
8300 processSlot(mapper, SECOND_SLOT);
8301 processId(mapper, SECOND_TRACKING_ID);
8302 processPosition(mapper, x2 + 2, y2 + 2);
8303 processSync(mapper);
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8306 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8307
8308 // Second finger up.
8309 processId(mapper, INVALID_TRACKING_ID);
8310 processSync(mapper);
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8312 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8313 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8314}
8315
8316/**
8317 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8318 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8319 */
8320TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8321 addConfigurationProperty("touch.deviceType", "touchScreen");
8322 prepareDisplay(DISPLAY_ORIENTATION_0);
8323 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8324 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8325
8326 NotifyMotionArgs motionArgs;
8327
8328 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8329 // First finger down.
8330 processId(mapper, FIRST_TRACKING_ID);
8331 processPosition(mapper, x1, y1);
8332 processSync(mapper);
8333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8334 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8336
8337 // Second finger down.
8338 processSlot(mapper, SECOND_SLOT);
8339 processId(mapper, SECOND_TRACKING_ID);
8340 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008341 processSync(mapper);
8342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8343 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8344 motionArgs.action);
8345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8346
arthurhungcc7f9802020-04-30 17:55:40 +08008347 // If the tool type of the first finger changes to MT_TOOL_PALM,
8348 // we expect to receive ACTION_POINTER_UP with cancel flag.
8349 processSlot(mapper, FIRST_SLOT);
8350 processId(mapper, FIRST_TRACKING_ID);
8351 processToolType(mapper, MT_TOOL_PALM);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8355 motionArgs.action);
8356 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8357
8358 // Second finger keeps moving.
8359 processSlot(mapper, SECOND_SLOT);
8360 processId(mapper, SECOND_TRACKING_ID);
8361 processPosition(mapper, x2 + 1, y2 + 1);
8362 processSync(mapper);
8363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8365
8366 // second finger becomes palm, receive cancel due to only 1 finger is active.
8367 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008368 processToolType(mapper, MT_TOOL_PALM);
8369 processSync(mapper);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8371 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8372
arthurhungcc7f9802020-04-30 17:55:40 +08008373 // third finger down.
8374 processSlot(mapper, THIRD_SLOT);
8375 processId(mapper, THIRD_TRACKING_ID);
8376 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008377 processPosition(mapper, x3, y3);
8378 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8380 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008382 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8383
8384 // third finger move
8385 processId(mapper, THIRD_TRACKING_ID);
8386 processPosition(mapper, x3 + 1, y3 + 1);
8387 processSync(mapper);
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8390
8391 // first finger up, third finger receive move.
8392 processSlot(mapper, FIRST_SLOT);
8393 processId(mapper, INVALID_TRACKING_ID);
8394 processSync(mapper);
8395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8397 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8398
8399 // second finger up, third finger receive move.
8400 processSlot(mapper, SECOND_SLOT);
8401 processId(mapper, INVALID_TRACKING_ID);
8402 processSync(mapper);
8403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8405 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8406
8407 // third finger up.
8408 processSlot(mapper, THIRD_SLOT);
8409 processId(mapper, INVALID_TRACKING_ID);
8410 processSync(mapper);
8411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8412 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8413 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8414}
8415
8416/**
8417 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8418 * and the active finger could still be allowed to receive the events
8419 */
8420TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8421 addConfigurationProperty("touch.deviceType", "touchScreen");
8422 prepareDisplay(DISPLAY_ORIENTATION_0);
8423 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8424 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8425
8426 NotifyMotionArgs motionArgs;
8427
8428 // default tool type is finger
8429 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8430 processId(mapper, FIRST_TRACKING_ID);
8431 processPosition(mapper, x1, y1);
8432 processSync(mapper);
8433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8434 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8436
8437 // Second finger down.
8438 processSlot(mapper, SECOND_SLOT);
8439 processId(mapper, SECOND_TRACKING_ID);
8440 processPosition(mapper, x2, y2);
8441 processSync(mapper);
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8443 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8444 motionArgs.action);
8445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8446
8447 // If the tool type of the second finger changes to MT_TOOL_PALM,
8448 // we expect to receive ACTION_POINTER_UP with cancel flag.
8449 processId(mapper, SECOND_TRACKING_ID);
8450 processToolType(mapper, MT_TOOL_PALM);
8451 processSync(mapper);
8452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8453 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8454 motionArgs.action);
8455 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8456
8457 // The following MOVE event should be processed.
8458 processSlot(mapper, FIRST_SLOT);
8459 processId(mapper, FIRST_TRACKING_ID);
8460 processPosition(mapper, x1 + 1, y1 + 1);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8464 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8465
8466 // second finger up.
8467 processSlot(mapper, SECOND_SLOT);
8468 processId(mapper, INVALID_TRACKING_ID);
8469 processSync(mapper);
8470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8472
8473 // first finger keep moving
8474 processSlot(mapper, FIRST_SLOT);
8475 processId(mapper, FIRST_TRACKING_ID);
8476 processPosition(mapper, x1 + 2, y1 + 2);
8477 processSync(mapper);
8478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8480
8481 // first finger up.
8482 processId(mapper, INVALID_TRACKING_ID);
8483 processSync(mapper);
8484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8486 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008487}
8488
Arthur Hung9ad18942021-06-19 02:04:46 +00008489/**
8490 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8491 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8492 * cause slot be valid again.
8493 */
8494TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8495 addConfigurationProperty("touch.deviceType", "touchScreen");
8496 prepareDisplay(DISPLAY_ORIENTATION_0);
8497 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8498 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8499
8500 NotifyMotionArgs motionArgs;
8501
8502 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8503 // First finger down.
8504 processId(mapper, FIRST_TRACKING_ID);
8505 processPosition(mapper, x1, y1);
8506 processPressure(mapper, RAW_PRESSURE_MAX);
8507 processSync(mapper);
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8510 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8511
8512 // First finger move.
8513 processId(mapper, FIRST_TRACKING_ID);
8514 processPosition(mapper, x1 + 1, y1 + 1);
8515 processPressure(mapper, RAW_PRESSURE_MAX);
8516 processSync(mapper);
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8519 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8520
8521 // Second finger down.
8522 processSlot(mapper, SECOND_SLOT);
8523 processId(mapper, SECOND_TRACKING_ID);
8524 processPosition(mapper, x2, y2);
8525 processPressure(mapper, RAW_PRESSURE_MAX);
8526 processSync(mapper);
8527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8528 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8529 motionArgs.action);
8530 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8531
8532 // second finger up with some unexpected data.
8533 processSlot(mapper, SECOND_SLOT);
8534 processId(mapper, INVALID_TRACKING_ID);
8535 processPosition(mapper, x2, y2);
8536 processSync(mapper);
8537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8538 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8539 motionArgs.action);
8540 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8541
8542 // first finger up with some unexpected data.
8543 processSlot(mapper, FIRST_SLOT);
8544 processId(mapper, INVALID_TRACKING_ID);
8545 processPosition(mapper, x2, y2);
8546 processPressure(mapper, RAW_PRESSURE_MAX);
8547 processSync(mapper);
8548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8549 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8550 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8551}
8552
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008553// --- MultiTouchInputMapperTest_ExternalDevice ---
8554
8555class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8556protected:
Chris Yea52ade12020-08-27 16:49:20 -07008557 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008558};
8559
8560/**
8561 * Expect fallback to internal viewport if device is external and external viewport is not present.
8562 */
8563TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8564 prepareAxes(POSITION);
8565 addConfigurationProperty("touch.deviceType", "touchScreen");
8566 prepareDisplay(DISPLAY_ORIENTATION_0);
8567 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8568
8569 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8570
8571 NotifyMotionArgs motionArgs;
8572
8573 // Expect the event to be sent to the internal viewport,
8574 // because an external viewport is not present.
8575 processPosition(mapper, 100, 100);
8576 processSync(mapper);
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8579
8580 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008581 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008582 processPosition(mapper, 100, 100);
8583 processSync(mapper);
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8585 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8586}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008587
8588/**
8589 * Test touch should not work if outside of surface.
8590 */
8591class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8592protected:
8593 void halfDisplayToCenterHorizontal(int32_t orientation) {
8594 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008595 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008596
8597 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8598 internalViewport->orientation = orientation;
8599 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8600 internalViewport->logicalLeft = 0;
8601 internalViewport->logicalTop = 0;
8602 internalViewport->logicalRight = DISPLAY_HEIGHT;
8603 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8604
8605 internalViewport->physicalLeft = 0;
8606 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8607 internalViewport->physicalRight = DISPLAY_HEIGHT;
8608 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8609
8610 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8611 internalViewport->deviceHeight = DISPLAY_WIDTH;
8612 } else {
8613 internalViewport->logicalLeft = 0;
8614 internalViewport->logicalTop = 0;
8615 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8616 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8617
8618 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8619 internalViewport->physicalTop = 0;
8620 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8621 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8622
8623 internalViewport->deviceWidth = DISPLAY_WIDTH;
8624 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8625 }
8626
8627 mFakePolicy->updateViewport(internalViewport.value());
8628 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8629 }
8630
arthurhung5d547942020-12-14 17:04:45 +08008631 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8632 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008633 int32_t yExpected) {
8634 // touch on outside area should not work.
8635 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8636 processSync(mapper);
8637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8638
8639 // touch on inside area should receive the event.
8640 NotifyMotionArgs args;
8641 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8642 processSync(mapper);
8643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8644 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8645 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8646
8647 // Reset.
8648 mapper.reset(ARBITRARY_TIME);
8649 }
8650};
8651
arthurhung5d547942020-12-14 17:04:45 +08008652TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008653 addConfigurationProperty("touch.deviceType", "touchScreen");
8654 prepareDisplay(DISPLAY_ORIENTATION_0);
8655 prepareAxes(POSITION);
8656 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8657
8658 // Touch on center of normal display should work.
8659 const int32_t x = DISPLAY_WIDTH / 4;
8660 const int32_t y = DISPLAY_HEIGHT / 2;
8661 processPosition(mapper, toRawX(x), toRawY(y));
8662 processSync(mapper);
8663 NotifyMotionArgs args;
8664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8666 0.0f, 0.0f, 0.0f, 0.0f));
8667 // Reset.
8668 mapper.reset(ARBITRARY_TIME);
8669
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008670 // Let physical display be different to device, and make surface and physical could be 1:1 in
8671 // all four orientations.
8672 for (int orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
8673 DISPLAY_ORIENTATION_270}) {
8674 halfDisplayToCenterHorizontal(orientation);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008675
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008676 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8677 const int32_t yExpected = y;
8678 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8679 }
Arthur Hung4197f6b2020-03-16 15:39:59 +08008680}
8681
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008682TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90_NotOrientationAware) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008683 addConfigurationProperty("touch.deviceType", "touchScreen");
8684 prepareDisplay(DISPLAY_ORIENTATION_0);
8685 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008686 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8687 // orientation-aware are affected by display rotation.
8688 addConfigurationProperty("touch.orientationAware", "0");
Arthur Hung4197f6b2020-03-16 15:39:59 +08008689 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8690
8691 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8692 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8693
8694 const int32_t x = DISPLAY_WIDTH / 4;
8695 const int32_t y = DISPLAY_HEIGHT / 2;
8696
Arthur Hung4197f6b2020-03-16 15:39:59 +08008697 // expect x/y = swap x/y then reverse x.
8698 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8699 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8700 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8701}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008702
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008703TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270_NotOrientationAware) {
arthurhunga36b28e2020-12-29 20:28:15 +08008704 addConfigurationProperty("touch.deviceType", "touchScreen");
8705 prepareDisplay(DISPLAY_ORIENTATION_0);
8706 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008707 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8708 // orientation-aware are affected by display rotation.
8709 addConfigurationProperty("touch.orientationAware", "0");
8710 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8711
8712 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8713 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8714
8715 const int32_t x = DISPLAY_WIDTH / 4;
8716 const int32_t y = DISPLAY_HEIGHT / 2;
8717
8718 // expect x/y = swap x/y then reverse y.
8719 const int32_t xExpected = y;
8720 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8721 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8722}
8723
8724TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner_NotOrientationAware) {
8725 addConfigurationProperty("touch.deviceType", "touchScreen");
8726 prepareDisplay(DISPLAY_ORIENTATION_0);
8727 prepareAxes(POSITION);
8728 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8729 // orientation-aware are affected by display rotation.
8730 addConfigurationProperty("touch.orientationAware", "0");
arthurhunga36b28e2020-12-29 20:28:15 +08008731 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8732
8733 const int32_t x = 0;
8734 const int32_t y = 0;
8735
8736 const int32_t xExpected = x;
8737 const int32_t yExpected = y;
8738 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8739
8740 clearViewports();
8741 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008742 // expect x/y = swap x/y then reverse x.
8743 const int32_t xExpected90 = DISPLAY_HEIGHT - 1;
8744 const int32_t yExpected90 = x;
arthurhunga36b28e2020-12-29 20:28:15 +08008745 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8746
8747 clearViewports();
8748 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008749 // expect x/y = swap x/y then reverse y.
8750 const int32_t xExpected270 = y;
8751 const int32_t yExpected270 = DISPLAY_WIDTH - 1;
arthurhunga36b28e2020-12-29 20:28:15 +08008752 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8753}
8754
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008755TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8756 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8757 std::shared_ptr<FakePointerController> fakePointerController =
8758 std::make_shared<FakePointerController>();
8759 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8760 fakePointerController->setPosition(0, 0);
8761 fakePointerController->setButtonState(0);
8762
8763 // prepare device and capture
8764 prepareDisplay(DISPLAY_ORIENTATION_0);
8765 prepareAxes(POSITION | ID | SLOT);
8766 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8767 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8768 mFakePolicy->setPointerCapture(true);
8769 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8770 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8771
8772 // captured touchpad should be a touchpad source
8773 NotifyDeviceResetArgs resetArgs;
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8775 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8776
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008777 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008778
8779 const InputDeviceInfo::MotionRange* relRangeX =
8780 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8781 ASSERT_NE(relRangeX, nullptr);
8782 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8783 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8784 const InputDeviceInfo::MotionRange* relRangeY =
8785 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8786 ASSERT_NE(relRangeY, nullptr);
8787 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8788 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8789
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008790 // run captured pointer tests - note that this is unscaled, so input listener events should be
8791 // identical to what the hardware sends (accounting for any
8792 // calibration).
8793 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008794 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008795 processId(mapper, 1);
8796 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8797 processKey(mapper, BTN_TOUCH, 1);
8798 processSync(mapper);
8799
8800 // expect coord[0] to contain initial location of touch 0
8801 NotifyMotionArgs args;
8802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8803 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8804 ASSERT_EQ(1U, args.pointerCount);
8805 ASSERT_EQ(0, args.pointerProperties[0].id);
8806 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8807 ASSERT_NO_FATAL_FAILURE(
8808 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8809
8810 // FINGER 1 DOWN
8811 processSlot(mapper, 1);
8812 processId(mapper, 2);
8813 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8814 processSync(mapper);
8815
8816 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008818 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8819 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008820 ASSERT_EQ(2U, args.pointerCount);
8821 ASSERT_EQ(0, args.pointerProperties[0].id);
8822 ASSERT_EQ(1, args.pointerProperties[1].id);
8823 ASSERT_NO_FATAL_FAILURE(
8824 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8825 ASSERT_NO_FATAL_FAILURE(
8826 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8827
8828 // FINGER 1 MOVE
8829 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8830 processSync(mapper);
8831
8832 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8833 // from move
8834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8836 ASSERT_NO_FATAL_FAILURE(
8837 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8838 ASSERT_NO_FATAL_FAILURE(
8839 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8840
8841 // FINGER 0 MOVE
8842 processSlot(mapper, 0);
8843 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8844 processSync(mapper);
8845
8846 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8849 ASSERT_NO_FATAL_FAILURE(
8850 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8851 ASSERT_NO_FATAL_FAILURE(
8852 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8853
8854 // BUTTON DOWN
8855 processKey(mapper, BTN_LEFT, 1);
8856 processSync(mapper);
8857
8858 // touchinputmapper design sends a move before button press
8859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8863
8864 // BUTTON UP
8865 processKey(mapper, BTN_LEFT, 0);
8866 processSync(mapper);
8867
8868 // touchinputmapper design sends a move after button release
8869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8870 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8873
8874 // FINGER 0 UP
8875 processId(mapper, -1);
8876 processSync(mapper);
8877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8878 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8879
8880 // FINGER 1 MOVE
8881 processSlot(mapper, 1);
8882 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8883 processSync(mapper);
8884
8885 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8888 ASSERT_EQ(1U, args.pointerCount);
8889 ASSERT_EQ(1, args.pointerProperties[0].id);
8890 ASSERT_NO_FATAL_FAILURE(
8891 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8892
8893 // FINGER 1 UP
8894 processId(mapper, -1);
8895 processKey(mapper, BTN_TOUCH, 0);
8896 processSync(mapper);
8897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8898 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8899
8900 // non captured touchpad should be a mouse source
8901 mFakePolicy->setPointerCapture(false);
8902 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8904 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8905}
8906
8907TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8908 std::shared_ptr<FakePointerController> fakePointerController =
8909 std::make_shared<FakePointerController>();
8910 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8911 fakePointerController->setPosition(0, 0);
8912 fakePointerController->setButtonState(0);
8913
8914 // prepare device and capture
8915 prepareDisplay(DISPLAY_ORIENTATION_0);
8916 prepareAxes(POSITION | ID | SLOT);
8917 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8918 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8919 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8920 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8921 // run uncaptured pointer tests - pushes out generic events
8922 // FINGER 0 DOWN
8923 processId(mapper, 3);
8924 processPosition(mapper, 100, 100);
8925 processKey(mapper, BTN_TOUCH, 1);
8926 processSync(mapper);
8927
8928 // start at (100,100), cursor should be at (0,0) * scale
8929 NotifyMotionArgs args;
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8931 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8932 ASSERT_NO_FATAL_FAILURE(
8933 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8934
8935 // FINGER 0 MOVE
8936 processPosition(mapper, 200, 200);
8937 processSync(mapper);
8938
8939 // compute scaling to help with touch position checking
8940 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8941 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8942 float scale =
8943 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8944
8945 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8947 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8949 0, 0, 0, 0, 0, 0, 0));
8950}
8951
8952TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8953 std::shared_ptr<FakePointerController> fakePointerController =
8954 std::make_shared<FakePointerController>();
8955
8956 prepareDisplay(DISPLAY_ORIENTATION_0);
8957 prepareAxes(POSITION | ID | SLOT);
8958 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8959 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8960 mFakePolicy->setPointerCapture(false);
8961 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8962
8963 // uncaptured touchpad should be a pointer device
8964 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8965
8966 // captured touchpad should be a touchpad device
8967 mFakePolicy->setPointerCapture(true);
8968 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8969 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8970}
8971
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008972// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008973
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008974class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008975protected:
8976 static const char* DEVICE_NAME;
8977 static const char* DEVICE_LOCATION;
8978 static const int32_t DEVICE_ID;
8979 static const int32_t DEVICE_GENERATION;
8980 static const int32_t DEVICE_CONTROLLER_NUMBER;
8981 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8982 static const int32_t EVENTHUB_ID;
8983
8984 std::shared_ptr<FakeEventHub> mFakeEventHub;
8985 sp<FakeInputReaderPolicy> mFakePolicy;
8986 sp<TestInputListener> mFakeListener;
8987 std::unique_ptr<InstrumentedInputReader> mReader;
8988 std::shared_ptr<InputDevice> mDevice;
8989
8990 virtual void SetUp(Flags<InputDeviceClass> classes) {
8991 mFakeEventHub = std::make_unique<FakeEventHub>();
8992 mFakePolicy = new FakeInputReaderPolicy();
8993 mFakeListener = new TestInputListener();
8994 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8995 mFakeListener);
8996 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8997 }
8998
8999 void SetUp() override { SetUp(DEVICE_CLASSES); }
9000
9001 void TearDown() override {
9002 mFakeListener.clear();
9003 mFakePolicy.clear();
9004 }
9005
9006 void configureDevice(uint32_t changes) {
9007 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9008 mReader->requestRefreshConfiguration(changes);
9009 mReader->loopOnce();
9010 }
9011 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9012 }
9013
9014 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9015 const std::string& location, int32_t eventHubId,
9016 Flags<InputDeviceClass> classes) {
9017 InputDeviceIdentifier identifier;
9018 identifier.name = name;
9019 identifier.location = location;
9020 std::shared_ptr<InputDevice> device =
9021 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9022 identifier);
9023 mReader->pushNextDevice(device);
9024 mFakeEventHub->addDevice(eventHubId, name, classes);
9025 mReader->loopOnce();
9026 return device;
9027 }
9028
9029 template <class T, typename... Args>
9030 T& addControllerAndConfigure(Args... args) {
9031 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9032
9033 return controller;
9034 }
9035};
9036
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009037const char* PeripheralControllerTest::DEVICE_NAME = "device";
9038const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9039const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9040const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9041const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
9042const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08009043 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009044const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009045
9046// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009047class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009048protected:
9049 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009050 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009051 }
9052};
9053
9054TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009055 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009056
9057 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9058 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9059}
9060
9061TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009062 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009063
9064 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9065 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9066}
9067
9068// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009069class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009070protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009071 void SetUp() override {
9072 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9073 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009074};
9075
Chris Ye85758332021-05-16 23:05:17 -07009076TEST_F(LightControllerTest, MonoLight) {
9077 RawLightInfo infoMono = {.id = 1,
9078 .name = "Mono",
9079 .maxBrightness = 255,
9080 .flags = InputLightClass::BRIGHTNESS,
9081 .path = ""};
9082 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009083
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009084 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009085 InputDeviceInfo info;
9086 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009087 std::vector<InputDeviceLightInfo> lights = info.getLights();
9088 ASSERT_EQ(1U, lights.size());
9089 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009090
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009091 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9092 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009093}
9094
9095TEST_F(LightControllerTest, RGBLight) {
9096 RawLightInfo infoRed = {.id = 1,
9097 .name = "red",
9098 .maxBrightness = 255,
9099 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9100 .path = ""};
9101 RawLightInfo infoGreen = {.id = 2,
9102 .name = "green",
9103 .maxBrightness = 255,
9104 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9105 .path = ""};
9106 RawLightInfo infoBlue = {.id = 3,
9107 .name = "blue",
9108 .maxBrightness = 255,
9109 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9110 .path = ""};
9111 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9112 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9113 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9114
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009115 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009116 InputDeviceInfo info;
9117 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009118 std::vector<InputDeviceLightInfo> lights = info.getLights();
9119 ASSERT_EQ(1U, lights.size());
9120 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009121
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009122 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9123 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009124}
9125
9126TEST_F(LightControllerTest, MultiColorRGBLight) {
9127 RawLightInfo infoColor = {.id = 1,
9128 .name = "red",
9129 .maxBrightness = 255,
9130 .flags = InputLightClass::BRIGHTNESS |
9131 InputLightClass::MULTI_INTENSITY |
9132 InputLightClass::MULTI_INDEX,
9133 .path = ""};
9134
9135 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9136
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009137 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009138 InputDeviceInfo info;
9139 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009140 std::vector<InputDeviceLightInfo> lights = info.getLights();
9141 ASSERT_EQ(1U, lights.size());
9142 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009143
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009144 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9145 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009146}
9147
9148TEST_F(LightControllerTest, PlayerIdLight) {
9149 RawLightInfo info1 = {.id = 1,
9150 .name = "player1",
9151 .maxBrightness = 255,
9152 .flags = InputLightClass::BRIGHTNESS,
9153 .path = ""};
9154 RawLightInfo info2 = {.id = 2,
9155 .name = "player2",
9156 .maxBrightness = 255,
9157 .flags = InputLightClass::BRIGHTNESS,
9158 .path = ""};
9159 RawLightInfo info3 = {.id = 3,
9160 .name = "player3",
9161 .maxBrightness = 255,
9162 .flags = InputLightClass::BRIGHTNESS,
9163 .path = ""};
9164 RawLightInfo info4 = {.id = 4,
9165 .name = "player4",
9166 .maxBrightness = 255,
9167 .flags = InputLightClass::BRIGHTNESS,
9168 .path = ""};
9169 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9170 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9171 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9172 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9173
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009174 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009175 InputDeviceInfo info;
9176 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009177 std::vector<InputDeviceLightInfo> lights = info.getLights();
9178 ASSERT_EQ(1U, lights.size());
9179 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009180
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009181 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9182 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9183 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009184}
9185
Michael Wrightd02c5b62014-02-10 15:10:22 -08009186} // namespace android