blob: 23649a93173e339d1142ba3795eab3dd008fca96 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
Prabir Pradhanc14266f2021-05-12 15:56:24 -070019#include <InputFlingerProperties.sysprop.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <InputMapper.h>
21#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080022#include <InputReaderBase.h>
23#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070024#include <KeyboardInputMapper.h>
25#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070026#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070027#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <SingleTouchInputMapper.h>
29#include <SwitchInputMapper.h>
30#include <TestInputListener.h>
31#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080032#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000033#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070034#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050036#include <gui/constants.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080037#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <math.h>
39
Michael Wright17db18e2020-06-26 20:51:44 +010040#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080041#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000042#include "input/DisplayViewport.h"
43#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045namespace android {
46
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070047using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070048using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070049
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Michael Wrightd02c5b62014-02-10 15:10:22 -080082// Error tolerance for floating point assertions.
83static const float EPSILON = 0.001f;
84
85template<typename T>
86static inline T min(T a, T b) {
87 return a < b ? a : b;
88}
89
90static inline float avg(float x, float y) {
91 return (x + y) / 2;
92}
93
Chris Ye3fdbfef2021-01-06 18:45:18 -080094// Mapping for light color name and the light color
95const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
96 {"green", LightColor::GREEN},
97 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
Prabir Pradhanc14266f2021-05-12 15:56:24 -070099static int32_t getInverseRotation(int32_t orientation) {
100 switch (orientation) {
101 case DISPLAY_ORIENTATION_90:
102 return DISPLAY_ORIENTATION_270;
103 case DISPLAY_ORIENTATION_270:
104 return DISPLAY_ORIENTATION_90;
105 default:
106 return orientation;
107 }
108}
109
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110// --- FakePointerController ---
111
112class FakePointerController : public PointerControllerInterface {
113 bool mHaveBounds;
114 float mMinX, mMinY, mMaxX, mMaxY;
115 float mX, mY;
116 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800117 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119public:
120 FakePointerController() :
121 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800122 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 }
124
Michael Wright17db18e2020-06-26 20:51:44 +0100125 virtual ~FakePointerController() {}
126
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127 void setBounds(float minX, float minY, float maxX, float maxY) {
128 mHaveBounds = true;
129 mMinX = minX;
130 mMinY = minY;
131 mMaxX = maxX;
132 mMaxY = maxY;
133 }
134
Chris Yea52ade12020-08-27 16:49:20 -0700135 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136 mX = x;
137 mY = y;
138 }
139
Chris Yea52ade12020-08-27 16:49:20 -0700140 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141
Chris Yea52ade12020-08-27 16:49:20 -0700142 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143
Chris Yea52ade12020-08-27 16:49:20 -0700144 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 *outX = mX;
146 *outY = mY;
147 }
148
Chris Yea52ade12020-08-27 16:49:20 -0700149 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800150
Chris Yea52ade12020-08-27 16:49:20 -0700151 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800152 mDisplayId = viewport.displayId;
153 }
154
Arthur Hung7c645402019-01-25 17:45:42 +0800155 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
156 return mSpotsByDisplay;
157 }
158
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159private:
Chris Yea52ade12020-08-27 16:49:20 -0700160 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 *outMinX = mMinX;
162 *outMinY = mMinY;
163 *outMaxX = mMaxX;
164 *outMaxY = mMaxY;
165 return mHaveBounds;
166 }
167
Chris Yea52ade12020-08-27 16:49:20 -0700168 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169 mX += deltaX;
170 if (mX < mMinX) mX = mMinX;
171 if (mX > mMaxX) mX = mMaxX;
172 mY += deltaY;
173 if (mY < mMinY) mY = mMinY;
174 if (mY > mMaxY) mY = mMaxY;
175 }
176
Chris Yea52ade12020-08-27 16:49:20 -0700177 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178
Chris Yea52ade12020-08-27 16:49:20 -0700179 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180
Chris Yea52ade12020-08-27 16:49:20 -0700181 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182
Chris Yea52ade12020-08-27 16:49:20 -0700183 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
184 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800185 std::vector<int32_t> newSpots;
186 // Add spots for fingers that are down.
187 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
188 uint32_t id = idBits.clearFirstMarkedBit();
189 newSpots.push_back(id);
190 }
191
192 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800196
197 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198};
199
200
201// --- FakeInputReaderPolicy ---
202
203class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700204 std::mutex mLock;
205 std::condition_variable mDevicesChangedCondition;
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000208 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
210 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100211 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700212 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213
214protected:
Chris Yea52ade12020-08-27 16:49:20 -0700215 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
217public:
218 FakeInputReaderPolicy() {
219 }
220
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800222 waitForInputDevices([](bool devicesChanged) {
223 if (!devicesChanged) {
224 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
225 }
226 });
227 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700228
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800229 void assertInputDevicesNotChanged() {
230 waitForInputDevices([](bool devicesChanged) {
231 if (devicesChanged) {
232 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
233 }
234 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700235 }
236
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700237 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100239 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700240 }
241
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700242 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
243 return mConfig.getDisplayViewportByUniqueId(uniqueId);
244 }
245 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
246 return mConfig.getDisplayViewportByType(type);
247 }
248
249 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
250 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700251 }
252
253 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000254 bool isActive, const std::string& uniqueId,
255 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
256 const DisplayViewport viewport =
257 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
258 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700259 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100260 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 }
262
Arthur Hung6cd19a42019-08-30 19:04:12 +0800263 bool updateViewport(const DisplayViewport& viewport) {
264 size_t count = mViewports.size();
265 for (size_t i = 0; i < count; i++) {
266 const DisplayViewport& currentViewport = mViewports[i];
267 if (currentViewport.displayId == viewport.displayId) {
268 mViewports[i] = viewport;
269 mConfig.setDisplayViewports(mViewports);
270 return true;
271 }
272 }
273 // no viewport found.
274 return false;
275 }
276
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100277 void addExcludedDeviceName(const std::string& deviceName) {
278 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 }
280
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700281 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
282 mConfig.portAssociations.insert({inputPort, displayPort});
283 }
284
Christine Franks1ba71cc2021-04-07 14:37:42 -0700285 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
286 const std::string& displayUniqueId) {
287 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
288 }
289
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000290 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700291
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000292 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700293
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000294 void setPointerController(std::shared_ptr<FakePointerController> controller) {
295 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 }
297
298 const InputReaderConfiguration* getReaderConfiguration() const {
299 return &mConfig;
300 }
301
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800302 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 return mInputDevices;
304 }
305
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100306 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700307 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700308 return transform;
309 }
310
311 void setTouchAffineTransformation(const TouchAffineTransformation t) {
312 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800313 }
314
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
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000360 std::shared_ptr<PointerControllerInterface> obtainPointerController(
361 int32_t /*deviceId*/) override {
362 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363 }
364
Chris Yea52ade12020-08-27 16:49:20 -0700365 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700366 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700368 mInputDevicesChanged = true;
369 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370 }
371
Chris Yea52ade12020-08-27 16:49:20 -0700372 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
373 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700374 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375 }
376
Chris Yea52ade12020-08-27 16:49:20 -0700377 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800378
379 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
380 std::unique_lock<std::mutex> lock(mLock);
381 base::ScopedLockAssertion assumeLocked(mLock);
382
383 const bool devicesChanged =
384 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
385 return mInputDevicesChanged;
386 });
387 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
388 mInputDevicesChanged = false;
389 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390};
391
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392// --- FakeEventHub ---
393
394class FakeEventHub : public EventHubInterface {
395 struct KeyInfo {
396 int32_t keyCode;
397 uint32_t flags;
398 };
399
Chris Yef59a2f42020-10-16 12:55:26 -0700400 struct SensorInfo {
401 InputDeviceSensorType sensorType;
402 int32_t sensorDataIndex;
403 };
404
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 struct Device {
406 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700407 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 PropertyMap configuration;
409 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
410 KeyedVector<int, bool> relativeAxes;
411 KeyedVector<int32_t, int32_t> keyCodeStates;
412 KeyedVector<int32_t, int32_t> scanCodeStates;
413 KeyedVector<int32_t, int32_t> switchStates;
414 KeyedVector<int32_t, int32_t> absoluteAxisValue;
415 KeyedVector<int32_t, KeyInfo> keysByScanCode;
416 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
417 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700418 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
419 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800420 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700421 bool enabled;
422
423 status_t enable() {
424 enabled = true;
425 return OK;
426 }
427
428 status_t disable() {
429 enabled = false;
430 return OK;
431 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432
Chris Ye1b0c7342020-07-28 21:57:03 -0700433 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434 };
435
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700436 std::mutex mLock;
437 std::condition_variable mEventsCondition;
438
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100440 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000441 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600442 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000443 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800444 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
445 // Simulates a device light brightness, from light id to light brightness.
446 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
447 // Simulates a device light intensities, from light id to light intensities map.
448 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
449 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800450
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700451public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800452 virtual ~FakeEventHub() {
453 for (size_t i = 0; i < mDevices.size(); i++) {
454 delete mDevices.valueAt(i);
455 }
456 }
457
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 FakeEventHub() { }
459
Chris Ye1b0c7342020-07-28 21:57:03 -0700460 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 Device* device = new Device(classes);
462 device->identifier.name = name;
463 mDevices.add(deviceId, device);
464
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000465 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 }
467
468 void removeDevice(int32_t deviceId) {
469 delete mDevices.valueFor(deviceId);
470 mDevices.removeItem(deviceId);
471
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000472 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473 }
474
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700475 bool isDeviceEnabled(int32_t deviceId) {
476 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700477 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700478 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
479 return false;
480 }
481 return device->enabled;
482 }
483
484 status_t enableDevice(int32_t deviceId) {
485 status_t result;
486 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700487 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700488 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
489 return BAD_VALUE;
490 }
491 if (device->enabled) {
492 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
493 return OK;
494 }
495 result = device->enable();
496 return result;
497 }
498
499 status_t disableDevice(int32_t deviceId) {
500 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700501 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700502 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
503 return BAD_VALUE;
504 }
505 if (!device->enabled) {
506 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
507 return OK;
508 }
509 return device->disable();
510 }
511
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000513 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 }
515
516 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
517 Device* device = getDevice(deviceId);
518 device->configuration.addProperty(key, value);
519 }
520
521 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
522 Device* device = getDevice(deviceId);
523 device->configuration.addAll(configuration);
524 }
525
526 void addAbsoluteAxis(int32_t deviceId, int axis,
527 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
528 Device* device = getDevice(deviceId);
529
530 RawAbsoluteAxisInfo info;
531 info.valid = true;
532 info.minValue = minValue;
533 info.maxValue = maxValue;
534 info.flat = flat;
535 info.fuzz = fuzz;
536 info.resolution = resolution;
537 device->absoluteAxes.add(axis, info);
538 }
539
540 void addRelativeAxis(int32_t deviceId, int32_t axis) {
541 Device* device = getDevice(deviceId);
542 device->relativeAxes.add(axis, true);
543 }
544
545 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
546 Device* device = getDevice(deviceId);
547 device->keyCodeStates.replaceValueFor(keyCode, state);
548 }
549
550 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
551 Device* device = getDevice(deviceId);
552 device->scanCodeStates.replaceValueFor(scanCode, state);
553 }
554
555 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
556 Device* device = getDevice(deviceId);
557 device->switchStates.replaceValueFor(switchCode, state);
558 }
559
560 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
561 Device* device = getDevice(deviceId);
562 device->absoluteAxisValue.replaceValueFor(axis, value);
563 }
564
565 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
566 int32_t keyCode, uint32_t flags) {
567 Device* device = getDevice(deviceId);
568 KeyInfo info;
569 info.keyCode = keyCode;
570 info.flags = flags;
571 if (scanCode) {
572 device->keysByScanCode.add(scanCode, info);
573 }
574 if (usageCode) {
575 device->keysByUsageCode.add(usageCode, info);
576 }
577 }
578
579 void addLed(int32_t deviceId, int32_t led, bool initialState) {
580 Device* device = getDevice(deviceId);
581 device->leds.add(led, initialState);
582 }
583
Chris Yef59a2f42020-10-16 12:55:26 -0700584 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
585 int32_t sensorDataIndex) {
586 Device* device = getDevice(deviceId);
587 SensorInfo info;
588 info.sensorType = sensorType;
589 info.sensorDataIndex = sensorDataIndex;
590 device->sensorsByAbsCode.emplace(absCode, info);
591 }
592
593 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
594 Device* device = getDevice(deviceId);
595 typename BitArray<MSC_MAX>::Buffer buffer;
596 buffer[mscEvent / 32] = 1 << mscEvent % 32;
597 device->mscBitmask.loadFromBuffer(buffer);
598 }
599
Chris Ye3fdbfef2021-01-06 18:45:18 -0800600 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
601 mRawLightInfos.emplace(rawId, std::move(info));
602 }
603
604 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
605 mLightBrightness.emplace(rawId, brightness);
606 }
607
608 void fakeLightIntensities(int32_t rawId,
609 const std::unordered_map<LightColor, int32_t> intensities) {
610 mLightIntensities.emplace(rawId, std::move(intensities));
611 }
612
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613 bool getLedState(int32_t deviceId, int32_t led) {
614 Device* device = getDevice(deviceId);
615 return device->leds.valueFor(led);
616 }
617
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100618 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619 return mExcludedDevices;
620 }
621
622 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
623 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800624 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625 }
626
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000627 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
628 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700629 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 RawEvent event;
631 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000632 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800633 event.deviceId = deviceId;
634 event.type = type;
635 event.code = code;
636 event.value = value;
637 mEvents.push_back(event);
638
639 if (type == EV_ABS) {
640 setAbsoluteAxisValue(deviceId, code, value);
641 }
642 }
643
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600644 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
645 std::vector<TouchVideoFrame>> videoFrames) {
646 mVideoFrames = std::move(videoFrames);
647 }
648
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700650 std::unique_lock<std::mutex> lock(mLock);
651 base::ScopedLockAssertion assumeLocked(mLock);
652 const bool queueIsEmpty =
653 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
654 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
655 if (!queueIsEmpty) {
656 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
657 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 }
659
660private:
661 Device* getDevice(int32_t deviceId) const {
662 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100663 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664 }
665
Chris Yea52ade12020-08-27 16:49:20 -0700666 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700668 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 }
670
Chris Yea52ade12020-08-27 16:49:20 -0700671 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672 Device* device = getDevice(deviceId);
673 return device ? device->identifier : InputDeviceIdentifier();
674 }
675
Chris Yea52ade12020-08-27 16:49:20 -0700676 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677
Chris Yea52ade12020-08-27 16:49:20 -0700678 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 Device* device = getDevice(deviceId);
680 if (device) {
681 *outConfiguration = device->configuration;
682 }
683 }
684
Chris Yea52ade12020-08-27 16:49:20 -0700685 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
686 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800688 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 ssize_t index = device->absoluteAxes.indexOfKey(axis);
690 if (index >= 0) {
691 *outAxisInfo = device->absoluteAxes.valueAt(index);
692 return OK;
693 }
694 }
695 outAxisInfo->clear();
696 return -1;
697 }
698
Chris Yea52ade12020-08-27 16:49:20 -0700699 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 Device* device = getDevice(deviceId);
701 if (device) {
702 return device->relativeAxes.indexOfKey(axis) >= 0;
703 }
704 return false;
705 }
706
Chris Yea52ade12020-08-27 16:49:20 -0700707 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708
Chris Yef59a2f42020-10-16 12:55:26 -0700709 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
710 Device* device = getDevice(deviceId);
711 if (device) {
712 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
713 }
714 return false;
715 }
716
Chris Yea52ade12020-08-27 16:49:20 -0700717 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
718 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 Device* device = getDevice(deviceId);
720 if (device) {
721 const KeyInfo* key = getKey(device, scanCode, usageCode);
722 if (key) {
723 if (outKeycode) {
724 *outKeycode = key->keyCode;
725 }
726 if (outFlags) {
727 *outFlags = key->flags;
728 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700729 if (outMetaState) {
730 *outMetaState = metaState;
731 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732 return OK;
733 }
734 }
735 return NAME_NOT_FOUND;
736 }
737
738 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
739 if (usageCode) {
740 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
741 if (index >= 0) {
742 return &device->keysByUsageCode.valueAt(index);
743 }
744 }
745 if (scanCode) {
746 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
747 if (index >= 0) {
748 return &device->keysByScanCode.valueAt(index);
749 }
750 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700751 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 }
753
Chris Yea52ade12020-08-27 16:49:20 -0700754 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755
Chris Yef59a2f42020-10-16 12:55:26 -0700756 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
757 int32_t absCode) {
758 Device* device = getDevice(deviceId);
759 if (!device) {
760 return Errorf("Sensor device not found.");
761 }
762 auto it = device->sensorsByAbsCode.find(absCode);
763 if (it == device->sensorsByAbsCode.end()) {
764 return Errorf("Sensor map not found.");
765 }
766 const SensorInfo& info = it->second;
767 return std::make_pair(info.sensorType, info.sensorDataIndex);
768 }
769
Chris Yea52ade12020-08-27 16:49:20 -0700770 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771 mExcludedDevices = devices;
772 }
773
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000774 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
775 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000777 const size_t filledSize = std::min(mEvents.size(), bufferSize);
778 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
779
780 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700781 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000782 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 }
784
Chris Yea52ade12020-08-27 16:49:20 -0700785 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600786 auto it = mVideoFrames.find(deviceId);
787 if (it != mVideoFrames.end()) {
788 std::vector<TouchVideoFrame> frames = std::move(it->second);
789 mVideoFrames.erase(deviceId);
790 return frames;
791 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800792 return {};
793 }
794
Chris Yea52ade12020-08-27 16:49:20 -0700795 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796 Device* device = getDevice(deviceId);
797 if (device) {
798 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
799 if (index >= 0) {
800 return device->scanCodeStates.valueAt(index);
801 }
802 }
803 return AKEY_STATE_UNKNOWN;
804 }
805
Chris Yea52ade12020-08-27 16:49:20 -0700806 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 Device* device = getDevice(deviceId);
808 if (device) {
809 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
810 if (index >= 0) {
811 return device->keyCodeStates.valueAt(index);
812 }
813 }
814 return AKEY_STATE_UNKNOWN;
815 }
816
Chris Yea52ade12020-08-27 16:49:20 -0700817 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 Device* device = getDevice(deviceId);
819 if (device) {
820 ssize_t index = device->switchStates.indexOfKey(sw);
821 if (index >= 0) {
822 return device->switchStates.valueAt(index);
823 }
824 }
825 return AKEY_STATE_UNKNOWN;
826 }
827
Chris Yea52ade12020-08-27 16:49:20 -0700828 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
829 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800830 Device* device = getDevice(deviceId);
831 if (device) {
832 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
833 if (index >= 0) {
834 *outValue = device->absoluteAxisValue.valueAt(index);
835 return OK;
836 }
837 }
838 *outValue = 0;
839 return -1;
840 }
841
Chris Yea52ade12020-08-27 16:49:20 -0700842 // Return true if the device has non-empty key layout.
843 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
844 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 bool result = false;
846 Device* device = getDevice(deviceId);
847 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700848 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849 for (size_t i = 0; i < numCodes; i++) {
850 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
851 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
852 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853 }
854 }
855 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
856 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
857 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800858 }
859 }
860 }
861 }
862 return result;
863 }
864
Chris Yea52ade12020-08-27 16:49:20 -0700865 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866 Device* device = getDevice(deviceId);
867 if (device) {
868 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
869 return index >= 0;
870 }
871 return false;
872 }
873
Chris Yea52ade12020-08-27 16:49:20 -0700874 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 Device* device = getDevice(deviceId);
876 return device && device->leds.indexOfKey(led) >= 0;
877 }
878
Chris Yea52ade12020-08-27 16:49:20 -0700879 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880 Device* device = getDevice(deviceId);
881 if (device) {
882 ssize_t index = device->leds.indexOfKey(led);
883 if (index >= 0) {
884 device->leds.replaceValueAt(led, on);
885 } else {
886 ADD_FAILURE()
887 << "Attempted to set the state of an LED that the EventHub declared "
888 "was not present. led=" << led;
889 }
890 }
891 }
892
Chris Yea52ade12020-08-27 16:49:20 -0700893 void getVirtualKeyDefinitions(
894 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 outVirtualKeys.clear();
896
897 Device* device = getDevice(deviceId);
898 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800899 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 }
901 }
902
Chris Yea52ade12020-08-27 16:49:20 -0700903 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700904 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 }
906
Chris Yea52ade12020-08-27 16:49:20 -0700907 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908 return false;
909 }
910
Chris Yea52ade12020-08-27 16:49:20 -0700911 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912
Chris Yea52ade12020-08-27 16:49:20 -0700913 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914
Chris Ye87143712020-11-10 05:05:58 +0000915 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
916
Chris Yee2b1e5c2021-03-10 22:45:12 -0800917 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
918 return BATTERY_CAPACITY;
919 }
Kim Low03ea0352020-11-06 12:45:07 -0800920
Chris Yee2b1e5c2021-03-10 22:45:12 -0800921 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
922 return BATTERY_STATUS;
923 }
924
925 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
926
927 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
928 return std::nullopt;
929 }
Kim Low03ea0352020-11-06 12:45:07 -0800930
Chris Ye3fdbfef2021-01-06 18:45:18 -0800931 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
932 std::vector<int32_t> ids;
933 for (const auto& [rawId, info] : mRawLightInfos) {
934 ids.push_back(rawId);
935 }
936 return ids;
937 }
938
939 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
940 auto it = mRawLightInfos.find(lightId);
941 if (it == mRawLightInfos.end()) {
942 return std::nullopt;
943 }
944 return it->second;
945 }
946
947 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
948 mLightBrightness.emplace(lightId, brightness);
949 }
950
951 void setLightIntensities(int32_t deviceId, int32_t lightId,
952 std::unordered_map<LightColor, int32_t> intensities) override {
953 mLightIntensities.emplace(lightId, intensities);
954 };
955
956 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
957 auto lightIt = mLightBrightness.find(lightId);
958 if (lightIt == mLightBrightness.end()) {
959 return std::nullopt;
960 }
961 return lightIt->second;
962 }
963
964 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
965 int32_t deviceId, int32_t lightId) override {
966 auto lightIt = mLightIntensities.find(lightId);
967 if (lightIt == mLightIntensities.end()) {
968 return std::nullopt;
969 }
970 return lightIt->second;
971 };
972
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100973 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 return false;
975 }
976
Chris Yea52ade12020-08-27 16:49:20 -0700977 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800978
Chris Yea52ade12020-08-27 16:49:20 -0700979 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980
Chris Yea52ade12020-08-27 16:49:20 -0700981 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982
Chris Yea52ade12020-08-27 16:49:20 -0700983 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984};
985
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986// --- FakeInputMapper ---
987
988class FakeInputMapper : public InputMapper {
989 uint32_t mSources;
990 int32_t mKeyboardType;
991 int32_t mMetaState;
992 KeyedVector<int32_t, int32_t> mKeyCodeStates;
993 KeyedVector<int32_t, int32_t> mScanCodeStates;
994 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800995 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800996
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700997 std::mutex mLock;
998 std::condition_variable mStateChangedCondition;
999 bool mConfigureWasCalled GUARDED_BY(mLock);
1000 bool mResetWasCalled GUARDED_BY(mLock);
1001 bool mProcessWasCalled GUARDED_BY(mLock);
1002 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003
Arthur Hungc23540e2018-11-29 20:42:11 +08001004 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001006 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1007 : InputMapper(deviceContext),
1008 mSources(sources),
1009 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001011 mConfigureWasCalled(false),
1012 mResetWasCalled(false),
1013 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001014
Chris Yea52ade12020-08-27 16:49:20 -07001015 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016
1017 void setKeyboardType(int32_t keyboardType) {
1018 mKeyboardType = keyboardType;
1019 }
1020
1021 void setMetaState(int32_t metaState) {
1022 mMetaState = metaState;
1023 }
1024
1025 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001026 std::unique_lock<std::mutex> lock(mLock);
1027 base::ScopedLockAssertion assumeLocked(mLock);
1028 const bool configureCalled =
1029 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1030 return mConfigureWasCalled;
1031 });
1032 if (!configureCalled) {
1033 FAIL() << "Expected configure() to have been called.";
1034 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001035 mConfigureWasCalled = false;
1036 }
1037
1038 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001039 std::unique_lock<std::mutex> lock(mLock);
1040 base::ScopedLockAssertion assumeLocked(mLock);
1041 const bool resetCalled =
1042 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1043 return mResetWasCalled;
1044 });
1045 if (!resetCalled) {
1046 FAIL() << "Expected reset() to have been called.";
1047 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048 mResetWasCalled = false;
1049 }
1050
Yi Kong9b14ac62018-07-17 13:48:38 -07001051 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001052 std::unique_lock<std::mutex> lock(mLock);
1053 base::ScopedLockAssertion assumeLocked(mLock);
1054 const bool processCalled =
1055 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1056 return mProcessWasCalled;
1057 });
1058 if (!processCalled) {
1059 FAIL() << "Expected process() to have been called.";
1060 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061 if (outLastEvent) {
1062 *outLastEvent = mLastEvent;
1063 }
1064 mProcessWasCalled = false;
1065 }
1066
1067 void setKeyCodeState(int32_t keyCode, int32_t state) {
1068 mKeyCodeStates.replaceValueFor(keyCode, state);
1069 }
1070
1071 void setScanCodeState(int32_t scanCode, int32_t state) {
1072 mScanCodeStates.replaceValueFor(scanCode, state);
1073 }
1074
1075 void setSwitchState(int32_t switchCode, int32_t state) {
1076 mSwitchStates.replaceValueFor(switchCode, state);
1077 }
1078
1079 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001080 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 }
1082
1083private:
Chris Yea52ade12020-08-27 16:49:20 -07001084 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085
Chris Yea52ade12020-08-27 16:49:20 -07001086 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087 InputMapper::populateDeviceInfo(deviceInfo);
1088
1089 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1090 deviceInfo->setKeyboardType(mKeyboardType);
1091 }
1092 }
1093
Chris Yea52ade12020-08-27 16:49:20 -07001094 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001095 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001096 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001097
1098 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001099 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001100 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1101 mViewport = config->getDisplayViewportByPort(*displayPort);
1102 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001103
1104 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105 }
1106
Chris Yea52ade12020-08-27 16:49:20 -07001107 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001108 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001110 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111 }
1112
Chris Yea52ade12020-08-27 16:49:20 -07001113 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001114 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115 mLastEvent = *rawEvent;
1116 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001117 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118 }
1119
Chris Yea52ade12020-08-27 16:49:20 -07001120 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001121 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1122 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1123 }
1124
Chris Yea52ade12020-08-27 16:49:20 -07001125 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1127 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1128 }
1129
Chris Yea52ade12020-08-27 16:49:20 -07001130 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1132 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1133 }
1134
Chris Yea52ade12020-08-27 16:49:20 -07001135 // Return true if the device has non-empty key layout.
1136 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1137 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138 for (size_t i = 0; i < numCodes; i++) {
1139 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1140 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1141 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001142 }
1143 }
1144 }
Chris Yea52ade12020-08-27 16:49:20 -07001145 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146 return result;
1147 }
1148
1149 virtual int32_t getMetaState() {
1150 return mMetaState;
1151 }
1152
1153 virtual void fadePointer() {
1154 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001155
1156 virtual std::optional<int32_t> getAssociatedDisplay() {
1157 if (mViewport) {
1158 return std::make_optional(mViewport->displayId);
1159 }
1160 return std::nullopt;
1161 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162};
1163
1164
1165// --- InstrumentedInputReader ---
1166
1167class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001168 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001169
1170public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001171 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1172 const sp<InputReaderPolicyInterface>& policy,
1173 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001174 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001176 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001178 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001179
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001180 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001181 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182 InputDeviceIdentifier identifier;
1183 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001184 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001186 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 }
1188
Prabir Pradhan28efc192019-11-05 01:10:04 +00001189 // Make the protected loopOnce method accessible to tests.
1190 using InputReader::loopOnce;
1191
Michael Wrightd02c5b62014-02-10 15:10:22 -08001192protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001193 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1194 const InputDeviceIdentifier& identifier)
1195 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001196 if (!mNextDevices.empty()) {
1197 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1198 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001199 return device;
1200 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001201 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 }
1203
arthurhungdcef2dc2020-08-11 14:47:50 +08001204 // --- FakeInputReaderContext ---
1205 class FakeInputReaderContext : public ContextImpl {
1206 int32_t mGlobalMetaState;
1207 bool mUpdateGlobalMetaStateWasCalled;
1208 int32_t mGeneration;
1209
1210 public:
1211 FakeInputReaderContext(InputReader* reader)
1212 : ContextImpl(reader),
1213 mGlobalMetaState(0),
1214 mUpdateGlobalMetaStateWasCalled(false),
1215 mGeneration(1) {}
1216
1217 virtual ~FakeInputReaderContext() {}
1218
1219 void assertUpdateGlobalMetaStateWasCalled() {
1220 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1221 << "Expected updateGlobalMetaState() to have been called.";
1222 mUpdateGlobalMetaStateWasCalled = false;
1223 }
1224
1225 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1226
1227 uint32_t getGeneration() { return mGeneration; }
1228
1229 void updateGlobalMetaState() override {
1230 mUpdateGlobalMetaStateWasCalled = true;
1231 ContextImpl::updateGlobalMetaState();
1232 }
1233
1234 int32_t getGlobalMetaState() override {
1235 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1236 }
1237
1238 int32_t bumpGeneration() override {
1239 mGeneration = ContextImpl::bumpGeneration();
1240 return mGeneration;
1241 }
1242 } mFakeContext;
1243
Michael Wrightd02c5b62014-02-10 15:10:22 -08001244 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001245
1246public:
1247 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001248};
1249
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001250// --- InputReaderPolicyTest ---
1251class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001252protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001253 sp<FakeInputReaderPolicy> mFakePolicy;
1254
Chris Yea52ade12020-08-27 16:49:20 -07001255 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1256 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001257};
1258
1259/**
1260 * Check that empty set of viewports is an acceptable configuration.
1261 * Also try to get internal viewport two different ways - by type and by uniqueId.
1262 *
1263 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1264 * Such configuration is not currently allowed.
1265 */
1266TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001267 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001268
1269 // We didn't add any viewports yet, so there shouldn't be any.
1270 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001271 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001272 ASSERT_FALSE(internalViewport);
1273
1274 // Add an internal viewport, then clear it
1275 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001276 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001277 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001278
1279 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001280 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001281 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001282 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001283
1284 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001285 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001286 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001287 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001288
1289 mFakePolicy->clearViewports();
1290 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001291 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001292 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001293 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001294 ASSERT_FALSE(internalViewport);
1295}
1296
1297TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1298 const std::string internalUniqueId = "local:0";
1299 const std::string externalUniqueId = "local:1";
1300 const std::string virtualUniqueId1 = "virtual:2";
1301 const std::string virtualUniqueId2 = "virtual:3";
1302 constexpr int32_t virtualDisplayId1 = 2;
1303 constexpr int32_t virtualDisplayId2 = 3;
1304
1305 // Add an internal viewport
1306 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001307 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1308 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001309 // Add an external viewport
1310 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001311 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1312 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001313 // Add an virtual viewport
1314 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001315 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1316 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001317 // Add another virtual viewport
1318 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001319 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1320 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001321
1322 // Check matching by type for internal
1323 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001324 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001325 ASSERT_TRUE(internalViewport);
1326 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1327
1328 // Check matching by type for external
1329 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001330 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001331 ASSERT_TRUE(externalViewport);
1332 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1333
1334 // Check matching by uniqueId for virtual viewport #1
1335 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001336 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001337 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001338 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001339 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1340 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1341
1342 // Check matching by uniqueId for virtual viewport #2
1343 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001344 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001345 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001346 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001347 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1348 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1349}
1350
1351
1352/**
1353 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1354 * that lookup works by checking display id.
1355 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1356 */
1357TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1358 const std::string uniqueId1 = "uniqueId1";
1359 const std::string uniqueId2 = "uniqueId2";
1360 constexpr int32_t displayId1 = 2;
1361 constexpr int32_t displayId2 = 3;
1362
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001363 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1364 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001365 for (const ViewportType& type : types) {
1366 mFakePolicy->clearViewports();
1367 // Add a viewport
1368 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001369 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1370 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001371 // Add another viewport
1372 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001373 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1374 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001375
1376 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001377 std::optional<DisplayViewport> viewport1 =
1378 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379 ASSERT_TRUE(viewport1);
1380 ASSERT_EQ(displayId1, viewport1->displayId);
1381 ASSERT_EQ(type, viewport1->type);
1382
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001383 std::optional<DisplayViewport> viewport2 =
1384 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001385 ASSERT_TRUE(viewport2);
1386 ASSERT_EQ(displayId2, viewport2->displayId);
1387 ASSERT_EQ(type, viewport2->type);
1388
1389 // When there are multiple viewports of the same kind, and uniqueId is not specified
1390 // in the call to getDisplayViewport, then that situation is not supported.
1391 // The viewports can be stored in any order, so we cannot rely on the order, since that
1392 // is just implementation detail.
1393 // However, we can check that it still returns *a* viewport, we just cannot assert
1394 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001395 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396 ASSERT_TRUE(someViewport);
1397 }
1398}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001400/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001401 * When we have multiple internal displays make sure we always return the default display when
1402 * querying by type.
1403 */
1404TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1405 const std::string uniqueId1 = "uniqueId1";
1406 const std::string uniqueId2 = "uniqueId2";
1407 constexpr int32_t nonDefaultDisplayId = 2;
1408 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1409 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1410
1411 // Add the default display first and ensure it gets returned.
1412 mFakePolicy->clearViewports();
1413 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001414 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001415 ViewportType::INTERNAL);
1416 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001417 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001418 ViewportType::INTERNAL);
1419
1420 std::optional<DisplayViewport> viewport =
1421 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1422 ASSERT_TRUE(viewport);
1423 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1424 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1425
1426 // Add the default display second to make sure order doesn't matter.
1427 mFakePolicy->clearViewports();
1428 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001429 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001430 ViewportType::INTERNAL);
1431 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001432 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001433 ViewportType::INTERNAL);
1434
1435 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1436 ASSERT_TRUE(viewport);
1437 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1438 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1439}
1440
1441/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001442 * Check getDisplayViewportByPort
1443 */
1444TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001445 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001446 const std::string uniqueId1 = "uniqueId1";
1447 const std::string uniqueId2 = "uniqueId2";
1448 constexpr int32_t displayId1 = 1;
1449 constexpr int32_t displayId2 = 2;
1450 const uint8_t hdmi1 = 0;
1451 const uint8_t hdmi2 = 1;
1452 const uint8_t hdmi3 = 2;
1453
1454 mFakePolicy->clearViewports();
1455 // Add a viewport that's associated with some display port that's not of interest.
1456 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001457 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1458 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001459 // Add another viewport, connected to HDMI1 port
1460 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001461 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1462 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001463
1464 // Check that correct display viewport was returned by comparing the display ports.
1465 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1466 ASSERT_TRUE(hdmi1Viewport);
1467 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1468 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1469
1470 // Check that we can still get the same viewport using the uniqueId
1471 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1472 ASSERT_TRUE(hdmi1Viewport);
1473 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1474 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1475 ASSERT_EQ(type, hdmi1Viewport->type);
1476
1477 // Check that we cannot find a port with "HDMI2", because we never added one
1478 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1479 ASSERT_FALSE(hdmi2Viewport);
1480}
1481
Michael Wrightd02c5b62014-02-10 15:10:22 -08001482// --- InputReaderTest ---
1483
1484class InputReaderTest : public testing::Test {
1485protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001486 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001487 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001488 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001489 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001490
Chris Yea52ade12020-08-27 16:49:20 -07001491 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001492 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001493 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001494 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001495
Prabir Pradhan28efc192019-11-05 01:10:04 +00001496 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1497 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001498 }
1499
Chris Yea52ade12020-08-27 16:49:20 -07001500 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001501 mFakeListener.clear();
1502 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503 }
1504
Chris Ye1b0c7342020-07-28 21:57:03 -07001505 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001506 const PropertyMap* configuration) {
1507 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001508
1509 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001510 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001511 }
1512 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001513 mReader->loopOnce();
1514 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001515 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1516 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001517 }
1518
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001519 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001520 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001521 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001522 }
1523
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001524 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001525 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001526 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001527 }
1528
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001529 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001530 const std::string& name,
1531 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001532 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001533 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1534 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001535 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001536 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001537 return mapper;
1538 }
1539};
1540
Chris Ye98d3f532020-10-01 21:48:59 -07001541TEST_F(InputReaderTest, PolicyGetInputDevices) {
1542 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1543 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1544 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001545
1546 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001547 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001548 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001549 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001550 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001551 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1552 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001553 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001554}
1555
Chris Yee7310032020-09-22 15:36:28 -07001556TEST_F(InputReaderTest, GetMergedInputDevices) {
1557 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1558 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1559 // Add two subdevices to device
1560 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1561 // Must add at least one mapper or the device will be ignored!
1562 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1563 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1564
1565 // Push same device instance for next device to be added, so they'll have same identifier.
1566 mReader->pushNextDevice(device);
1567 mReader->pushNextDevice(device);
1568 ASSERT_NO_FATAL_FAILURE(
1569 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1570 ASSERT_NO_FATAL_FAILURE(
1571 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1572
1573 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001574 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001575}
1576
Chris Yee14523a2020-12-19 13:46:00 -08001577TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1578 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1579 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1580 // Add two subdevices to device
1581 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1582 // Must add at least one mapper or the device will be ignored!
1583 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1584 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1585
1586 // Push same device instance for next device to be added, so they'll have same identifier.
1587 mReader->pushNextDevice(device);
1588 mReader->pushNextDevice(device);
1589 // Sensor device is initially disabled
1590 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1591 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1592 nullptr));
1593 // Device is disabled because the only sub device is a sensor device and disabled initially.
1594 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1595 ASSERT_FALSE(device->isEnabled());
1596 ASSERT_NO_FATAL_FAILURE(
1597 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1598 // The merged device is enabled if any sub device is enabled
1599 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1600 ASSERT_TRUE(device->isEnabled());
1601}
1602
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001603TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001604 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001605 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 constexpr int32_t eventHubId = 1;
1607 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001608 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001609 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001610 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001611 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001612
Yi Kong9b14ac62018-07-17 13:48:38 -07001613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001614
1615 NotifyDeviceResetArgs resetArgs;
1616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001617 ASSERT_EQ(deviceId, resetArgs.deviceId);
1618
1619 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001620 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001621 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001622
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001624 ASSERT_EQ(deviceId, resetArgs.deviceId);
1625 ASSERT_EQ(device->isEnabled(), false);
1626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001627 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001628 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001631 ASSERT_EQ(device->isEnabled(), false);
1632
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001633 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001634 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001636 ASSERT_EQ(deviceId, resetArgs.deviceId);
1637 ASSERT_EQ(device->isEnabled(), true);
1638}
1639
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001642 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001643 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001644 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001645 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001646 AINPUT_SOURCE_KEYBOARD, nullptr);
1647 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648
1649 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1650 AINPUT_SOURCE_ANY, AKEYCODE_A))
1651 << "Should return unknown when the device id is >= 0 but unknown.";
1652
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1654 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1655 << "Should return unknown when the device id is valid but the sources are not "
1656 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 ASSERT_EQ(AKEY_STATE_DOWN,
1659 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1660 AKEYCODE_A))
1661 << "Should return value provided by mapper when device id is valid and the device "
1662 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001663
1664 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1665 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1666 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1667
1668 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1669 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1670 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1671}
1672
1673TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001674 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001675 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001676 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001677 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001678 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001679 AINPUT_SOURCE_KEYBOARD, nullptr);
1680 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681
1682 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1683 AINPUT_SOURCE_ANY, KEY_A))
1684 << "Should return unknown when the device id is >= 0 but unknown.";
1685
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001686 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1687 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1688 << "Should return unknown when the device id is valid but the sources are not "
1689 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001691 ASSERT_EQ(AKEY_STATE_DOWN,
1692 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1693 KEY_A))
1694 << "Should return value provided by mapper when device id is valid and the device "
1695 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001696
1697 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1698 AINPUT_SOURCE_TRACKBALL, KEY_A))
1699 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1700
1701 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1702 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1703 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1704}
1705
1706TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001707 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001708 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001709 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001710 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001711 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001712 AINPUT_SOURCE_KEYBOARD, nullptr);
1713 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714
1715 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1716 AINPUT_SOURCE_ANY, SW_LID))
1717 << "Should return unknown when the device id is >= 0 but unknown.";
1718
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001719 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1720 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1721 << "Should return unknown when the device id is valid but the sources are not "
1722 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001723
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 ASSERT_EQ(AKEY_STATE_DOWN,
1725 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1726 SW_LID))
1727 << "Should return value provided by mapper when device id is valid and the device "
1728 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729
1730 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1731 AINPUT_SOURCE_TRACKBALL, SW_LID))
1732 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1733
1734 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1735 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1736 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1737}
1738
1739TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001741 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001743 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001744 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001745 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001746
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001747 mapper.addSupportedKeyCode(AKEYCODE_A);
1748 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001749
1750 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1751 uint8_t flags[4] = { 0, 0, 0, 1 };
1752
1753 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1754 << "Should return false when device id is >= 0 but unknown.";
1755 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1756
1757 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001758 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1759 << "Should return false when device id is valid but the sources are not supported by "
1760 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001761 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1762
1763 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001764 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1765 keyCodes, flags))
1766 << "Should return value provided by mapper when device id is valid and the device "
1767 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001768 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1769
1770 flags[3] = 1;
1771 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1772 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1773 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1774
1775 flags[3] = 1;
1776 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1777 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1778 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1779}
1780
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001781TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001782 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001783 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784
1785 NotifyConfigurationChangedArgs args;
1786
1787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1788 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1789}
1790
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001791TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001792 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001793 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001794 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001795 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001796 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001797 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001798 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001799 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001800
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001801 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001802 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1804
1805 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001806 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001807 ASSERT_EQ(when, event.when);
1808 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001809 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001810 ASSERT_EQ(EV_KEY, event.type);
1811 ASSERT_EQ(KEY_A, event.code);
1812 ASSERT_EQ(1, event.value);
1813}
1814
Garfield Tan1c7bc862020-01-28 13:24:04 -08001815TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001817 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001818 constexpr int32_t eventHubId = 1;
1819 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001820 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001822 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001823 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001824
1825 NotifyDeviceResetArgs resetArgs;
1826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001827 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001828
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001829 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001830 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001832 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001833 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001834
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001835 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001836 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001838 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001839 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001840
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001841 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001842 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001844 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001845 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001846}
1847
Garfield Tan1c7bc862020-01-28 13:24:04 -08001848TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1849 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001850 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001851 constexpr int32_t eventHubId = 1;
1852 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1853 // Must add at least one mapper or the device will be ignored!
1854 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001855 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001856 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1857
1858 NotifyDeviceResetArgs resetArgs;
1859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1860 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1861}
1862
Arthur Hungc23540e2018-11-29 20:42:11 +08001863TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001864 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001865 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001866 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001867 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1869 FakeInputMapper& mapper =
1870 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001871 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001872
1873 const uint8_t hdmi1 = 1;
1874
1875 // Associated touch screen with second display.
1876 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1877
1878 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001879 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001880 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001881 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001882 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001883 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001884 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001885 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001886 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001887 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001888
1889 // Add the device, and make sure all of the callbacks are triggered.
1890 // The device is added after the input port associations are processed since
1891 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001892 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001895 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001896
Arthur Hung2c9a3342019-07-23 14:18:59 +08001897 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001898 ASSERT_EQ(deviceId, device->getId());
1899 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1900 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001901
1902 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001903 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001904 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001905 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001906}
1907
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001908TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1909 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1910 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1911 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1912 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1913 // Must add at least one mapper or the device will be ignored!
1914 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1915 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1916 mReader->pushNextDevice(device);
1917 mReader->pushNextDevice(device);
1918 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1919 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1920
1921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1922
1923 NotifyDeviceResetArgs resetArgs;
1924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1925 ASSERT_EQ(deviceId, resetArgs.deviceId);
1926 ASSERT_TRUE(device->isEnabled());
1927 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1928 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1929
1930 disableDevice(deviceId);
1931 mReader->loopOnce();
1932
1933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1934 ASSERT_EQ(deviceId, resetArgs.deviceId);
1935 ASSERT_FALSE(device->isEnabled());
1936 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1937 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1938
1939 enableDevice(deviceId);
1940 mReader->loopOnce();
1941
1942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1943 ASSERT_EQ(deviceId, resetArgs.deviceId);
1944 ASSERT_TRUE(device->isEnabled());
1945 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1946 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1947}
1948
1949TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1950 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1951 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1952 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1953 // Add two subdevices to device
1954 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1955 FakeInputMapper& mapperDevice1 =
1956 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1957 FakeInputMapper& mapperDevice2 =
1958 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1959 mReader->pushNextDevice(device);
1960 mReader->pushNextDevice(device);
1961 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1962 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1963
1964 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1965 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1966
1967 ASSERT_EQ(AKEY_STATE_DOWN,
1968 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1969 ASSERT_EQ(AKEY_STATE_DOWN,
1970 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1971 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1972 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1973}
1974
Prabir Pradhan7e186182020-11-10 13:56:45 -08001975TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1976 NotifyPointerCaptureChangedArgs args;
1977
1978 mFakePolicy->setPointerCapture(true);
1979 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1980 mReader->loopOnce();
1981 mFakeListener->assertNotifyCaptureWasCalled(&args);
1982 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1983
1984 mFakePolicy->setPointerCapture(false);
1985 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1986 mReader->loopOnce();
1987 mFakeListener->assertNotifyCaptureWasCalled(&args);
1988 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1989
1990 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1991 // does not change.
1992 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1993 mReader->loopOnce();
1994 mFakeListener->assertNotifyCaptureWasCalled(&args);
1995 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1996}
1997
Chris Ye87143712020-11-10 05:05:58 +00001998class FakeVibratorInputMapper : public FakeInputMapper {
1999public:
2000 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2001 : FakeInputMapper(deviceContext, sources) {}
2002
2003 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2004};
2005
2006TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2007 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2008 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2009 constexpr int32_t eventHubId = 1;
2010 const char* DEVICE_LOCATION = "BLUETOOTH";
2011 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2012 FakeVibratorInputMapper& mapper =
2013 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2014 mReader->pushNextDevice(device);
2015
2016 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2017 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2018
2019 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2020 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2021}
2022
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002023// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002024
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002025class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002026public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002027 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002028
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002029 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002030
2031 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2032
2033 void dump(std::string& dump) override {}
2034
2035 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2036 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002037 }
2038
Chris Yee2b1e5c2021-03-10 22:45:12 -08002039 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2040 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002041 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002042
2043 bool setLightColor(int32_t lightId, int32_t color) override {
2044 getDeviceContext().setLightBrightness(lightId, color >> 24);
2045 return true;
2046 }
2047
2048 std::optional<int32_t> getLightColor(int32_t lightId) override {
2049 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2050 if (!result.has_value()) {
2051 return std::nullopt;
2052 }
2053 return result.value() << 24;
2054 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002055
2056 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2057
2058 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2059
2060private:
2061 InputDeviceContext& mDeviceContext;
2062 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2063 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002064};
2065
Chris Yee2b1e5c2021-03-10 22:45:12 -08002066TEST_F(InputReaderTest, BatteryGetCapacity) {
2067 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2068 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2069 constexpr int32_t eventHubId = 1;
2070 const char* DEVICE_LOCATION = "BLUETOOTH";
2071 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002072 FakePeripheralController& controller =
2073 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002074 mReader->pushNextDevice(device);
2075
2076 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2077
2078 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2079 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2080}
2081
2082TEST_F(InputReaderTest, BatteryGetStatus) {
2083 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2084 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2085 constexpr int32_t eventHubId = 1;
2086 const char* DEVICE_LOCATION = "BLUETOOTH";
2087 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002088 FakePeripheralController& controller =
2089 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002090 mReader->pushNextDevice(device);
2091
2092 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2093
2094 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2095 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2096}
2097
Chris Ye3fdbfef2021-01-06 18:45:18 -08002098TEST_F(InputReaderTest, LightGetColor) {
2099 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2100 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2101 constexpr int32_t eventHubId = 1;
2102 const char* DEVICE_LOCATION = "BLUETOOTH";
2103 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002104 FakePeripheralController& controller =
2105 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002106 mReader->pushNextDevice(device);
2107 RawLightInfo info = {.id = 1,
2108 .name = "Mono",
2109 .maxBrightness = 255,
2110 .flags = InputLightClass::BRIGHTNESS,
2111 .path = ""};
2112 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2113 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2114
2115 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002116
Chris Yee2b1e5c2021-03-10 22:45:12 -08002117 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2118 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002119 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2120 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2121}
2122
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002123// --- InputReaderIntegrationTest ---
2124
2125// These tests create and interact with the InputReader only through its interface.
2126// The InputReader is started during SetUp(), which starts its processing in its own
2127// thread. The tests use linux uinput to emulate input devices.
2128// NOTE: Interacting with the physical device while these tests are running may cause
2129// the tests to fail.
2130class InputReaderIntegrationTest : public testing::Test {
2131protected:
2132 sp<TestInputListener> mTestListener;
2133 sp<FakeInputReaderPolicy> mFakePolicy;
2134 sp<InputReaderInterface> mReader;
2135
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002136 std::shared_ptr<FakePointerController> mFakePointerController;
2137
Chris Yea52ade12020-08-27 16:49:20 -07002138 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002139 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002140 mFakePointerController = std::make_shared<FakePointerController>();
2141 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002142 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2143 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002144
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002145 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002146 ASSERT_EQ(mReader->start(), OK);
2147
2148 // Since this test is run on a real device, all the input devices connected
2149 // to the test device will show up in mReader. We wait for those input devices to
2150 // show up before beginning the tests.
2151 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2152 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2153 }
2154
Chris Yea52ade12020-08-27 16:49:20 -07002155 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002156 ASSERT_EQ(mReader->stop(), OK);
2157 mTestListener.clear();
2158 mFakePolicy.clear();
2159 }
2160};
2161
2162TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2163 // An invalid input device that is only used for this test.
2164 class InvalidUinputDevice : public UinputDevice {
2165 public:
2166 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2167
2168 private:
2169 void configureDevice(int fd, uinput_user_dev* device) override {}
2170 };
2171
2172 const size_t numDevices = mFakePolicy->getInputDevices().size();
2173
2174 // UinputDevice does not set any event or key bits, so InputReader should not
2175 // consider it as a valid device.
2176 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2177 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2178 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2179 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2180
2181 invalidDevice.reset();
2182 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2183 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2184 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2185}
2186
2187TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2188 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2189
2190 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2191 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2192 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2193 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2194
2195 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002196 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002197 const auto& it =
2198 std::find_if(inputDevices.begin(), inputDevices.end(),
2199 [&keyboard](const InputDeviceInfo& info) {
2200 return info.getIdentifier().name == keyboard->getName();
2201 });
2202
2203 ASSERT_NE(it, inputDevices.end());
2204 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2205 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2206 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002207
2208 keyboard.reset();
2209 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2210 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2211 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2212}
2213
2214TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2215 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2216 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2217
2218 NotifyConfigurationChangedArgs configChangedArgs;
2219 ASSERT_NO_FATAL_FAILURE(
2220 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002221 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002222 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2223
2224 NotifyKeyArgs keyArgs;
2225 keyboard->pressAndReleaseHomeKey();
2226 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2227 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002228 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002229 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002230 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002231 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002232 prevTimestamp = keyArgs.eventTime;
2233
2234 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2235 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002236 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002237 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002238 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002239}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002240
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002241/**
2242 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2243 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2244 * are passed to the listener.
2245 */
2246static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2247TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2248 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2249 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2250 NotifyKeyArgs keyArgs;
2251
2252 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
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_DOWN, keyArgs.scanCode);
2256
2257 controller->pressAndReleaseKey(BTN_GEAR_UP);
2258 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2259 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2260 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2261}
2262
Arthur Hungaab25622020-01-16 11:22:11 +08002263// --- TouchProcessTest ---
2264class TouchIntegrationTest : public InputReaderIntegrationTest {
2265protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002266 const std::string UNIQUE_ID = "local:0";
2267
Chris Yea52ade12020-08-27 16:49:20 -07002268 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002269 InputReaderIntegrationTest::SetUp();
2270 // At least add an internal display.
2271 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2272 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002273 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002274
2275 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2276 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2277 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2278 }
2279
2280 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2281 int32_t orientation, const std::string& uniqueId,
2282 std::optional<uint8_t> physicalPort,
2283 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002284 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2285 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002286 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2287 }
2288
2289 std::unique_ptr<UinputTouchScreen> mDevice;
2290};
2291
2292TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2293 NotifyMotionArgs args;
2294 const Point centerPoint = mDevice->getCenterPoint();
2295
2296 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002297 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002298 mDevice->sendDown(centerPoint);
2299 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2300 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2301
2302 // ACTION_MOVE
2303 mDevice->sendMove(centerPoint + Point(1, 1));
2304 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2306
2307 // ACTION_UP
2308 mDevice->sendUp();
2309 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2310 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2311}
2312
2313TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2314 NotifyMotionArgs args;
2315 const Point centerPoint = mDevice->getCenterPoint();
2316
2317 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002318 mDevice->sendSlot(FIRST_SLOT);
2319 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002320 mDevice->sendDown(centerPoint);
2321 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2322 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2323
2324 // ACTION_POINTER_DOWN (Second slot)
2325 const Point secondPoint = centerPoint + Point(100, 100);
2326 mDevice->sendSlot(SECOND_SLOT);
2327 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2328 mDevice->sendDown(secondPoint + Point(1, 1));
2329 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2330 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2331 args.action);
2332
2333 // ACTION_MOVE (Second slot)
2334 mDevice->sendMove(secondPoint);
2335 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2337
2338 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002339 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002340 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002341 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002342 args.action);
2343
2344 // ACTION_UP
2345 mDevice->sendSlot(FIRST_SLOT);
2346 mDevice->sendUp();
2347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2348 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2349}
2350
2351TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2352 NotifyMotionArgs args;
2353 const Point centerPoint = mDevice->getCenterPoint();
2354
2355 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002356 mDevice->sendSlot(FIRST_SLOT);
2357 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002358 mDevice->sendDown(centerPoint);
2359 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2360 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2361
arthurhungcc7f9802020-04-30 17:55:40 +08002362 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002363 const Point secondPoint = centerPoint + Point(100, 100);
2364 mDevice->sendSlot(SECOND_SLOT);
2365 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2366 mDevice->sendDown(secondPoint);
2367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2368 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2369 args.action);
2370
arthurhungcc7f9802020-04-30 17:55:40 +08002371 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002372 mDevice->sendMove(secondPoint + Point(1, 1));
2373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2375
arthurhungcc7f9802020-04-30 17:55:40 +08002376 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2377 // a palm event.
2378 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002379 mDevice->sendToolType(MT_TOOL_PALM);
2380 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002381 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2382 args.action);
2383 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002384
arthurhungcc7f9802020-04-30 17:55:40 +08002385 // Send up to second slot, expect first slot send moving.
2386 mDevice->sendPointerUp();
2387 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002389
arthurhungcc7f9802020-04-30 17:55:40 +08002390 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002391 mDevice->sendSlot(FIRST_SLOT);
2392 mDevice->sendUp();
2393
arthurhungcc7f9802020-04-30 17:55:40 +08002394 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2395 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002396}
2397
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002399class InputDeviceTest : public testing::Test {
2400protected:
2401 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002402 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403 static const int32_t DEVICE_ID;
2404 static const int32_t DEVICE_GENERATION;
2405 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002406 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002407 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002409 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002410 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002411 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002412 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002413 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414
Chris Yea52ade12020-08-27 16:49:20 -07002415 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002416 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002418 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002419 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2420 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002421 InputDeviceIdentifier identifier;
2422 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002423 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002424 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002425 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002426 mReader->pushNextDevice(mDevice);
2427 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2428 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002429 }
2430
Chris Yea52ade12020-08-27 16:49:20 -07002431 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432 mFakeListener.clear();
2433 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434 }
2435};
2436
2437const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002438const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002439const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002440const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2441const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002442const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2443 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002444const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445
2446TEST_F(InputDeviceTest, ImmutableProperties) {
2447 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002448 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002449 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450}
2451
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002452TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2453 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002454}
2455
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2457 // Configuration.
2458 InputReaderConfiguration config;
2459 mDevice->configure(ARBITRARY_TIME, &config, 0);
2460
2461 // Reset.
2462 mDevice->reset(ARBITRARY_TIME);
2463
2464 NotifyDeviceResetArgs resetArgs;
2465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2466 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2467 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2468
2469 // Metadata.
2470 ASSERT_TRUE(mDevice->isIgnored());
2471 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2472
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002473 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002475 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2477 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2478
2479 // State queries.
2480 ASSERT_EQ(0, mDevice->getMetaState());
2481
2482 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2483 << "Ignored device should return unknown key code state.";
2484 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2485 << "Ignored device should return unknown scan code state.";
2486 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2487 << "Ignored device should return unknown switch state.";
2488
2489 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2490 uint8_t flags[2] = { 0, 1 };
2491 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2492 << "Ignored device should never mark any key codes.";
2493 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2494 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2495}
2496
2497TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2498 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002499 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002500
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002501 FakeInputMapper& mapper1 =
2502 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002503 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2504 mapper1.setMetaState(AMETA_ALT_ON);
2505 mapper1.addSupportedKeyCode(AKEYCODE_A);
2506 mapper1.addSupportedKeyCode(AKEYCODE_B);
2507 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2508 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2509 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2510 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2511 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002512
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002513 FakeInputMapper& mapper2 =
2514 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002515 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516
2517 InputReaderConfiguration config;
2518 mDevice->configure(ARBITRARY_TIME, &config, 0);
2519
2520 String8 propertyValue;
2521 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2522 << "Device should have read configuration during configuration phase.";
2523 ASSERT_STREQ("value", propertyValue.string());
2524
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002525 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2526 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002527
2528 // Reset
2529 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002530 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2531 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002532
2533 NotifyDeviceResetArgs resetArgs;
2534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2535 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2536 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2537
2538 // Metadata.
2539 ASSERT_FALSE(mDevice->isIgnored());
2540 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2541
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002542 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002544 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2546 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2547
2548 // State queries.
2549 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2550 << "Should query mappers and combine meta states.";
2551
2552 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2553 << "Should return unknown key code state when source not supported.";
2554 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2555 << "Should return unknown scan code state when source not supported.";
2556 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2557 << "Should return unknown switch state when source not supported.";
2558
2559 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2560 << "Should query mapper when source is supported.";
2561 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2562 << "Should query mapper when source is supported.";
2563 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2564 << "Should query mapper when source is supported.";
2565
2566 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2567 uint8_t flags[4] = { 0, 0, 0, 1 };
2568 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2569 << "Should do nothing when source is unsupported.";
2570 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2571 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2572 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2573 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2574
2575 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2576 << "Should query mapper when source is supported.";
2577 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2578 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2579 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2580 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2581
2582 // Event handling.
2583 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002584 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002585 mDevice->process(&event, 1);
2586
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002587 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2588 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002589}
2590
Arthur Hung2c9a3342019-07-23 14:18:59 +08002591// A single input device is associated with a specific display. Check that:
2592// 1. Device is disabled if the viewport corresponding to the associated display is not found
2593// 2. Device is disabled when setEnabled API is called
2594TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002595 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002596
2597 // First Configuration.
2598 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2599
2600 // Device should be enabled by default.
2601 ASSERT_TRUE(mDevice->isEnabled());
2602
2603 // Prepare associated info.
2604 constexpr uint8_t hdmi = 1;
2605 const std::string UNIQUE_ID = "local:1";
2606
2607 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2608 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2609 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2610 // Device should be disabled because it is associated with a specific display via
2611 // input port <-> display port association, but the corresponding display is not found
2612 ASSERT_FALSE(mDevice->isEnabled());
2613
2614 // Prepare displays.
2615 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002616 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2617 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002618 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2619 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2620 ASSERT_TRUE(mDevice->isEnabled());
2621
2622 // Device should be disabled after set disable.
2623 mFakePolicy->addDisabledDevice(mDevice->getId());
2624 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2625 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2626 ASSERT_FALSE(mDevice->isEnabled());
2627
2628 // Device should still be disabled even found the associated display.
2629 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2630 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2631 ASSERT_FALSE(mDevice->isEnabled());
2632}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002633
Christine Franks1ba71cc2021-04-07 14:37:42 -07002634TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2635 // Device should be enabled by default.
2636 mFakePolicy->clearViewports();
2637 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2638 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2639 ASSERT_TRUE(mDevice->isEnabled());
2640
2641 // Device should be disabled because it is associated with a specific display, but the
2642 // corresponding display is not found.
2643 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2644 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2645 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2646 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2647 ASSERT_FALSE(mDevice->isEnabled());
2648
2649 // Device should be enabled when a display is found.
2650 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2651 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2652 NO_PORT, ViewportType::INTERNAL);
2653 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2654 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2655 ASSERT_TRUE(mDevice->isEnabled());
2656
2657 // Device should be disabled after set disable.
2658 mFakePolicy->addDisabledDevice(mDevice->getId());
2659 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2660 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2661 ASSERT_FALSE(mDevice->isEnabled());
2662
2663 // Device should still be disabled even found the associated display.
2664 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2665 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2666 ASSERT_FALSE(mDevice->isEnabled());
2667}
2668
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669// --- InputMapperTest ---
2670
2671class InputMapperTest : public testing::Test {
2672protected:
2673 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002674 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675 static const int32_t DEVICE_ID;
2676 static const int32_t DEVICE_GENERATION;
2677 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002678 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002679 static const int32_t EVENTHUB_ID;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002680 static const std::optional<bool> INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002682 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002684 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002685 std::unique_ptr<InstrumentedInputReader> mReader;
2686 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687
Chris Ye1b0c7342020-07-28 21:57:03 -07002688 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002689 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002691 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002692 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2693 mFakeListener);
2694 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695 }
2696
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002697 void SetUp() override {
2698 // Ensure per_window_input_rotation is enabled.
2699 sysprop::InputFlingerProperties::per_window_input_rotation(true);
2700
2701 SetUp(DEVICE_CLASSES);
2702 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002703
Chris Yea52ade12020-08-27 16:49:20 -07002704 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705 mFakeListener.clear();
2706 mFakePolicy.clear();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002707
2708 sysprop::InputFlingerProperties::per_window_input_rotation(
2709 INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710 }
2711
2712 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002713 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002714 }
2715
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002716 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002717 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002718 mReader->requestRefreshConfiguration(changes);
2719 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002720 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002721 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2722 }
2723
arthurhungdcef2dc2020-08-11 14:47:50 +08002724 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2725 const std::string& location, int32_t eventHubId,
2726 Flags<InputDeviceClass> classes) {
2727 InputDeviceIdentifier identifier;
2728 identifier.name = name;
2729 identifier.location = location;
2730 std::shared_ptr<InputDevice> device =
2731 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2732 identifier);
2733 mReader->pushNextDevice(device);
2734 mFakeEventHub->addDevice(eventHubId, name, classes);
2735 mReader->loopOnce();
2736 return device;
2737 }
2738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002739 template <class T, typename... Args>
2740 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002741 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002742 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002743 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002744 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002745 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002746 }
2747
2748 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002749 int32_t orientation, const std::string& uniqueId,
2750 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002751 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2752 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002753 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2754 }
2755
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002756 void clearViewports() {
2757 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002758 }
2759
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002760 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2761 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002762 RawEvent event;
2763 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002764 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002765 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766 event.type = type;
2767 event.code = code;
2768 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002769 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002770 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771 }
2772
2773 static void assertMotionRange(const InputDeviceInfo& info,
2774 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2775 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002776 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002777 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2778 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2779 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2780 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2781 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2782 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2783 }
2784
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002785 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2786 float size, float touchMajor, float touchMinor, float toolMajor,
2787 float toolMinor, float orientation, float distance,
2788 float scaledAxisEpsilon = 1.f) {
2789 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2790 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2792 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002793 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2794 scaledAxisEpsilon);
2795 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2796 scaledAxisEpsilon);
2797 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2798 scaledAxisEpsilon);
2799 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2800 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002801 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2802 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2803 }
2804
Michael Wright17db18e2020-06-26 20:51:44 +01002805 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002807 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 ASSERT_NEAR(x, actualX, 1);
2809 ASSERT_NEAR(y, actualY, 1);
2810 }
2811};
2812
2813const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002814const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002815const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2817const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002818const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2819 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002820const int32_t InputMapperTest::EVENTHUB_ID = 1;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002821const std::optional<bool> InputMapperTest::INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE =
2822 sysprop::InputFlingerProperties::per_window_input_rotation();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002823
2824// --- SwitchInputMapperTest ---
2825
2826class SwitchInputMapperTest : public InputMapperTest {
2827protected:
2828};
2829
2830TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002831 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002833 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834}
2835
2836TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002837 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002839 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002840 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002842 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002843 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844}
2845
2846TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002847 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002853
2854 NotifySwitchArgs args;
2855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2856 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002857 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2858 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859 args.switchMask);
2860 ASSERT_EQ(uint32_t(0), args.policyFlags);
2861}
2862
Chris Ye87143712020-11-10 05:05:58 +00002863// --- VibratorInputMapperTest ---
2864class VibratorInputMapperTest : public InputMapperTest {
2865protected:
2866 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2867};
2868
2869TEST_F(VibratorInputMapperTest, GetSources) {
2870 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2871
2872 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2873}
2874
2875TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2876 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2877
2878 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2879}
2880
2881TEST_F(VibratorInputMapperTest, Vibrate) {
2882 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002883 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002884 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2885
2886 VibrationElement pattern(2);
2887 VibrationSequence sequence(2);
2888 pattern.duration = std::chrono::milliseconds(200);
2889 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2890 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2891 sequence.addElement(pattern);
2892 pattern.duration = std::chrono::milliseconds(500);
2893 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2894 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2895 sequence.addElement(pattern);
2896
2897 std::vector<int64_t> timings = {0, 1};
2898 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2899
2900 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002901 // Start vibrating
2902 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002903 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002904 // Verify vibrator state listener was notified.
2905 mReader->loopOnce();
2906 NotifyVibratorStateArgs args;
2907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2908 ASSERT_EQ(DEVICE_ID, args.deviceId);
2909 ASSERT_TRUE(args.isOn);
2910 // Stop vibrating
2911 mapper.cancelVibrate(VIBRATION_TOKEN);
2912 ASSERT_FALSE(mapper.isVibrating());
2913 // Verify vibrator state listener was notified.
2914 mReader->loopOnce();
2915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2916 ASSERT_EQ(DEVICE_ID, args.deviceId);
2917 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002918}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002919
Chris Yef59a2f42020-10-16 12:55:26 -07002920// --- SensorInputMapperTest ---
2921
2922class SensorInputMapperTest : public InputMapperTest {
2923protected:
2924 static const int32_t ACCEL_RAW_MIN;
2925 static const int32_t ACCEL_RAW_MAX;
2926 static const int32_t ACCEL_RAW_FUZZ;
2927 static const int32_t ACCEL_RAW_FLAT;
2928 static const int32_t ACCEL_RAW_RESOLUTION;
2929
2930 static const int32_t GYRO_RAW_MIN;
2931 static const int32_t GYRO_RAW_MAX;
2932 static const int32_t GYRO_RAW_FUZZ;
2933 static const int32_t GYRO_RAW_FLAT;
2934 static const int32_t GYRO_RAW_RESOLUTION;
2935
2936 static const float GRAVITY_MS2_UNIT;
2937 static const float DEGREE_RADIAN_UNIT;
2938
2939 void prepareAccelAxes();
2940 void prepareGyroAxes();
2941 void setAccelProperties();
2942 void setGyroProperties();
2943 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2944};
2945
2946const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2947const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2948const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2949const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2950const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2951
2952const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2953const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2954const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2955const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2956const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2957
2958const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2959const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2960
2961void SensorInputMapperTest::prepareAccelAxes() {
2962 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2963 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2964 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2965 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2966 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2967 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2968}
2969
2970void SensorInputMapperTest::prepareGyroAxes() {
2971 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2972 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2973 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2974 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2975 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2976 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2977}
2978
2979void SensorInputMapperTest::setAccelProperties() {
2980 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2981 /* sensorDataIndex */ 0);
2982 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2983 /* sensorDataIndex */ 1);
2984 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2985 /* sensorDataIndex */ 2);
2986 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2987 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2988 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2989 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2990 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2991}
2992
2993void SensorInputMapperTest::setGyroProperties() {
2994 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2995 /* sensorDataIndex */ 0);
2996 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2997 /* sensorDataIndex */ 1);
2998 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2999 /* sensorDataIndex */ 2);
3000 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3001 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3002 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3003 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3004 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3005}
3006
3007TEST_F(SensorInputMapperTest, GetSources) {
3008 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3009
3010 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3011}
3012
3013TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3014 setAccelProperties();
3015 prepareAccelAxes();
3016 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3017
3018 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3019 std::chrono::microseconds(10000),
3020 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003021 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003027
3028 NotifySensorArgs args;
3029 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3030 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3031 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3032
3033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3034 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3035 ASSERT_EQ(args.deviceId, DEVICE_ID);
3036 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3037 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3038 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3039 ASSERT_EQ(args.values, values);
3040 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3041}
3042
3043TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3044 setGyroProperties();
3045 prepareGyroAxes();
3046 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3047
3048 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3049 std::chrono::microseconds(10000),
3050 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003051 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003057
3058 NotifySensorArgs args;
3059 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3060 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3061 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3062
3063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3064 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3065 ASSERT_EQ(args.deviceId, DEVICE_ID);
3066 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3067 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3068 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3069 ASSERT_EQ(args.values, values);
3070 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3071}
3072
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073// --- KeyboardInputMapperTest ---
3074
3075class KeyboardInputMapperTest : public InputMapperTest {
3076protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003077 const std::string UNIQUE_ID = "local:0";
3078
3079 void prepareDisplay(int32_t orientation);
3080
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003082 int32_t originalKeyCode, int32_t rotatedKeyCode,
3083 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084};
3085
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003086/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3087 * orientation.
3088 */
3089void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003090 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3091 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003092}
3093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003094void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003095 int32_t originalScanCode, int32_t originalKeyCode,
3096 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097 NotifyKeyArgs args;
3098
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3101 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3102 ASSERT_EQ(originalScanCode, args.scanCode);
3103 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003104 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3108 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3109 ASSERT_EQ(originalScanCode, args.scanCode);
3110 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003111 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112}
3113
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003115 KeyboardInputMapper& mapper =
3116 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3117 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003119 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120}
3121
3122TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3123 const int32_t USAGE_A = 0x070004;
3124 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003125 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3126 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003127 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3128 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3129 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003131 KeyboardInputMapper& mapper =
3132 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3133 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003134 // Initial metastate to AMETA_NONE.
3135 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3136 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137
3138 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140 NotifyKeyArgs args;
3141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3142 ASSERT_EQ(DEVICE_ID, args.deviceId);
3143 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3144 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3146 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3147 ASSERT_EQ(KEY_HOME, args.scanCode);
3148 ASSERT_EQ(AMETA_NONE, args.metaState);
3149 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3150 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3151 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3152
3153 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003154 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3156 ASSERT_EQ(DEVICE_ID, args.deviceId);
3157 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3158 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3159 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3160 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3161 ASSERT_EQ(KEY_HOME, args.scanCode);
3162 ASSERT_EQ(AMETA_NONE, args.metaState);
3163 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3164 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3165 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3166
3167 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3171 ASSERT_EQ(DEVICE_ID, args.deviceId);
3172 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3173 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3174 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3175 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3176 ASSERT_EQ(0, args.scanCode);
3177 ASSERT_EQ(AMETA_NONE, args.metaState);
3178 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3179 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3180 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3181
3182 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3184 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3186 ASSERT_EQ(DEVICE_ID, args.deviceId);
3187 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3188 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3189 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3190 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3191 ASSERT_EQ(0, args.scanCode);
3192 ASSERT_EQ(AMETA_NONE, args.metaState);
3193 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3194 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3195 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3196
3197 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3201 ASSERT_EQ(DEVICE_ID, args.deviceId);
3202 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3203 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3204 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3205 ASSERT_EQ(0, args.keyCode);
3206 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3207 ASSERT_EQ(AMETA_NONE, args.metaState);
3208 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3209 ASSERT_EQ(0U, args.policyFlags);
3210 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3211
3212 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3214 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3216 ASSERT_EQ(DEVICE_ID, args.deviceId);
3217 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3218 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3219 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3220 ASSERT_EQ(0, args.keyCode);
3221 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3222 ASSERT_EQ(AMETA_NONE, args.metaState);
3223 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3224 ASSERT_EQ(0U, args.policyFlags);
3225 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3226}
3227
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003228/**
3229 * Ensure that the readTime is set to the time when the EV_KEY is received.
3230 */
3231TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3232 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3233
3234 KeyboardInputMapper& mapper =
3235 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3236 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3237 NotifyKeyArgs args;
3238
3239 // Key down
3240 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3242 ASSERT_EQ(12, args.readTime);
3243
3244 // Key up
3245 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3247 ASSERT_EQ(15, args.readTime);
3248}
3249
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003251 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3252 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003253 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3254 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3255 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003257 KeyboardInputMapper& mapper =
3258 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3259 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260
arthurhungc903df12020-08-11 15:08:42 +08003261 // Initial metastate to AMETA_NONE.
3262 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3263 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264
3265 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 NotifyKeyArgs args;
3268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3269 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003270 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003271 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272
3273 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003274 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003277 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278
3279 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003280 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3282 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003283 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284
3285 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003286 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3288 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003289 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003290 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291}
3292
3293TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003294 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3295 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3296 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3297 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003299 KeyboardInputMapper& mapper =
3300 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3301 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003303 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3305 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3306 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3307 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3308 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3309 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3310 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3311 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3312}
3313
3314TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003315 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3316 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3317 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3318 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003321 KeyboardInputMapper& mapper =
3322 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3323 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003325 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003326 ASSERT_NO_FATAL_FAILURE(
3327 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3328 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3329 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3330 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3331 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3332 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3333 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003335 clearViewports();
3336 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003337 ASSERT_NO_FATAL_FAILURE(
3338 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3339 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3340 AKEYCODE_DPAD_UP, DISPLAY_ID));
3341 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3342 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3343 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3344 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003346 clearViewports();
3347 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003348 ASSERT_NO_FATAL_FAILURE(
3349 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3350 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3351 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3352 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3353 AKEYCODE_DPAD_UP, DISPLAY_ID));
3354 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3355 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003357 clearViewports();
3358 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003359 ASSERT_NO_FATAL_FAILURE(
3360 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3361 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3362 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3363 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3364 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3365 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3366 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367
3368 // Special case: if orientation changes while key is down, we still emit the same keycode
3369 // in the key up as we did in the key down.
3370 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003371 clearViewports();
3372 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3375 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3376 ASSERT_EQ(KEY_UP, args.scanCode);
3377 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3378
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003379 clearViewports();
3380 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3383 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3384 ASSERT_EQ(KEY_UP, args.scanCode);
3385 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3386}
3387
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003388TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3389 // If the keyboard is not orientation aware,
3390 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003391 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003393 KeyboardInputMapper& mapper =
3394 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3395 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003396 NotifyKeyArgs args;
3397
3398 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3403 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3404
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003405 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3410 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3411}
3412
3413TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3414 // If the keyboard is orientation aware,
3415 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003416 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003417
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003418 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003419 KeyboardInputMapper& mapper =
3420 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3421 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003422 NotifyKeyArgs args;
3423
3424 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3425 // ^--- already checked by the previous test
3426
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003427 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003428 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3433 ASSERT_EQ(DISPLAY_ID, args.displayId);
3434
3435 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003436 clearViewports();
3437 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003438 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3443 ASSERT_EQ(newDisplayId, args.displayId);
3444}
3445
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003447 KeyboardInputMapper& mapper =
3448 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3449 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003450
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003451 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003452 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003454 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003455 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456}
3457
3458TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003459 KeyboardInputMapper& mapper =
3460 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3461 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003463 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003464 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003466 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003467 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468}
3469
3470TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003471 KeyboardInputMapper& mapper =
3472 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3473 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003475 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476
3477 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3478 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003479 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480 ASSERT_TRUE(flags[0]);
3481 ASSERT_FALSE(flags[1]);
3482}
3483
3484TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003485 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3486 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3487 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3488 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3489 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3490 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003492 KeyboardInputMapper& mapper =
3493 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3494 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003495 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003496 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3497 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498
3499 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003500 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3501 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3502 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
3504 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003507 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3508 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3509 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003510 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
3512 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003515 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3516 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3517 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519
3520 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003523 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3524 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3525 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527
3528 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003531 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3532 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3533 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003534 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535
3536 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003539 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3540 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3541 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003542 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543
3544 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003547 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3548 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3549 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003550 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551}
3552
Chris Yea52ade12020-08-27 16:49:20 -07003553TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3554 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3555 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3556 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3557 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3558
3559 KeyboardInputMapper& mapper =
3560 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3561 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3562
3563 // Initial metastate should be AMETA_NONE as no meta keys added.
3564 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3565 // Meta state should be AMETA_NONE after reset
3566 mapper.reset(ARBITRARY_TIME);
3567 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3568 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3569 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3570 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3571
3572 NotifyKeyArgs args;
3573 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003574 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3576 ASSERT_EQ(AMETA_NONE, args.metaState);
3577 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3578 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3579 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3580
3581 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003582 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3584 ASSERT_EQ(AMETA_NONE, args.metaState);
3585 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3586 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3587 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3588}
3589
Arthur Hung2c9a3342019-07-23 14:18:59 +08003590TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3591 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003592 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3593 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3594 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3595 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003596
3597 // keyboard 2.
3598 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003599 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003600 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003601 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003602 std::shared_ptr<InputDevice> device2 =
3603 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3604 Flags<InputDeviceClass>(0));
3605
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003606 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3607 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3608 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3609 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003611 KeyboardInputMapper& mapper =
3612 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3613 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003615 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003616 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003617 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003618 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3619 device2->reset(ARBITRARY_TIME);
3620
3621 // Prepared displays and associated info.
3622 constexpr uint8_t hdmi1 = 0;
3623 constexpr uint8_t hdmi2 = 1;
3624 const std::string SECONDARY_UNIQUE_ID = "local:1";
3625
3626 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3627 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3628
3629 // No associated display viewport found, should disable the device.
3630 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3631 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3632 ASSERT_FALSE(device2->isEnabled());
3633
3634 // Prepare second display.
3635 constexpr int32_t newDisplayId = 2;
3636 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003637 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003638 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003639 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003640 // Default device will reconfigure above, need additional reconfiguration for another device.
3641 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3642 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3643
3644 // Device should be enabled after the associated display is found.
3645 ASSERT_TRUE(mDevice->isEnabled());
3646 ASSERT_TRUE(device2->isEnabled());
3647
3648 // Test pad key events
3649 ASSERT_NO_FATAL_FAILURE(
3650 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3651 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3652 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3653 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3654 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3655 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3656 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3657
3658 ASSERT_NO_FATAL_FAILURE(
3659 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3660 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3661 AKEYCODE_DPAD_RIGHT, newDisplayId));
3662 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3663 AKEYCODE_DPAD_DOWN, newDisplayId));
3664 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3665 AKEYCODE_DPAD_LEFT, newDisplayId));
3666}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003667
arthurhungc903df12020-08-11 15:08:42 +08003668TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3669 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3670 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3671 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3672 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3673 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3674 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3675
3676 KeyboardInputMapper& mapper =
3677 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3678 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3679 // Initial metastate to AMETA_NONE.
3680 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3681 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3682
3683 // Initialization should have turned all of the lights off.
3684 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3685 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3686 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3687
3688 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003691 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3692 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3693
3694 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003697 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3698 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3699
3700 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003703 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3704 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3705
3706 mFakeEventHub->removeDevice(EVENTHUB_ID);
3707 mReader->loopOnce();
3708
3709 // keyboard 2 should default toggle keys.
3710 const std::string USB2 = "USB2";
3711 const std::string DEVICE_NAME2 = "KEYBOARD2";
3712 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3713 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3714 std::shared_ptr<InputDevice> device2 =
3715 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3716 Flags<InputDeviceClass>(0));
3717 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3718 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3719 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3720 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3721 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3722 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3723
arthurhung6fe95782020-10-05 22:41:16 +08003724 KeyboardInputMapper& mapper2 =
3725 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3726 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003727 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3728 device2->reset(ARBITRARY_TIME);
3729
3730 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3731 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3732 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003733 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3734 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003735}
3736
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003737// --- KeyboardInputMapperTest_ExternalDevice ---
3738
3739class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3740protected:
Chris Yea52ade12020-08-27 16:49:20 -07003741 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003742};
3743
3744TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003745 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3746 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003747
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3749 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3750 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3751 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003752
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003753 KeyboardInputMapper& mapper =
3754 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3755 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003756
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003758 NotifyKeyArgs args;
3759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3760 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3761
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003762 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3764 ASSERT_EQ(uint32_t(0), args.policyFlags);
3765
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3768 ASSERT_EQ(uint32_t(0), args.policyFlags);
3769
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003770 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3772 ASSERT_EQ(uint32_t(0), args.policyFlags);
3773
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3776 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3777
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003778 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3780 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3781}
3782
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003783TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003784 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003785
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003786 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3787 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3788 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003789
Powei Fengd041c5d2019-05-03 17:11:33 -07003790 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003791 KeyboardInputMapper& mapper =
3792 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3793 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003794
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003796 NotifyKeyArgs args;
3797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3798 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3799
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003800 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3802 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3803
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3806 ASSERT_EQ(uint32_t(0), args.policyFlags);
3807
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003808 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3810 ASSERT_EQ(uint32_t(0), args.policyFlags);
3811
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3814 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3815
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003816 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3818 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3819}
3820
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821// --- CursorInputMapperTest ---
3822
3823class CursorInputMapperTest : public InputMapperTest {
3824protected:
3825 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3826
Michael Wright17db18e2020-06-26 20:51:44 +01003827 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003828
Chris Yea52ade12020-08-27 16:49:20 -07003829 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830 InputMapperTest::SetUp();
3831
Michael Wright17db18e2020-06-26 20:51:44 +01003832 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003833 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003834 }
3835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003836 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3837 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003838
3839 void prepareDisplay(int32_t orientation) {
3840 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003841 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003842 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3843 orientation, uniqueId, NO_PORT, viewportType);
3844 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003845
3846 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3847 float pressure) {
3848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3849 0.0f, 0.0f, 0.0f, EPSILON));
3850 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851};
3852
3853const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003855void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3856 int32_t originalY, int32_t rotatedX,
3857 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858 NotifyMotionArgs args;
3859
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003865 ASSERT_NO_FATAL_FAILURE(
3866 assertCursorPointerCoords(args.pointerCoords[0],
3867 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3868 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869}
3870
3871TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003875 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876}
3877
3878TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003880 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003882 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883}
3884
3885TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003887 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888
3889 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003890 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891
3892 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003893 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3894 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3896 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3897
3898 // When the bounds are set, then there should be a valid motion range.
3899 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3900
3901 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003902 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903
3904 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3905 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3906 1, 800 - 1, 0.0f, 0.0f));
3907 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3908 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3909 2, 480 - 1, 0.0f, 0.0f));
3910 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3911 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3912 0.0f, 1.0f, 0.0f, 0.0f));
3913}
3914
3915TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003917 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
3919 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003920 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
3922 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3923 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3924 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3925 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3926 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3927 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3928 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3929 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3930 0.0f, 1.0f, 0.0f, 0.0f));
3931}
3932
3933TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003934 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003935 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936
arthurhungdcef2dc2020-08-11 14:47:50 +08003937 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003938
3939 NotifyMotionArgs args;
3940
3941 // Button press.
3942 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3946 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3947 ASSERT_EQ(DEVICE_ID, args.deviceId);
3948 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3949 ASSERT_EQ(uint32_t(0), args.policyFlags);
3950 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3951 ASSERT_EQ(0, args.flags);
3952 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3953 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3954 ASSERT_EQ(0, args.edgeFlags);
3955 ASSERT_EQ(uint32_t(1), args.pointerCount);
3956 ASSERT_EQ(0, args.pointerProperties[0].id);
3957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003958 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3960 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3961 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3962
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3964 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3965 ASSERT_EQ(DEVICE_ID, args.deviceId);
3966 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3967 ASSERT_EQ(uint32_t(0), args.policyFlags);
3968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3969 ASSERT_EQ(0, args.flags);
3970 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3971 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3972 ASSERT_EQ(0, args.edgeFlags);
3973 ASSERT_EQ(uint32_t(1), args.pointerCount);
3974 ASSERT_EQ(0, args.pointerProperties[0].id);
3975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003976 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003977 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3978 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3979 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3980
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003982 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3983 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3985 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3986 ASSERT_EQ(DEVICE_ID, args.deviceId);
3987 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3988 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3990 ASSERT_EQ(0, args.flags);
3991 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3992 ASSERT_EQ(0, args.buttonState);
3993 ASSERT_EQ(0, args.edgeFlags);
3994 ASSERT_EQ(uint32_t(1), args.pointerCount);
3995 ASSERT_EQ(0, args.pointerProperties[0].id);
3996 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003997 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003998 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3999 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4000 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4001
4002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4003 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4004 ASSERT_EQ(DEVICE_ID, args.deviceId);
4005 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4006 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4008 ASSERT_EQ(0, args.flags);
4009 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4010 ASSERT_EQ(0, args.buttonState);
4011 ASSERT_EQ(0, args.edgeFlags);
4012 ASSERT_EQ(uint32_t(1), args.pointerCount);
4013 ASSERT_EQ(0, args.pointerProperties[0].id);
4014 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004015 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4017 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4018 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4019}
4020
4021TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004023 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024
4025 NotifyMotionArgs args;
4026
4027 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004032 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4033 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4034 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035
4036 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004041 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4042 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043}
4044
4045TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004047 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048
4049 NotifyMotionArgs args;
4050
4051 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004056 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4059 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004060 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004061
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004067 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004068
4069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004071 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072}
4073
4074TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004076 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077
4078 NotifyMotionArgs args;
4079
4080 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4086 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004087 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4088 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4089 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004093 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4094 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4095 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004096
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004103 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4104 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4105 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106
4107 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004112 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004113
4114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004116 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117}
4118
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004119TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004121 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4122 // need to be rotated.
4123 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004124 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004126 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4135}
4136
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004137TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004139 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4140 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004141 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004143 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4152
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004153 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004163 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4172
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004173 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182}
4183
4184TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004186 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187
4188 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4189 mFakePointerController->setPosition(100, 200);
4190 mFakePointerController->setButtonState(0);
4191
4192 NotifyMotionArgs motionArgs;
4193 NotifyKeyArgs keyArgs;
4194
4195 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4199 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4200 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4201 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004202 ASSERT_NO_FATAL_FAILURE(
4203 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4207 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4208 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004209 ASSERT_NO_FATAL_FAILURE(
4210 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004211
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216 ASSERT_EQ(0, motionArgs.buttonState);
4217 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004218 ASSERT_NO_FATAL_FAILURE(
4219 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220
4221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004222 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 ASSERT_EQ(0, motionArgs.buttonState);
4224 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004225 ASSERT_NO_FATAL_FAILURE(
4226 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004227
4228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004230 ASSERT_EQ(0, motionArgs.buttonState);
4231 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004232 ASSERT_NO_FATAL_FAILURE(
4233 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234
4235 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4240 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4241 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4242 motionArgs.buttonState);
4243 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4244 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004245 ASSERT_NO_FATAL_FAILURE(
4246 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4249 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4250 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4251 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4252 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004253 ASSERT_NO_FATAL_FAILURE(
4254 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004255
4256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4257 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4258 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4259 motionArgs.buttonState);
4260 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4261 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004262 ASSERT_NO_FATAL_FAILURE(
4263 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4270 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004271 ASSERT_NO_FATAL_FAILURE(
4272 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004273
4274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004276 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4277 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004278 ASSERT_NO_FATAL_FAILURE(
4279 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4285 ASSERT_EQ(0, motionArgs.buttonState);
4286 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004287 ASSERT_NO_FATAL_FAILURE(
4288 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004291
4292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293 ASSERT_EQ(0, motionArgs.buttonState);
4294 ASSERT_EQ(0, mFakePointerController->getButtonState());
4295 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004296 ASSERT_NO_FATAL_FAILURE(
4297 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004298
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4300 ASSERT_EQ(0, motionArgs.buttonState);
4301 ASSERT_EQ(0, mFakePointerController->getButtonState());
4302 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004303 ASSERT_NO_FATAL_FAILURE(
4304 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305
4306 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4310 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4311 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004312
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004314 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4316 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004317 ASSERT_NO_FATAL_FAILURE(
4318 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004319
4320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4321 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4322 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4323 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004324 ASSERT_NO_FATAL_FAILURE(
4325 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004330 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 ASSERT_EQ(0, motionArgs.buttonState);
4332 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(
4334 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335
4336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338 ASSERT_EQ(0, motionArgs.buttonState);
4339 ASSERT_EQ(0, mFakePointerController->getButtonState());
4340
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004341 ASSERT_NO_FATAL_FAILURE(
4342 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4344 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4345 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4346
4347 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4351 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4352 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004353
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4357 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004358 ASSERT_NO_FATAL_FAILURE(
4359 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004360
4361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4363 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4364 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004365 ASSERT_NO_FATAL_FAILURE(
4366 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 ASSERT_EQ(0, motionArgs.buttonState);
4373 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004374 ASSERT_NO_FATAL_FAILURE(
4375 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004376
4377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4379 ASSERT_EQ(0, motionArgs.buttonState);
4380 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004381 ASSERT_NO_FATAL_FAILURE(
4382 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004383
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4385 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4386 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4387
4388 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4392 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4393 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004394
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004396 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4398 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004399 ASSERT_NO_FATAL_FAILURE(
4400 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004401
4402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4404 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4405 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004406 ASSERT_NO_FATAL_FAILURE(
4407 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 ASSERT_EQ(0, motionArgs.buttonState);
4414 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(
4416 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004417
4418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4420 ASSERT_EQ(0, motionArgs.buttonState);
4421 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004422 ASSERT_NO_FATAL_FAILURE(
4423 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004424
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4426 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4427 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4428
4429 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4433 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4434 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004437 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4439 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004440 ASSERT_NO_FATAL_FAILURE(
4441 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004442
4443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4444 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4445 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4446 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004447 ASSERT_NO_FATAL_FAILURE(
4448 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 ASSERT_EQ(0, motionArgs.buttonState);
4455 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004456 ASSERT_NO_FATAL_FAILURE(
4457 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004458
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4460 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4461 ASSERT_EQ(0, motionArgs.buttonState);
4462 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004463 ASSERT_NO_FATAL_FAILURE(
4464 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004465
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4467 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4468 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4469}
4470
4471TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004473 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474
4475 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4476 mFakePointerController->setPosition(100, 200);
4477 mFakePointerController->setButtonState(0);
4478
4479 NotifyMotionArgs args;
4480
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004485 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4488 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 +01004489 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004490}
4491
4492TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004493 addConfigurationProperty("cursor.mode", "pointer");
4494 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004495 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004496
4497 NotifyDeviceResetArgs resetArgs;
4498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4499 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4500 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4501
4502 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4503 mFakePointerController->setPosition(100, 200);
4504 mFakePointerController->setButtonState(0);
4505
4506 NotifyMotionArgs args;
4507
4508 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4513 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4516 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 +01004517 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004518
4519 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4523 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4524 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4528 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4529 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4531 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4532
4533 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004534 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4535 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4537 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4538 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4542 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4543 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4545 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4546
4547 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4552 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4555 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 +01004556 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004557
4558 // Disable pointer capture and check that the device generation got bumped
4559 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004560 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004561 mFakePolicy->setPointerCapture(false);
4562 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004563 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004564
4565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4566 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4567 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4568
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4573 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4576 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 +01004577 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578}
4579
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004580TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004581 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004582
Garfield Tan888a6a42020-01-09 11:39:16 -08004583 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004584 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004585 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4586 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004587 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4588 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004589 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4590 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4591
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004592 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4593 mFakePointerController->setPosition(100, 200);
4594 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004595
4596 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4601 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4602 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4604 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 +01004605 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004606 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4607}
4608
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609// --- TouchInputMapperTest ---
4610
4611class TouchInputMapperTest : public InputMapperTest {
4612protected:
4613 static const int32_t RAW_X_MIN;
4614 static const int32_t RAW_X_MAX;
4615 static const int32_t RAW_Y_MIN;
4616 static const int32_t RAW_Y_MAX;
4617 static const int32_t RAW_TOUCH_MIN;
4618 static const int32_t RAW_TOUCH_MAX;
4619 static const int32_t RAW_TOOL_MIN;
4620 static const int32_t RAW_TOOL_MAX;
4621 static const int32_t RAW_PRESSURE_MIN;
4622 static const int32_t RAW_PRESSURE_MAX;
4623 static const int32_t RAW_ORIENTATION_MIN;
4624 static const int32_t RAW_ORIENTATION_MAX;
4625 static const int32_t RAW_DISTANCE_MIN;
4626 static const int32_t RAW_DISTANCE_MAX;
4627 static const int32_t RAW_TILT_MIN;
4628 static const int32_t RAW_TILT_MAX;
4629 static const int32_t RAW_ID_MIN;
4630 static const int32_t RAW_ID_MAX;
4631 static const int32_t RAW_SLOT_MIN;
4632 static const int32_t RAW_SLOT_MAX;
4633 static const float X_PRECISION;
4634 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004635 static const float X_PRECISION_VIRTUAL;
4636 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637
4638 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004639 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640
4641 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4642
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004643 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004644 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004645
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646 enum Axes {
4647 POSITION = 1 << 0,
4648 TOUCH = 1 << 1,
4649 TOOL = 1 << 2,
4650 PRESSURE = 1 << 3,
4651 ORIENTATION = 1 << 4,
4652 MINOR = 1 << 5,
4653 ID = 1 << 6,
4654 DISTANCE = 1 << 7,
4655 TILT = 1 << 8,
4656 SLOT = 1 << 9,
4657 TOOL_TYPE = 1 << 10,
4658 };
4659
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004660 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4661 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004662 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004664 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 int32_t toRawX(float displayX);
4666 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004667 float toCookedX(float rawX, float rawY);
4668 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004670 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004672 float toDisplayY(int32_t rawY, int32_t displayHeight);
4673
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674};
4675
4676const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4677const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4678const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4679const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4680const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4681const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4682const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4683const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004684const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4685const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4687const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4688const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4689const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4690const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4691const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4692const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4693const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4694const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4695const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4696const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4697const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004698const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4699 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4700const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4701 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004702const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4703 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704
4705const float TouchInputMapperTest::GEOMETRIC_SCALE =
4706 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4707 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4708
4709const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4710 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4711 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4712};
4713
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004714void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004715 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4716 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004717}
4718
4719void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4720 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4721 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722}
4723
Santos Cordonfa5cf462017-04-05 10:37:00 -07004724void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004725 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4726 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4727 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004728}
4729
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004731 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4732 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4733 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4734 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735}
4736
Jason Gerecke489fda82012-09-07 17:19:40 -07004737void TouchInputMapperTest::prepareLocationCalibration() {
4738 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4739}
4740
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741int32_t TouchInputMapperTest::toRawX(float displayX) {
4742 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4743}
4744
4745int32_t TouchInputMapperTest::toRawY(float displayY) {
4746 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4747}
4748
Jason Gerecke489fda82012-09-07 17:19:40 -07004749float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4750 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4751 return rawX;
4752}
4753
4754float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4755 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4756 return rawY;
4757}
4758
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004760 return toDisplayX(rawX, DISPLAY_WIDTH);
4761}
4762
4763float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4764 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765}
4766
4767float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004768 return toDisplayY(rawY, DISPLAY_HEIGHT);
4769}
4770
4771float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4772 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773}
4774
4775
4776// --- SingleTouchInputMapperTest ---
4777
4778class SingleTouchInputMapperTest : public TouchInputMapperTest {
4779protected:
4780 void prepareButtons();
4781 void prepareAxes(int axes);
4782
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004783 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4784 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4785 void processUp(SingleTouchInputMapper& mappery);
4786 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4787 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4788 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4789 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4790 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4791 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792};
4793
4794void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004795 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796}
4797
4798void SingleTouchInputMapperTest::prepareAxes(int axes) {
4799 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004800 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4801 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802 }
4803 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004804 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4805 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 }
4807 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004808 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4809 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 }
4811 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004812 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4813 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814 }
4815 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004816 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4817 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818 }
4819}
4820
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004821void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825}
4826
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004827void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004828 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::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834}
4835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004836void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838}
4839
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004840void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4841 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843}
4844
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004845void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847}
4848
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004849void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4850 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853}
4854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004855void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4856 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858}
4859
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004860void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862}
4863
Michael Wrightd02c5b62014-02-10 15:10:22 -08004864TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 prepareButtons();
4866 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004867 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004869 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870}
4871
4872TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004873 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4874 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875 prepareButtons();
4876 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004877 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004879 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880}
4881
4882TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883 prepareButtons();
4884 prepareAxes(POSITION);
4885 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004886 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004888 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889}
4890
4891TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892 prepareButtons();
4893 prepareAxes(POSITION);
4894 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004895 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004897 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898}
4899
4900TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901 addConfigurationProperty("touch.deviceType", "touchScreen");
4902 prepareDisplay(DISPLAY_ORIENTATION_0);
4903 prepareButtons();
4904 prepareAxes(POSITION);
4905 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004906 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907
4908 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004909 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910
4911 // Virtual key is down.
4912 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4913 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4914 processDown(mapper, x, y);
4915 processSync(mapper);
4916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4917
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004918 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919
4920 // Virtual key is up.
4921 processUp(mapper);
4922 processSync(mapper);
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004925 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926}
4927
4928TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929 addConfigurationProperty("touch.deviceType", "touchScreen");
4930 prepareDisplay(DISPLAY_ORIENTATION_0);
4931 prepareButtons();
4932 prepareAxes(POSITION);
4933 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004934 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935
4936 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004937 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938
4939 // Virtual key is down.
4940 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4941 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4942 processDown(mapper, x, y);
4943 processSync(mapper);
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4945
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004946 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947
4948 // Virtual key is up.
4949 processUp(mapper);
4950 processSync(mapper);
4951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004953 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954}
4955
4956TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957 addConfigurationProperty("touch.deviceType", "touchScreen");
4958 prepareDisplay(DISPLAY_ORIENTATION_0);
4959 prepareButtons();
4960 prepareAxes(POSITION);
4961 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004962 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963
4964 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4965 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004966 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 ASSERT_TRUE(flags[0]);
4968 ASSERT_FALSE(flags[1]);
4969}
4970
4971TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972 addConfigurationProperty("touch.deviceType", "touchScreen");
4973 prepareDisplay(DISPLAY_ORIENTATION_0);
4974 prepareButtons();
4975 prepareAxes(POSITION);
4976 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004977 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978
arthurhungdcef2dc2020-08-11 14:47:50 +08004979 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980
4981 NotifyKeyArgs args;
4982
4983 // Press virtual key.
4984 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4985 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4986 processDown(mapper, x, y);
4987 processSync(mapper);
4988
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4990 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4991 ASSERT_EQ(DEVICE_ID, args.deviceId);
4992 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4993 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4994 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4995 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4996 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4997 ASSERT_EQ(KEY_HOME, args.scanCode);
4998 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4999 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5000
5001 // Release virtual key.
5002 processUp(mapper);
5003 processSync(mapper);
5004
5005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5006 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5007 ASSERT_EQ(DEVICE_ID, args.deviceId);
5008 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5009 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5010 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5011 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5012 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5013 ASSERT_EQ(KEY_HOME, args.scanCode);
5014 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5015 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5016
5017 // Should not have sent any motions.
5018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5019}
5020
5021TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 addConfigurationProperty("touch.deviceType", "touchScreen");
5023 prepareDisplay(DISPLAY_ORIENTATION_0);
5024 prepareButtons();
5025 prepareAxes(POSITION);
5026 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005027 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028
arthurhungdcef2dc2020-08-11 14:47:50 +08005029 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030
5031 NotifyKeyArgs keyArgs;
5032
5033 // Press virtual key.
5034 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5035 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5036 processDown(mapper, x, y);
5037 processSync(mapper);
5038
5039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5040 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5041 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5042 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5043 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5044 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5045 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5046 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5047 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5048 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5049 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5050
5051 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5052 // into the display area.
5053 y -= 100;
5054 processMove(mapper, x, y);
5055 processSync(mapper);
5056
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5058 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5059 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5060 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5061 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5062 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5063 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5064 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5065 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5066 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5067 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5068 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5069
5070 NotifyMotionArgs motionArgs;
5071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5072 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5073 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5074 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5075 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5076 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5077 ASSERT_EQ(0, motionArgs.flags);
5078 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5079 ASSERT_EQ(0, motionArgs.buttonState);
5080 ASSERT_EQ(0, motionArgs.edgeFlags);
5081 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5082 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5085 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5086 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5087 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5088 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5089
5090 // Keep moving out of bounds. Should generate a pointer move.
5091 y -= 50;
5092 processMove(mapper, x, y);
5093 processSync(mapper);
5094
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5096 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5097 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5098 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5099 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5101 ASSERT_EQ(0, motionArgs.flags);
5102 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5103 ASSERT_EQ(0, motionArgs.buttonState);
5104 ASSERT_EQ(0, motionArgs.edgeFlags);
5105 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5106 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5109 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5110 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5111 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5112 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5113
5114 // Release out of bounds. Should generate a pointer up.
5115 processUp(mapper);
5116 processSync(mapper);
5117
5118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5119 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5120 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5121 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5122 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5123 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5124 ASSERT_EQ(0, motionArgs.flags);
5125 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5126 ASSERT_EQ(0, motionArgs.buttonState);
5127 ASSERT_EQ(0, motionArgs.edgeFlags);
5128 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5129 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5132 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5133 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5134 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5135 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5136
5137 // Should not have sent any more keys or motions.
5138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5140}
5141
5142TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143 addConfigurationProperty("touch.deviceType", "touchScreen");
5144 prepareDisplay(DISPLAY_ORIENTATION_0);
5145 prepareButtons();
5146 prepareAxes(POSITION);
5147 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005148 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149
arthurhungdcef2dc2020-08-11 14:47:50 +08005150 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151
5152 NotifyMotionArgs motionArgs;
5153
5154 // Initially go down out of bounds.
5155 int32_t x = -10;
5156 int32_t y = -10;
5157 processDown(mapper, x, y);
5158 processSync(mapper);
5159
5160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5161
5162 // Move into the display area. Should generate a pointer down.
5163 x = 50;
5164 y = 75;
5165 processMove(mapper, x, y);
5166 processSync(mapper);
5167
5168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5169 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5170 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5171 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5172 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5173 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5174 ASSERT_EQ(0, motionArgs.flags);
5175 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5176 ASSERT_EQ(0, motionArgs.buttonState);
5177 ASSERT_EQ(0, motionArgs.edgeFlags);
5178 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5179 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5182 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5183 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5184 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5186
5187 // Release. Should generate a pointer up.
5188 processUp(mapper);
5189 processSync(mapper);
5190
5191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5192 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5193 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5194 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5195 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5196 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5197 ASSERT_EQ(0, motionArgs.flags);
5198 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5199 ASSERT_EQ(0, motionArgs.buttonState);
5200 ASSERT_EQ(0, motionArgs.edgeFlags);
5201 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5202 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5205 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5206 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5207 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5208 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5209
5210 // Should not have sent any more keys or motions.
5211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5213}
5214
Santos Cordonfa5cf462017-04-05 10:37:00 -07005215TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005216 addConfigurationProperty("touch.deviceType", "touchScreen");
5217 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5218
5219 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5220 prepareButtons();
5221 prepareAxes(POSITION);
5222 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005223 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005224
arthurhungdcef2dc2020-08-11 14:47:50 +08005225 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005226
5227 NotifyMotionArgs motionArgs;
5228
5229 // Down.
5230 int32_t x = 100;
5231 int32_t y = 125;
5232 processDown(mapper, x, y);
5233 processSync(mapper);
5234
5235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5236 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5237 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5238 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5239 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5240 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5241 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5242 ASSERT_EQ(0, motionArgs.flags);
5243 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5244 ASSERT_EQ(0, motionArgs.buttonState);
5245 ASSERT_EQ(0, motionArgs.edgeFlags);
5246 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5247 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5250 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5251 1, 0, 0, 0, 0, 0, 0, 0));
5252 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5253 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5254 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5255
5256 // Move.
5257 x += 50;
5258 y += 75;
5259 processMove(mapper, x, y);
5260 processSync(mapper);
5261
5262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5264 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5265 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5266 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5267 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5269 ASSERT_EQ(0, motionArgs.flags);
5270 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5271 ASSERT_EQ(0, motionArgs.buttonState);
5272 ASSERT_EQ(0, motionArgs.edgeFlags);
5273 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5274 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5277 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5278 1, 0, 0, 0, 0, 0, 0, 0));
5279 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5280 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5281 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5282
5283 // Up.
5284 processUp(mapper);
5285 processSync(mapper);
5286
5287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5289 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5290 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5291 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5292 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5293 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5294 ASSERT_EQ(0, motionArgs.flags);
5295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5296 ASSERT_EQ(0, motionArgs.buttonState);
5297 ASSERT_EQ(0, motionArgs.edgeFlags);
5298 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5299 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5302 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5303 1, 0, 0, 0, 0, 0, 0, 0));
5304 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5305 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5306 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5307
5308 // Should not have sent any more keys or motions.
5309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5311}
5312
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314 addConfigurationProperty("touch.deviceType", "touchScreen");
5315 prepareDisplay(DISPLAY_ORIENTATION_0);
5316 prepareButtons();
5317 prepareAxes(POSITION);
5318 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005319 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320
arthurhungdcef2dc2020-08-11 14:47:50 +08005321 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322
5323 NotifyMotionArgs motionArgs;
5324
5325 // Down.
5326 int32_t x = 100;
5327 int32_t y = 125;
5328 processDown(mapper, x, y);
5329 processSync(mapper);
5330
5331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5333 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5334 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5335 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5336 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5337 ASSERT_EQ(0, motionArgs.flags);
5338 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5339 ASSERT_EQ(0, motionArgs.buttonState);
5340 ASSERT_EQ(0, motionArgs.edgeFlags);
5341 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5342 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5345 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5346 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5347 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5349
5350 // Move.
5351 x += 50;
5352 y += 75;
5353 processMove(mapper, x, y);
5354 processSync(mapper);
5355
5356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5357 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5358 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5359 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5360 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5362 ASSERT_EQ(0, motionArgs.flags);
5363 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5364 ASSERT_EQ(0, motionArgs.buttonState);
5365 ASSERT_EQ(0, motionArgs.edgeFlags);
5366 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5367 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5370 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5371 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5372 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5374
5375 // Up.
5376 processUp(mapper);
5377 processSync(mapper);
5378
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5380 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5381 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5382 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5383 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5384 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5385 ASSERT_EQ(0, motionArgs.flags);
5386 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5387 ASSERT_EQ(0, motionArgs.buttonState);
5388 ASSERT_EQ(0, motionArgs.edgeFlags);
5389 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5390 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5393 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5394 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5395 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5397
5398 // Should not have sent any more keys or motions.
5399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5401}
5402
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005403TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 addConfigurationProperty("touch.deviceType", "touchScreen");
5405 prepareButtons();
5406 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005407 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5408 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005409 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410
5411 NotifyMotionArgs args;
5412
5413 // Rotation 90.
5414 prepareDisplay(DISPLAY_ORIENTATION_90);
5415 processDown(mapper, toRawX(50), toRawY(75));
5416 processSync(mapper);
5417
5418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5419 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5420 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5421
5422 processUp(mapper);
5423 processSync(mapper);
5424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5425}
5426
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005427TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428 addConfigurationProperty("touch.deviceType", "touchScreen");
5429 prepareButtons();
5430 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005431 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5432 // orientation-aware are affected by display rotation.
5433 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005434 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435
5436 NotifyMotionArgs args;
5437
5438 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005439 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 prepareDisplay(DISPLAY_ORIENTATION_0);
5441 processDown(mapper, toRawX(50), toRawY(75));
5442 processSync(mapper);
5443
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5445 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5446 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5447
5448 processUp(mapper);
5449 processSync(mapper);
5450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5451
5452 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005453 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005454 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005455 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456 processSync(mapper);
5457
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5459 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5460 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5461
5462 processUp(mapper);
5463 processSync(mapper);
5464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5465
5466 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005467 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468 prepareDisplay(DISPLAY_ORIENTATION_180);
5469 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5470 processSync(mapper);
5471
5472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5473 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5474 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5475
5476 processUp(mapper);
5477 processSync(mapper);
5478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5479
5480 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005481 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005483 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484 processSync(mapper);
5485
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5487 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5488 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5489
5490 processUp(mapper);
5491 processSync(mapper);
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5493}
5494
5495TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496 addConfigurationProperty("touch.deviceType", "touchScreen");
5497 prepareDisplay(DISPLAY_ORIENTATION_0);
5498 prepareButtons();
5499 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005500 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501
5502 // These calculations are based on the input device calibration documentation.
5503 int32_t rawX = 100;
5504 int32_t rawY = 200;
5505 int32_t rawPressure = 10;
5506 int32_t rawToolMajor = 12;
5507 int32_t rawDistance = 2;
5508 int32_t rawTiltX = 30;
5509 int32_t rawTiltY = 110;
5510
5511 float x = toDisplayX(rawX);
5512 float y = toDisplayY(rawY);
5513 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5514 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5515 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5516 float distance = float(rawDistance);
5517
5518 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5519 float tiltScale = M_PI / 180;
5520 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5521 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5522 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5523 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5524
5525 processDown(mapper, rawX, rawY);
5526 processPressure(mapper, rawPressure);
5527 processToolMajor(mapper, rawToolMajor);
5528 processDistance(mapper, rawDistance);
5529 processTilt(mapper, rawTiltX, rawTiltY);
5530 processSync(mapper);
5531
5532 NotifyMotionArgs args;
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5535 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5536 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5537}
5538
Jason Gerecke489fda82012-09-07 17:19:40 -07005539TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005540 addConfigurationProperty("touch.deviceType", "touchScreen");
5541 prepareDisplay(DISPLAY_ORIENTATION_0);
5542 prepareLocationCalibration();
5543 prepareButtons();
5544 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005545 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005546
5547 int32_t rawX = 100;
5548 int32_t rawY = 200;
5549
5550 float x = toDisplayX(toCookedX(rawX, rawY));
5551 float y = toDisplayY(toCookedY(rawX, rawY));
5552
5553 processDown(mapper, rawX, rawY);
5554 processSync(mapper);
5555
5556 NotifyMotionArgs args;
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5559 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5560}
5561
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563 addConfigurationProperty("touch.deviceType", "touchScreen");
5564 prepareDisplay(DISPLAY_ORIENTATION_0);
5565 prepareButtons();
5566 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005567 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568
5569 NotifyMotionArgs motionArgs;
5570 NotifyKeyArgs keyArgs;
5571
5572 processDown(mapper, 100, 200);
5573 processSync(mapper);
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5575 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5576 ASSERT_EQ(0, motionArgs.buttonState);
5577
5578 // press BTN_LEFT, release BTN_LEFT
5579 processKey(mapper, BTN_LEFT, 1);
5580 processSync(mapper);
5581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5583 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5584
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5587 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5588
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589 processKey(mapper, BTN_LEFT, 0);
5590 processSync(mapper);
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005592 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005594
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005597 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005598
5599 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5600 processKey(mapper, BTN_RIGHT, 1);
5601 processKey(mapper, BTN_MIDDLE, 1);
5602 processSync(mapper);
5603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5605 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5606 motionArgs.buttonState);
5607
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5609 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5610 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5611
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5613 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5614 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5615 motionArgs.buttonState);
5616
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617 processKey(mapper, BTN_RIGHT, 0);
5618 processSync(mapper);
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005620 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005622
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005625 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626
5627 processKey(mapper, BTN_MIDDLE, 0);
5628 processSync(mapper);
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005631 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005632
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005635 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005636
5637 // press BTN_BACK, release BTN_BACK
5638 processKey(mapper, BTN_BACK, 1);
5639 processSync(mapper);
5640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5641 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5642 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005643
Michael Wrightd02c5b62014-02-10 15:10:22 -08005644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005646 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5647
5648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5649 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5650 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651
5652 processKey(mapper, BTN_BACK, 0);
5653 processSync(mapper);
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005655 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005656 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005657
5658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005660 ASSERT_EQ(0, motionArgs.buttonState);
5661
Michael Wrightd02c5b62014-02-10 15:10:22 -08005662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5663 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5664 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5665
5666 // press BTN_SIDE, release BTN_SIDE
5667 processKey(mapper, BTN_SIDE, 1);
5668 processSync(mapper);
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5670 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5671 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005672
Michael Wrightd02c5b62014-02-10 15:10:22 -08005673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005675 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5676
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5678 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5679 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680
5681 processKey(mapper, BTN_SIDE, 0);
5682 processSync(mapper);
5683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005684 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005685 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005686
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005689 ASSERT_EQ(0, motionArgs.buttonState);
5690
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5692 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5693 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5694
5695 // press BTN_FORWARD, release BTN_FORWARD
5696 processKey(mapper, BTN_FORWARD, 1);
5697 processSync(mapper);
5698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5699 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5700 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005701
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005704 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5705
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5707 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5708 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709
5710 processKey(mapper, BTN_FORWARD, 0);
5711 processSync(mapper);
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005713 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005714 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005715
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005718 ASSERT_EQ(0, motionArgs.buttonState);
5719
Michael Wrightd02c5b62014-02-10 15:10:22 -08005720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5721 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5722 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5723
5724 // press BTN_EXTRA, release BTN_EXTRA
5725 processKey(mapper, BTN_EXTRA, 1);
5726 processSync(mapper);
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5728 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5729 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005730
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005733 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5734
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5736 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5737 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738
5739 processKey(mapper, BTN_EXTRA, 0);
5740 processSync(mapper);
5741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005742 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005743 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005744
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005747 ASSERT_EQ(0, motionArgs.buttonState);
5748
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5750 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5751 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5752
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5754
Michael Wrightd02c5b62014-02-10 15:10:22 -08005755 // press BTN_STYLUS, release BTN_STYLUS
5756 processKey(mapper, BTN_STYLUS, 1);
5757 processSync(mapper);
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005760 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5761
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5763 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5764 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765
5766 processKey(mapper, BTN_STYLUS, 0);
5767 processSync(mapper);
5768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005769 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005771
5772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005774 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775
5776 // press BTN_STYLUS2, release BTN_STYLUS2
5777 processKey(mapper, BTN_STYLUS2, 1);
5778 processSync(mapper);
5779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005781 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, 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_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786
5787 processKey(mapper, BTN_STYLUS2, 0);
5788 processSync(mapper);
5789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005790 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005791 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005792
5793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005795 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005796
5797 // release touch
5798 processUp(mapper);
5799 processSync(mapper);
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5801 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5802 ASSERT_EQ(0, motionArgs.buttonState);
5803}
5804
5805TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005806 addConfigurationProperty("touch.deviceType", "touchScreen");
5807 prepareDisplay(DISPLAY_ORIENTATION_0);
5808 prepareButtons();
5809 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005810 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811
5812 NotifyMotionArgs motionArgs;
5813
5814 // default tool type is finger
5815 processDown(mapper, 100, 200);
5816 processSync(mapper);
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5818 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5820
5821 // eraser
5822 processKey(mapper, BTN_TOOL_RUBBER, 1);
5823 processSync(mapper);
5824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5827
5828 // stylus
5829 processKey(mapper, BTN_TOOL_RUBBER, 0);
5830 processKey(mapper, BTN_TOOL_PEN, 1);
5831 processSync(mapper);
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5835
5836 // brush
5837 processKey(mapper, BTN_TOOL_PEN, 0);
5838 processKey(mapper, BTN_TOOL_BRUSH, 1);
5839 processSync(mapper);
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5843
5844 // pencil
5845 processKey(mapper, BTN_TOOL_BRUSH, 0);
5846 processKey(mapper, BTN_TOOL_PENCIL, 1);
5847 processSync(mapper);
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5851
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005852 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005853 processKey(mapper, BTN_TOOL_PENCIL, 0);
5854 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5855 processSync(mapper);
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5859
5860 // mouse
5861 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5862 processKey(mapper, BTN_TOOL_MOUSE, 1);
5863 processSync(mapper);
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5867
5868 // lens
5869 processKey(mapper, BTN_TOOL_MOUSE, 0);
5870 processKey(mapper, BTN_TOOL_LENS, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5875
5876 // double-tap
5877 processKey(mapper, BTN_TOOL_LENS, 0);
5878 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5883
5884 // triple-tap
5885 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5886 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5887 processSync(mapper);
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5891
5892 // quad-tap
5893 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5894 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5895 processSync(mapper);
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5899
5900 // finger
5901 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5902 processKey(mapper, BTN_TOOL_FINGER, 1);
5903 processSync(mapper);
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5907
5908 // stylus trumps finger
5909 processKey(mapper, BTN_TOOL_PEN, 1);
5910 processSync(mapper);
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5914
5915 // eraser trumps stylus
5916 processKey(mapper, BTN_TOOL_RUBBER, 1);
5917 processSync(mapper);
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5921
5922 // mouse trumps eraser
5923 processKey(mapper, BTN_TOOL_MOUSE, 1);
5924 processSync(mapper);
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5928
5929 // back to default tool type
5930 processKey(mapper, BTN_TOOL_MOUSE, 0);
5931 processKey(mapper, BTN_TOOL_RUBBER, 0);
5932 processKey(mapper, BTN_TOOL_PEN, 0);
5933 processKey(mapper, BTN_TOOL_FINGER, 0);
5934 processSync(mapper);
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5938}
5939
5940TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 addConfigurationProperty("touch.deviceType", "touchScreen");
5942 prepareDisplay(DISPLAY_ORIENTATION_0);
5943 prepareButtons();
5944 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005945 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005946 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947
5948 NotifyMotionArgs motionArgs;
5949
5950 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5951 processKey(mapper, BTN_TOOL_FINGER, 1);
5952 processMove(mapper, 100, 200);
5953 processSync(mapper);
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5957 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5958
5959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5960 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5962 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5963
5964 // move a little
5965 processMove(mapper, 150, 250);
5966 processSync(mapper);
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5968 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5970 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5971
5972 // down when BTN_TOUCH is pressed, pressure defaults to 1
5973 processKey(mapper, BTN_TOUCH, 1);
5974 processSync(mapper);
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5976 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5978 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5979
5980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5981 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5983 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5984
5985 // up when BTN_TOUCH is released, hover restored
5986 processKey(mapper, BTN_TOUCH, 0);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5991 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5992
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5994 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5996 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5997
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5999 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6001 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6002
6003 // exit hover when pointer goes away
6004 processKey(mapper, BTN_TOOL_FINGER, 0);
6005 processSync(mapper);
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6009 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6010}
6011
6012TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013 addConfigurationProperty("touch.deviceType", "touchScreen");
6014 prepareDisplay(DISPLAY_ORIENTATION_0);
6015 prepareButtons();
6016 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006017 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006018
6019 NotifyMotionArgs motionArgs;
6020
6021 // initially hovering because pressure is 0
6022 processDown(mapper, 100, 200);
6023 processPressure(mapper, 0);
6024 processSync(mapper);
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6028 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6029
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6031 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6033 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6034
6035 // move a little
6036 processMove(mapper, 150, 250);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6041 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6042
6043 // down when pressure is non-zero
6044 processPressure(mapper, RAW_PRESSURE_MAX);
6045 processSync(mapper);
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6047 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6049 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6050
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6052 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6054 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6055
6056 // up when pressure becomes 0, hover restored
6057 processPressure(mapper, 0);
6058 processSync(mapper);
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6062 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6063
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6065 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6067 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6068
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6070 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6072 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6073
6074 // exit hover when pointer goes away
6075 processUp(mapper);
6076 processSync(mapper);
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6078 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6080 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6081}
6082
Michael Wrightd02c5b62014-02-10 15:10:22 -08006083// --- MultiTouchInputMapperTest ---
6084
6085class MultiTouchInputMapperTest : public TouchInputMapperTest {
6086protected:
6087 void prepareAxes(int axes);
6088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006089 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6090 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6091 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6092 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6093 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6094 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6095 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6096 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6097 void processId(MultiTouchInputMapper& mapper, int32_t id);
6098 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6099 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6100 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6101 void processMTSync(MultiTouchInputMapper& mapper);
6102 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103};
6104
6105void MultiTouchInputMapperTest::prepareAxes(int axes) {
6106 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006107 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6108 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 }
6110 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006111 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6112 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006113 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006114 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6115 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116 }
6117 }
6118 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006119 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6120 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006122 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6123 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124 }
6125 }
6126 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006127 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6128 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 }
6130 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006131 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6132 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133 }
6134 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6136 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137 }
6138 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006139 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6140 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 }
6142 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006143 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6144 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145 }
6146 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006147 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148 }
6149}
6150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006151void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6152 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155}
6156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006157void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6158 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160}
6161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006162void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6163 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165}
6166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006167void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169}
6170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006171void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173}
6174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006175void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6176 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178}
6179
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006180void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182}
6183
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006184void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186}
6187
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006188void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190}
6191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006192void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194}
6195
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006196void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006198}
6199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006200void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6201 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203}
6204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006205void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006207}
6208
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006209void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211}
6212
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214 addConfigurationProperty("touch.deviceType", "touchScreen");
6215 prepareDisplay(DISPLAY_ORIENTATION_0);
6216 prepareAxes(POSITION);
6217 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006218 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006219
arthurhungdcef2dc2020-08-11 14:47:50 +08006220 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221
6222 NotifyMotionArgs motionArgs;
6223
6224 // Two fingers down at once.
6225 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6226 processPosition(mapper, x1, y1);
6227 processMTSync(mapper);
6228 processPosition(mapper, x2, y2);
6229 processMTSync(mapper);
6230 processSync(mapper);
6231
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6234 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6235 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6236 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6238 ASSERT_EQ(0, motionArgs.flags);
6239 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6240 ASSERT_EQ(0, motionArgs.buttonState);
6241 ASSERT_EQ(0, motionArgs.edgeFlags);
6242 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6243 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6246 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6247 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6248 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6250
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6252 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6253 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6254 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6255 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6256 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6257 motionArgs.action);
6258 ASSERT_EQ(0, motionArgs.flags);
6259 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6260 ASSERT_EQ(0, motionArgs.buttonState);
6261 ASSERT_EQ(0, motionArgs.edgeFlags);
6262 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6263 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6265 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6268 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6270 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6271 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6272 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6274
6275 // Move.
6276 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6277 processPosition(mapper, x1, y1);
6278 processMTSync(mapper);
6279 processPosition(mapper, x2, y2);
6280 processMTSync(mapper);
6281 processSync(mapper);
6282
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6284 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6285 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6286 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6287 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6289 ASSERT_EQ(0, motionArgs.flags);
6290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6291 ASSERT_EQ(0, motionArgs.buttonState);
6292 ASSERT_EQ(0, motionArgs.edgeFlags);
6293 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6294 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6296 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6299 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6301 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6302 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6303 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6304 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6305
6306 // First finger up.
6307 x2 += 15; y2 -= 20;
6308 processPosition(mapper, x2, y2);
6309 processMTSync(mapper);
6310 processSync(mapper);
6311
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6314 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6315 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6316 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6317 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6318 motionArgs.action);
6319 ASSERT_EQ(0, motionArgs.flags);
6320 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6321 ASSERT_EQ(0, motionArgs.buttonState);
6322 ASSERT_EQ(0, motionArgs.edgeFlags);
6323 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6324 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6326 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6329 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6331 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6332 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6333 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6334 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6335
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6338 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6339 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6340 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6342 ASSERT_EQ(0, motionArgs.flags);
6343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6344 ASSERT_EQ(0, motionArgs.buttonState);
6345 ASSERT_EQ(0, motionArgs.edgeFlags);
6346 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6347 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6350 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6351 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6352 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6354
6355 // Move.
6356 x2 += 20; y2 -= 25;
6357 processPosition(mapper, x2, y2);
6358 processMTSync(mapper);
6359 processSync(mapper);
6360
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6362 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6363 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6364 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6365 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6367 ASSERT_EQ(0, motionArgs.flags);
6368 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6369 ASSERT_EQ(0, motionArgs.buttonState);
6370 ASSERT_EQ(0, motionArgs.edgeFlags);
6371 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6372 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6375 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6376 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6377 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6379
6380 // New finger down.
6381 int32_t x3 = 700, y3 = 300;
6382 processPosition(mapper, x2, y2);
6383 processMTSync(mapper);
6384 processPosition(mapper, x3, y3);
6385 processMTSync(mapper);
6386 processSync(mapper);
6387
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6390 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6391 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6392 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6393 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6394 motionArgs.action);
6395 ASSERT_EQ(0, motionArgs.flags);
6396 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6397 ASSERT_EQ(0, motionArgs.buttonState);
6398 ASSERT_EQ(0, motionArgs.edgeFlags);
6399 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6400 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6402 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6405 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6407 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6408 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6409 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6410 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6411
6412 // Second finger up.
6413 x3 += 30; y3 -= 20;
6414 processPosition(mapper, x3, y3);
6415 processMTSync(mapper);
6416 processSync(mapper);
6417
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6420 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6421 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6422 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6424 motionArgs.action);
6425 ASSERT_EQ(0, motionArgs.flags);
6426 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6427 ASSERT_EQ(0, motionArgs.buttonState);
6428 ASSERT_EQ(0, motionArgs.edgeFlags);
6429 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6430 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6432 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6435 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6437 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6438 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6439 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6441
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6444 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6445 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6446 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6448 ASSERT_EQ(0, motionArgs.flags);
6449 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6450 ASSERT_EQ(0, motionArgs.buttonState);
6451 ASSERT_EQ(0, motionArgs.edgeFlags);
6452 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6453 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6456 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6457 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6458 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6460
6461 // Last finger up.
6462 processMTSync(mapper);
6463 processSync(mapper);
6464
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6467 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6468 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6469 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6471 ASSERT_EQ(0, motionArgs.flags);
6472 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6473 ASSERT_EQ(0, motionArgs.buttonState);
6474 ASSERT_EQ(0, motionArgs.edgeFlags);
6475 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6476 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6479 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6480 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6481 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6483
6484 // Should not have sent any more keys or motions.
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6487}
6488
6489TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490 addConfigurationProperty("touch.deviceType", "touchScreen");
6491 prepareDisplay(DISPLAY_ORIENTATION_0);
6492 prepareAxes(POSITION | ID);
6493 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006494 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006495
arthurhungdcef2dc2020-08-11 14:47:50 +08006496 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006497
6498 NotifyMotionArgs motionArgs;
6499
6500 // Two fingers down at once.
6501 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6502 processPosition(mapper, x1, y1);
6503 processId(mapper, 1);
6504 processMTSync(mapper);
6505 processPosition(mapper, x2, y2);
6506 processId(mapper, 2);
6507 processMTSync(mapper);
6508 processSync(mapper);
6509
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6512 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6513 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6516 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6517
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6520 motionArgs.action);
6521 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6522 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6524 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6527 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6529 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6530
6531 // Move.
6532 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6533 processPosition(mapper, x1, y1);
6534 processId(mapper, 1);
6535 processMTSync(mapper);
6536 processPosition(mapper, x2, y2);
6537 processId(mapper, 2);
6538 processMTSync(mapper);
6539 processSync(mapper);
6540
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6543 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6544 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6546 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6549 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6551 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6552
6553 // First finger up.
6554 x2 += 15; y2 -= 20;
6555 processPosition(mapper, x2, y2);
6556 processId(mapper, 2);
6557 processMTSync(mapper);
6558 processSync(mapper);
6559
6560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6561 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6562 motionArgs.action);
6563 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6564 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6566 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6569 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6571 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6572
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6575 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6576 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6579 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6580
6581 // Move.
6582 x2 += 20; y2 -= 25;
6583 processPosition(mapper, x2, y2);
6584 processId(mapper, 2);
6585 processMTSync(mapper);
6586 processSync(mapper);
6587
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6590 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6591 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6594 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6595
6596 // New finger down.
6597 int32_t x3 = 700, y3 = 300;
6598 processPosition(mapper, x2, y2);
6599 processId(mapper, 2);
6600 processMTSync(mapper);
6601 processPosition(mapper, x3, y3);
6602 processId(mapper, 3);
6603 processMTSync(mapper);
6604 processSync(mapper);
6605
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6607 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6608 motionArgs.action);
6609 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6610 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6612 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6615 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6617 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6618
6619 // Second finger up.
6620 x3 += 30; y3 -= 20;
6621 processPosition(mapper, x3, y3);
6622 processId(mapper, 3);
6623 processMTSync(mapper);
6624 processSync(mapper);
6625
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6627 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6628 motionArgs.action);
6629 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6630 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6632 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6635 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6637 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6638
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6641 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6642 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6645 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6646
6647 // Last finger up.
6648 processMTSync(mapper);
6649 processSync(mapper);
6650
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6652 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6653 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6654 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6657 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6658
6659 // Should not have sent any more keys or motions.
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6662}
6663
6664TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006665 addConfigurationProperty("touch.deviceType", "touchScreen");
6666 prepareDisplay(DISPLAY_ORIENTATION_0);
6667 prepareAxes(POSITION | ID | SLOT);
6668 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006669 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006670
arthurhungdcef2dc2020-08-11 14:47:50 +08006671 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006672
6673 NotifyMotionArgs motionArgs;
6674
6675 // Two fingers down at once.
6676 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6677 processPosition(mapper, x1, y1);
6678 processId(mapper, 1);
6679 processSlot(mapper, 1);
6680 processPosition(mapper, x2, y2);
6681 processId(mapper, 2);
6682 processSync(mapper);
6683
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6685 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6686 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6687 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6690 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6691
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6693 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6694 motionArgs.action);
6695 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6696 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6698 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6701 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6703 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6704
6705 // Move.
6706 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6707 processSlot(mapper, 0);
6708 processPosition(mapper, x1, y1);
6709 processSlot(mapper, 1);
6710 processPosition(mapper, x2, y2);
6711 processSync(mapper);
6712
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6715 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6716 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6718 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6721 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6723 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6724
6725 // First finger up.
6726 x2 += 15; y2 -= 20;
6727 processSlot(mapper, 0);
6728 processId(mapper, -1);
6729 processSlot(mapper, 1);
6730 processPosition(mapper, x2, y2);
6731 processSync(mapper);
6732
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6734 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6735 motionArgs.action);
6736 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6737 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6739 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6740 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6742 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6744 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6745
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6748 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6749 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6752 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6753
6754 // Move.
6755 x2 += 20; y2 -= 25;
6756 processPosition(mapper, x2, y2);
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 processSlot(mapper, 0);
6771 processId(mapper, 3);
6772 processPosition(mapper, x3, y3);
6773 processSync(mapper);
6774
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6776 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6777 motionArgs.action);
6778 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6779 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6781 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6784 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6786 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6787
6788 // Second finger up.
6789 x3 += 30; y3 -= 20;
6790 processSlot(mapper, 1);
6791 processId(mapper, -1);
6792 processSlot(mapper, 0);
6793 processPosition(mapper, x3, y3);
6794 processSync(mapper);
6795
6796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6797 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6798 motionArgs.action);
6799 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6800 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6802 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6805 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6807 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6808
6809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6811 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6812 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6815 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6816
6817 // Last finger up.
6818 processId(mapper, -1);
6819 processSync(mapper);
6820
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6822 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6823 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6824 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6827 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6828
6829 // Should not have sent any more keys or motions.
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6832}
6833
6834TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006835 addConfigurationProperty("touch.deviceType", "touchScreen");
6836 prepareDisplay(DISPLAY_ORIENTATION_0);
6837 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006838 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006839
6840 // These calculations are based on the input device calibration documentation.
6841 int32_t rawX = 100;
6842 int32_t rawY = 200;
6843 int32_t rawTouchMajor = 7;
6844 int32_t rawTouchMinor = 6;
6845 int32_t rawToolMajor = 9;
6846 int32_t rawToolMinor = 8;
6847 int32_t rawPressure = 11;
6848 int32_t rawDistance = 0;
6849 int32_t rawOrientation = 3;
6850 int32_t id = 5;
6851
6852 float x = toDisplayX(rawX);
6853 float y = toDisplayY(rawY);
6854 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6855 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6856 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6857 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6858 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6859 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6860 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6861 float distance = float(rawDistance);
6862
6863 processPosition(mapper, rawX, rawY);
6864 processTouchMajor(mapper, rawTouchMajor);
6865 processTouchMinor(mapper, rawTouchMinor);
6866 processToolMajor(mapper, rawToolMajor);
6867 processToolMinor(mapper, rawToolMinor);
6868 processPressure(mapper, rawPressure);
6869 processOrientation(mapper, rawOrientation);
6870 processDistance(mapper, rawDistance);
6871 processId(mapper, id);
6872 processMTSync(mapper);
6873 processSync(mapper);
6874
6875 NotifyMotionArgs args;
6876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6877 ASSERT_EQ(0, args.pointerProperties[0].id);
6878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6879 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6880 orientation, distance));
6881}
6882
6883TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006884 addConfigurationProperty("touch.deviceType", "touchScreen");
6885 prepareDisplay(DISPLAY_ORIENTATION_0);
6886 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6887 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006888 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006889
6890 // These calculations are based on the input device calibration documentation.
6891 int32_t rawX = 100;
6892 int32_t rawY = 200;
6893 int32_t rawTouchMajor = 140;
6894 int32_t rawTouchMinor = 120;
6895 int32_t rawToolMajor = 180;
6896 int32_t rawToolMinor = 160;
6897
6898 float x = toDisplayX(rawX);
6899 float y = toDisplayY(rawY);
6900 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6901 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6902 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6903 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6904 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6905
6906 processPosition(mapper, rawX, rawY);
6907 processTouchMajor(mapper, rawTouchMajor);
6908 processTouchMinor(mapper, rawTouchMinor);
6909 processToolMajor(mapper, rawToolMajor);
6910 processToolMinor(mapper, rawToolMinor);
6911 processMTSync(mapper);
6912 processSync(mapper);
6913
6914 NotifyMotionArgs args;
6915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6917 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6918}
6919
6920TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006921 addConfigurationProperty("touch.deviceType", "touchScreen");
6922 prepareDisplay(DISPLAY_ORIENTATION_0);
6923 prepareAxes(POSITION | TOUCH | TOOL);
6924 addConfigurationProperty("touch.size.calibration", "diameter");
6925 addConfigurationProperty("touch.size.scale", "10");
6926 addConfigurationProperty("touch.size.bias", "160");
6927 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006928 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006929
6930 // These calculations are based on the input device calibration documentation.
6931 // Note: We only provide a single common touch/tool value because the device is assumed
6932 // not to emit separate values for each pointer (isSummed = 1).
6933 int32_t rawX = 100;
6934 int32_t rawY = 200;
6935 int32_t rawX2 = 150;
6936 int32_t rawY2 = 250;
6937 int32_t rawTouchMajor = 5;
6938 int32_t rawToolMajor = 8;
6939
6940 float x = toDisplayX(rawX);
6941 float y = toDisplayY(rawY);
6942 float x2 = toDisplayX(rawX2);
6943 float y2 = toDisplayY(rawY2);
6944 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6945 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6946 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6947
6948 processPosition(mapper, rawX, rawY);
6949 processTouchMajor(mapper, rawTouchMajor);
6950 processToolMajor(mapper, rawToolMajor);
6951 processMTSync(mapper);
6952 processPosition(mapper, rawX2, rawY2);
6953 processTouchMajor(mapper, rawTouchMajor);
6954 processToolMajor(mapper, rawToolMajor);
6955 processMTSync(mapper);
6956 processSync(mapper);
6957
6958 NotifyMotionArgs args;
6959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6960 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6961
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6963 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6964 args.action);
6965 ASSERT_EQ(size_t(2), args.pointerCount);
6966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6967 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6969 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6970}
6971
6972TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006973 addConfigurationProperty("touch.deviceType", "touchScreen");
6974 prepareDisplay(DISPLAY_ORIENTATION_0);
6975 prepareAxes(POSITION | TOUCH | TOOL);
6976 addConfigurationProperty("touch.size.calibration", "area");
6977 addConfigurationProperty("touch.size.scale", "43");
6978 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006979 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006980
6981 // These calculations are based on the input device calibration documentation.
6982 int32_t rawX = 100;
6983 int32_t rawY = 200;
6984 int32_t rawTouchMajor = 5;
6985 int32_t rawToolMajor = 8;
6986
6987 float x = toDisplayX(rawX);
6988 float y = toDisplayY(rawY);
6989 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6990 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6991 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6992
6993 processPosition(mapper, rawX, rawY);
6994 processTouchMajor(mapper, rawTouchMajor);
6995 processToolMajor(mapper, rawToolMajor);
6996 processMTSync(mapper);
6997 processSync(mapper);
6998
6999 NotifyMotionArgs args;
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7002 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7003}
7004
7005TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007006 addConfigurationProperty("touch.deviceType", "touchScreen");
7007 prepareDisplay(DISPLAY_ORIENTATION_0);
7008 prepareAxes(POSITION | PRESSURE);
7009 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7010 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007011 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007012
Michael Wrightaa449c92017-12-13 21:21:43 +00007013 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007014 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007015 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7016 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7017 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7018
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019 // These calculations are based on the input device calibration documentation.
7020 int32_t rawX = 100;
7021 int32_t rawY = 200;
7022 int32_t rawPressure = 60;
7023
7024 float x = toDisplayX(rawX);
7025 float y = toDisplayY(rawY);
7026 float pressure = float(rawPressure) * 0.01f;
7027
7028 processPosition(mapper, rawX, rawY);
7029 processPressure(mapper, rawPressure);
7030 processMTSync(mapper);
7031 processSync(mapper);
7032
7033 NotifyMotionArgs args;
7034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7036 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7037}
7038
7039TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007040 addConfigurationProperty("touch.deviceType", "touchScreen");
7041 prepareDisplay(DISPLAY_ORIENTATION_0);
7042 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007043 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007044
7045 NotifyMotionArgs motionArgs;
7046 NotifyKeyArgs keyArgs;
7047
7048 processId(mapper, 1);
7049 processPosition(mapper, 100, 200);
7050 processSync(mapper);
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7052 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7053 ASSERT_EQ(0, motionArgs.buttonState);
7054
7055 // press BTN_LEFT, release BTN_LEFT
7056 processKey(mapper, BTN_LEFT, 1);
7057 processSync(mapper);
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7060 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7061
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7064 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7065
Michael Wrightd02c5b62014-02-10 15:10:22 -08007066 processKey(mapper, BTN_LEFT, 0);
7067 processSync(mapper);
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007071
7072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007074 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007075
7076 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7077 processKey(mapper, BTN_RIGHT, 1);
7078 processKey(mapper, BTN_MIDDLE, 1);
7079 processSync(mapper);
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7082 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7083 motionArgs.buttonState);
7084
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7087 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7088
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7091 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7092 motionArgs.buttonState);
7093
Michael Wrightd02c5b62014-02-10 15:10:22 -08007094 processKey(mapper, BTN_RIGHT, 0);
7095 processSync(mapper);
7096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007097 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007099
7100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007102 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103
7104 processKey(mapper, BTN_MIDDLE, 0);
7105 processSync(mapper);
7106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007108 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007109
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007112 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113
7114 // press BTN_BACK, release BTN_BACK
7115 processKey(mapper, BTN_BACK, 1);
7116 processSync(mapper);
7117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7118 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7119 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007120
Michael Wrightd02c5b62014-02-10 15:10:22 -08007121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007123 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7124
7125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7127 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128
7129 processKey(mapper, BTN_BACK, 0);
7130 processSync(mapper);
7131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007133 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007134
7135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007137 ASSERT_EQ(0, motionArgs.buttonState);
7138
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7140 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7141 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7142
7143 // press BTN_SIDE, release BTN_SIDE
7144 processKey(mapper, BTN_SIDE, 1);
7145 processSync(mapper);
7146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7147 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7148 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007149
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007152 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7153
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7156 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157
7158 processKey(mapper, BTN_SIDE, 0);
7159 processSync(mapper);
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007162 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007163
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007166 ASSERT_EQ(0, motionArgs.buttonState);
7167
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7169 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7170 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7171
7172 // press BTN_FORWARD, release BTN_FORWARD
7173 processKey(mapper, BTN_FORWARD, 1);
7174 processSync(mapper);
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7176 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7177 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007178
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007181 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7182
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7185 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007186
7187 processKey(mapper, BTN_FORWARD, 0);
7188 processSync(mapper);
7189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007191 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007192
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007195 ASSERT_EQ(0, motionArgs.buttonState);
7196
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7198 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7199 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7200
7201 // press BTN_EXTRA, release BTN_EXTRA
7202 processKey(mapper, BTN_EXTRA, 1);
7203 processSync(mapper);
7204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7205 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7206 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007207
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007210 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7211
7212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7214 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215
7216 processKey(mapper, BTN_EXTRA, 0);
7217 processSync(mapper);
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007220 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007221
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007224 ASSERT_EQ(0, motionArgs.buttonState);
7225
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7227 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7228 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7229
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7231
Michael Wrightd02c5b62014-02-10 15:10:22 -08007232 // press BTN_STYLUS, release BTN_STYLUS
7233 processKey(mapper, BTN_STYLUS, 1);
7234 processSync(mapper);
7235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007237 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7238
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7241 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007242
7243 processKey(mapper, BTN_STYLUS, 0);
7244 processSync(mapper);
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007248
7249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007251 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007252
7253 // press BTN_STYLUS2, release BTN_STYLUS2
7254 processKey(mapper, BTN_STYLUS2, 1);
7255 processSync(mapper);
7256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007258 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, 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_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007263
7264 processKey(mapper, BTN_STYLUS2, 0);
7265 processSync(mapper);
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007268 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007269
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007272 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007273
7274 // release touch
7275 processId(mapper, -1);
7276 processSync(mapper);
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7278 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7279 ASSERT_EQ(0, motionArgs.buttonState);
7280}
7281
7282TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007283 addConfigurationProperty("touch.deviceType", "touchScreen");
7284 prepareDisplay(DISPLAY_ORIENTATION_0);
7285 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007286 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007287
7288 NotifyMotionArgs motionArgs;
7289
7290 // default tool type is finger
7291 processId(mapper, 1);
7292 processPosition(mapper, 100, 200);
7293 processSync(mapper);
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7297
7298 // eraser
7299 processKey(mapper, BTN_TOOL_RUBBER, 1);
7300 processSync(mapper);
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7304
7305 // stylus
7306 processKey(mapper, BTN_TOOL_RUBBER, 0);
7307 processKey(mapper, BTN_TOOL_PEN, 1);
7308 processSync(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7312
7313 // brush
7314 processKey(mapper, BTN_TOOL_PEN, 0);
7315 processKey(mapper, BTN_TOOL_BRUSH, 1);
7316 processSync(mapper);
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7320
7321 // pencil
7322 processKey(mapper, BTN_TOOL_BRUSH, 0);
7323 processKey(mapper, BTN_TOOL_PENCIL, 1);
7324 processSync(mapper);
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7328
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007329 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007330 processKey(mapper, BTN_TOOL_PENCIL, 0);
7331 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7332 processSync(mapper);
7333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7336
7337 // mouse
7338 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7339 processKey(mapper, BTN_TOOL_MOUSE, 1);
7340 processSync(mapper);
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7344
7345 // lens
7346 processKey(mapper, BTN_TOOL_MOUSE, 0);
7347 processKey(mapper, BTN_TOOL_LENS, 1);
7348 processSync(mapper);
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7352
7353 // double-tap
7354 processKey(mapper, BTN_TOOL_LENS, 0);
7355 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7356 processSync(mapper);
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7360
7361 // triple-tap
7362 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7363 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7364 processSync(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7368
7369 // quad-tap
7370 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7371 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7372 processSync(mapper);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7376
7377 // finger
7378 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7379 processKey(mapper, BTN_TOOL_FINGER, 1);
7380 processSync(mapper);
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7384
7385 // stylus trumps finger
7386 processKey(mapper, BTN_TOOL_PEN, 1);
7387 processSync(mapper);
7388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7391
7392 // eraser trumps stylus
7393 processKey(mapper, BTN_TOOL_RUBBER, 1);
7394 processSync(mapper);
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7398
7399 // mouse trumps eraser
7400 processKey(mapper, BTN_TOOL_MOUSE, 1);
7401 processSync(mapper);
7402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7405
7406 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7407 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7408 processSync(mapper);
7409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7412
7413 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7414 processToolType(mapper, MT_TOOL_PEN);
7415 processSync(mapper);
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7419
7420 // back to default tool type
7421 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7422 processKey(mapper, BTN_TOOL_MOUSE, 0);
7423 processKey(mapper, BTN_TOOL_RUBBER, 0);
7424 processKey(mapper, BTN_TOOL_PEN, 0);
7425 processKey(mapper, BTN_TOOL_FINGER, 0);
7426 processSync(mapper);
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7430}
7431
7432TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007433 addConfigurationProperty("touch.deviceType", "touchScreen");
7434 prepareDisplay(DISPLAY_ORIENTATION_0);
7435 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007436 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007437 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007438
7439 NotifyMotionArgs motionArgs;
7440
7441 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7442 processId(mapper, 1);
7443 processPosition(mapper, 100, 200);
7444 processSync(mapper);
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7448 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7449
7450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7453 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7454
7455 // move a little
7456 processPosition(mapper, 150, 250);
7457 processSync(mapper);
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7459 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7461 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7462
7463 // down when BTN_TOUCH is pressed, pressure defaults to 1
7464 processKey(mapper, BTN_TOUCH, 1);
7465 processSync(mapper);
7466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7467 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7469 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7470
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7472 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7474 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7475
7476 // up when BTN_TOUCH is released, hover restored
7477 processKey(mapper, BTN_TOUCH, 0);
7478 processSync(mapper);
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7480 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7482 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7483
7484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7485 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7487 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7488
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7490 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7492 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7493
7494 // exit hover when pointer goes away
7495 processId(mapper, -1);
7496 processSync(mapper);
7497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7498 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7500 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7501}
7502
7503TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007504 addConfigurationProperty("touch.deviceType", "touchScreen");
7505 prepareDisplay(DISPLAY_ORIENTATION_0);
7506 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007507 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007508
7509 NotifyMotionArgs motionArgs;
7510
7511 // initially hovering because pressure is 0
7512 processId(mapper, 1);
7513 processPosition(mapper, 100, 200);
7514 processPressure(mapper, 0);
7515 processSync(mapper);
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7517 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7519 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7520
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7522 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7524 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7525
7526 // move a little
7527 processPosition(mapper, 150, 250);
7528 processSync(mapper);
7529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7532 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7533
7534 // down when pressure becomes non-zero
7535 processPressure(mapper, RAW_PRESSURE_MAX);
7536 processSync(mapper);
7537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7540 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7541
7542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7545 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7546
7547 // up when pressure becomes 0, hover restored
7548 processPressure(mapper, 0);
7549 processSync(mapper);
7550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7551 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7553 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7554
7555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7556 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7558 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7559
7560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7561 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7563 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7564
7565 // exit hover when pointer goes away
7566 processId(mapper, -1);
7567 processSync(mapper);
7568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7571 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7572}
7573
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007574/**
7575 * Set the input device port <--> display port associations, and check that the
7576 * events are routed to the display that matches the display port.
7577 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7578 */
7579TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007580 const std::string usb2 = "USB2";
7581 const uint8_t hdmi1 = 0;
7582 const uint8_t hdmi2 = 1;
7583 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007584 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007585
7586 addConfigurationProperty("touch.deviceType", "touchScreen");
7587 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007588 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007589
7590 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7591 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7592
7593 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7594 // for this input device is specified, and the matching viewport is not present,
7595 // the input device should be disabled (at the mapper level).
7596
7597 // Add viewport for display 2 on hdmi2
7598 prepareSecondaryDisplay(type, hdmi2);
7599 // Send a touch event
7600 processPosition(mapper, 100, 100);
7601 processSync(mapper);
7602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7603
7604 // Add viewport for display 1 on hdmi1
7605 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7606 // Send a touch event again
7607 processPosition(mapper, 100, 100);
7608 processSync(mapper);
7609
7610 NotifyMotionArgs args;
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7612 ASSERT_EQ(DISPLAY_ID, args.displayId);
7613}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007614
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007615TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007616 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007617 std::shared_ptr<FakePointerController> fakePointerController =
7618 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007619 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007620 fakePointerController->setPosition(100, 200);
7621 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00007622 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007623
Garfield Tan888a6a42020-01-09 11:39:16 -08007624 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007625 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007626
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007627 prepareDisplay(DISPLAY_ORIENTATION_0);
7628 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007629 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007630
7631 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007632 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007633
7634 NotifyMotionArgs motionArgs;
7635 processPosition(mapper, 100, 100);
7636 processSync(mapper);
7637
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7640 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7641}
7642
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007643/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007644 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7645 */
7646TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7647 addConfigurationProperty("touch.deviceType", "touchScreen");
7648 prepareAxes(POSITION);
7649 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7650
7651 prepareDisplay(DISPLAY_ORIENTATION_0);
7652 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7653 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7654 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7655 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7656
7657 NotifyMotionArgs args;
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7659 ASSERT_EQ(26, args.readTime);
7660
7661 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7662 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7663 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7664
7665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7666 ASSERT_EQ(33, args.readTime);
7667}
7668
7669/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007670 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7671 * events should not be delivered to the listener.
7672 */
7673TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7674 addConfigurationProperty("touch.deviceType", "touchScreen");
7675 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7676 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7677 ViewportType::INTERNAL);
7678 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7679 prepareAxes(POSITION);
7680 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7681
7682 NotifyMotionArgs motionArgs;
7683 processPosition(mapper, 100, 100);
7684 processSync(mapper);
7685
7686 mFakeListener->assertNotifyMotionWasNotCalled();
7687}
7688
Garfield Tanc734e4f2021-01-15 20:01:39 -08007689TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7690 addConfigurationProperty("touch.deviceType", "touchScreen");
7691 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7692 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7693 ViewportType::INTERNAL);
7694 std::optional<DisplayViewport> optionalDisplayViewport =
7695 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7696 ASSERT_TRUE(optionalDisplayViewport.has_value());
7697 DisplayViewport displayViewport = *optionalDisplayViewport;
7698
7699 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7700 prepareAxes(POSITION);
7701 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7702
7703 // Finger down
7704 int32_t x = 100, y = 100;
7705 processPosition(mapper, x, y);
7706 processSync(mapper);
7707
7708 NotifyMotionArgs motionArgs;
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7710 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7711
7712 // Deactivate display viewport
7713 displayViewport.isActive = false;
7714 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7715 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7716
7717 // Finger move
7718 x += 10, y += 10;
7719 processPosition(mapper, x, y);
7720 processSync(mapper);
7721
7722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7723 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7724
7725 // Reactivate display viewport
7726 displayViewport.isActive = true;
7727 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7728 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7729
7730 // Finger move again
7731 x += 10, y += 10;
7732 processPosition(mapper, x, y);
7733 processSync(mapper);
7734
7735 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7736 // no pointer on the touch device.
7737 mFakeListener->assertNotifyMotionWasNotCalled();
7738}
7739
Arthur Hung7c645402019-01-25 17:45:42 +08007740TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7741 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007742 prepareAxes(POSITION | ID | SLOT);
7743 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007744 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007745
7746 // Create the second touch screen device, and enable multi fingers.
7747 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007748 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007749 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007750 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007751 std::shared_ptr<InputDevice> device2 =
7752 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7753 Flags<InputDeviceClass>(0));
7754
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007755 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7756 0 /*flat*/, 0 /*fuzz*/);
7757 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7758 0 /*flat*/, 0 /*fuzz*/);
7759 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7760 0 /*flat*/, 0 /*fuzz*/);
7761 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7762 0 /*flat*/, 0 /*fuzz*/);
7763 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7764 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7765 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007766
7767 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007768 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007769 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7770 device2->reset(ARBITRARY_TIME);
7771
7772 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007773 std::shared_ptr<FakePointerController> fakePointerController =
7774 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00007775 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08007776
7777 // Setup policy for associated displays and show touches.
7778 const uint8_t hdmi1 = 0;
7779 const uint8_t hdmi2 = 1;
7780 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7781 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7782 mFakePolicy->setShowTouches(true);
7783
7784 // Create displays.
7785 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007786 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007787
7788 // Default device will reconfigure above, need additional reconfiguration for another device.
7789 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007790 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007791
7792 // Two fingers down at default display.
7793 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7794 processPosition(mapper, x1, y1);
7795 processId(mapper, 1);
7796 processSlot(mapper, 1);
7797 processPosition(mapper, x2, y2);
7798 processId(mapper, 2);
7799 processSync(mapper);
7800
7801 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7802 fakePointerController->getSpots().find(DISPLAY_ID);
7803 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7804 ASSERT_EQ(size_t(2), iter->second.size());
7805
7806 // Two fingers down at second display.
7807 processPosition(mapper2, x1, y1);
7808 processId(mapper2, 1);
7809 processSlot(mapper2, 1);
7810 processPosition(mapper2, x2, y2);
7811 processId(mapper2, 2);
7812 processSync(mapper2);
7813
7814 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7815 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7816 ASSERT_EQ(size_t(2), iter->second.size());
7817}
7818
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007819TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007820 prepareAxes(POSITION);
7821 addConfigurationProperty("touch.deviceType", "touchScreen");
7822 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007823 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007824
7825 NotifyMotionArgs motionArgs;
7826 // Unrotated video frame
7827 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7828 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007829 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007830 processPosition(mapper, 100, 200);
7831 processSync(mapper);
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7833 ASSERT_EQ(frames, motionArgs.videoFrames);
7834
7835 // Subsequent touch events should not have any videoframes
7836 // This is implemented separately in FakeEventHub,
7837 // but that should match the behaviour of TouchVideoDevice.
7838 processPosition(mapper, 200, 200);
7839 processSync(mapper);
7840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7841 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7842}
7843
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007844TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007845 prepareAxes(POSITION);
7846 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007847 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007848 // Unrotated video frame
7849 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7850 NotifyMotionArgs motionArgs;
7851
7852 // Test all 4 orientations
7853 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007854 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7855 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7856 clearViewports();
7857 prepareDisplay(orientation);
7858 std::vector<TouchVideoFrame> frames{frame};
7859 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
7860 processPosition(mapper, 100, 200);
7861 processSync(mapper);
7862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7863 ASSERT_EQ(frames, motionArgs.videoFrames);
7864 }
7865}
7866
7867TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
7868 prepareAxes(POSITION);
7869 addConfigurationProperty("touch.deviceType", "touchScreen");
7870 // Since InputReader works in the un-rotated coordinate space, only devices that are not
7871 // orientation-aware are affected by display rotation.
7872 addConfigurationProperty("touch.orientationAware", "0");
7873 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7874 // Unrotated video frame
7875 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7876 NotifyMotionArgs motionArgs;
7877
7878 // Test all 4 orientations
7879 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007880 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7881 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7882 clearViewports();
7883 prepareDisplay(orientation);
7884 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007885 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007886 processPosition(mapper, 100, 200);
7887 processSync(mapper);
7888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007889 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
7890 // compared to the display. This is so that when the window transform (which contains the
7891 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
7892 // window's coordinate space.
7893 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007894 ASSERT_EQ(frames, motionArgs.videoFrames);
7895 }
7896}
7897
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007898TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007899 prepareAxes(POSITION);
7900 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007901 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007902 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7903 // so mix these.
7904 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7905 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7906 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7907 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7908 NotifyMotionArgs motionArgs;
7909
7910 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007911 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007912 processPosition(mapper, 100, 200);
7913 processSync(mapper);
7914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007915 ASSERT_EQ(frames, motionArgs.videoFrames);
7916}
7917
7918TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
7919 prepareAxes(POSITION);
7920 addConfigurationProperty("touch.deviceType", "touchScreen");
7921 // Since InputReader works in the un-rotated coordinate space, only devices that are not
7922 // orientation-aware are affected by display rotation.
7923 addConfigurationProperty("touch.orientationAware", "0");
7924 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7925 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7926 // so mix these.
7927 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7928 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7929 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7930 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7931 NotifyMotionArgs motionArgs;
7932
7933 prepareDisplay(DISPLAY_ORIENTATION_90);
7934 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
7935 processPosition(mapper, 100, 200);
7936 processSync(mapper);
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7938 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
7939 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
7940 // compared to the display. This is so that when the window transform (which contains the
7941 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
7942 // window's coordinate space.
7943 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
7944 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007945 ASSERT_EQ(frames, motionArgs.videoFrames);
7946}
7947
Arthur Hung9da14732019-09-02 16:16:58 +08007948/**
7949 * If we had defined port associations, but the viewport is not ready, the touch device would be
7950 * expected to be disabled, and it should be enabled after the viewport has found.
7951 */
7952TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007953 constexpr uint8_t hdmi2 = 1;
7954 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007955 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007956
7957 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7958
7959 addConfigurationProperty("touch.deviceType", "touchScreen");
7960 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007961 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007962
7963 ASSERT_EQ(mDevice->isEnabled(), false);
7964
7965 // Add display on hdmi2, the device should be enabled and can receive touch event.
7966 prepareSecondaryDisplay(type, hdmi2);
7967 ASSERT_EQ(mDevice->isEnabled(), true);
7968
7969 // Send a touch event.
7970 processPosition(mapper, 100, 100);
7971 processSync(mapper);
7972
7973 NotifyMotionArgs args;
7974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7975 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7976}
7977
Arthur Hung421eb1c2020-01-16 00:09:42 +08007978TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007979 addConfigurationProperty("touch.deviceType", "touchScreen");
7980 prepareDisplay(DISPLAY_ORIENTATION_0);
7981 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007982 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007983
7984 NotifyMotionArgs motionArgs;
7985
7986 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7987 // finger down
7988 processId(mapper, 1);
7989 processPosition(mapper, x1, y1);
7990 processSync(mapper);
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7992 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7994
7995 // finger move
7996 processId(mapper, 1);
7997 processPosition(mapper, x2, y2);
7998 processSync(mapper);
7999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8000 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8001 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8002
8003 // finger up.
8004 processId(mapper, -1);
8005 processSync(mapper);
8006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8007 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8009
8010 // new finger down
8011 processId(mapper, 1);
8012 processPosition(mapper, x3, y3);
8013 processSync(mapper);
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8015 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8016 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8017}
8018
8019/**
arthurhungcc7f9802020-04-30 17:55:40 +08008020 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8021 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008022 */
arthurhungcc7f9802020-04-30 17:55:40 +08008023TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008024 addConfigurationProperty("touch.deviceType", "touchScreen");
8025 prepareDisplay(DISPLAY_ORIENTATION_0);
8026 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008027 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008028
8029 NotifyMotionArgs motionArgs;
8030
8031 // default tool type is finger
8032 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008033 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008034 processPosition(mapper, x1, y1);
8035 processSync(mapper);
8036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8037 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8038 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8039
8040 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8041 processToolType(mapper, MT_TOOL_PALM);
8042 processSync(mapper);
8043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8044 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8045
8046 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008047 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008048 processPosition(mapper, x2, y2);
8049 processSync(mapper);
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8051
8052 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008053 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008054 processSync(mapper);
8055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8056
8057 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008058 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008059 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008060 processPosition(mapper, x3, y3);
8061 processSync(mapper);
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8063 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8064 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8065}
8066
arthurhungbf89a482020-04-17 17:37:55 +08008067/**
arthurhungcc7f9802020-04-30 17:55:40 +08008068 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8069 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008070 */
arthurhungcc7f9802020-04-30 17:55:40 +08008071TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008072 addConfigurationProperty("touch.deviceType", "touchScreen");
8073 prepareDisplay(DISPLAY_ORIENTATION_0);
8074 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8075 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8076
8077 NotifyMotionArgs motionArgs;
8078
8079 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008080 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8081 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008082 processPosition(mapper, x1, y1);
8083 processSync(mapper);
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8087
8088 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008089 processSlot(mapper, SECOND_SLOT);
8090 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008091 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008092 processSync(mapper);
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8094 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8095 motionArgs.action);
8096 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8097
8098 // If the tool type of the first finger changes to MT_TOOL_PALM,
8099 // we expect to receive ACTION_POINTER_UP with cancel flag.
8100 processSlot(mapper, FIRST_SLOT);
8101 processId(mapper, FIRST_TRACKING_ID);
8102 processToolType(mapper, MT_TOOL_PALM);
8103 processSync(mapper);
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8105 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8106 motionArgs.action);
8107 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8108
8109 // The following MOVE events of second finger should be processed.
8110 processSlot(mapper, SECOND_SLOT);
8111 processId(mapper, SECOND_TRACKING_ID);
8112 processPosition(mapper, x2 + 1, y2 + 1);
8113 processSync(mapper);
8114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8116 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8117
8118 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8119 // it. Second finger receive move.
8120 processSlot(mapper, FIRST_SLOT);
8121 processId(mapper, INVALID_TRACKING_ID);
8122 processSync(mapper);
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8125 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8126
8127 // Second finger keeps moving.
8128 processSlot(mapper, SECOND_SLOT);
8129 processId(mapper, SECOND_TRACKING_ID);
8130 processPosition(mapper, x2 + 2, y2 + 2);
8131 processSync(mapper);
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8134 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8135
8136 // Second finger up.
8137 processId(mapper, INVALID_TRACKING_ID);
8138 processSync(mapper);
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8140 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8141 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8142}
8143
8144/**
8145 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8146 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8147 */
8148TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8149 addConfigurationProperty("touch.deviceType", "touchScreen");
8150 prepareDisplay(DISPLAY_ORIENTATION_0);
8151 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8152 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8153
8154 NotifyMotionArgs motionArgs;
8155
8156 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8157 // First finger down.
8158 processId(mapper, FIRST_TRACKING_ID);
8159 processPosition(mapper, x1, y1);
8160 processSync(mapper);
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8162 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8164
8165 // Second finger down.
8166 processSlot(mapper, SECOND_SLOT);
8167 processId(mapper, SECOND_TRACKING_ID);
8168 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008169 processSync(mapper);
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8171 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8172 motionArgs.action);
8173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8174
arthurhungcc7f9802020-04-30 17:55:40 +08008175 // If the tool type of the first finger changes to MT_TOOL_PALM,
8176 // we expect to receive ACTION_POINTER_UP with cancel flag.
8177 processSlot(mapper, FIRST_SLOT);
8178 processId(mapper, FIRST_TRACKING_ID);
8179 processToolType(mapper, MT_TOOL_PALM);
8180 processSync(mapper);
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8183 motionArgs.action);
8184 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8185
8186 // Second finger keeps moving.
8187 processSlot(mapper, SECOND_SLOT);
8188 processId(mapper, SECOND_TRACKING_ID);
8189 processPosition(mapper, x2 + 1, y2 + 1);
8190 processSync(mapper);
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8193
8194 // second finger becomes palm, receive cancel due to only 1 finger is active.
8195 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008196 processToolType(mapper, MT_TOOL_PALM);
8197 processSync(mapper);
8198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8199 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8200
arthurhungcc7f9802020-04-30 17:55:40 +08008201 // third finger down.
8202 processSlot(mapper, THIRD_SLOT);
8203 processId(mapper, THIRD_TRACKING_ID);
8204 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008205 processPosition(mapper, x3, y3);
8206 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8208 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008210 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8211
8212 // third finger move
8213 processId(mapper, THIRD_TRACKING_ID);
8214 processPosition(mapper, x3 + 1, y3 + 1);
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8218
8219 // first finger up, third finger receive move.
8220 processSlot(mapper, FIRST_SLOT);
8221 processId(mapper, INVALID_TRACKING_ID);
8222 processSync(mapper);
8223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8225 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8226
8227 // second finger up, third finger receive move.
8228 processSlot(mapper, SECOND_SLOT);
8229 processId(mapper, INVALID_TRACKING_ID);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8233 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8234
8235 // third finger up.
8236 processSlot(mapper, THIRD_SLOT);
8237 processId(mapper, INVALID_TRACKING_ID);
8238 processSync(mapper);
8239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8240 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8241 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8242}
8243
8244/**
8245 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8246 * and the active finger could still be allowed to receive the events
8247 */
8248TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8249 addConfigurationProperty("touch.deviceType", "touchScreen");
8250 prepareDisplay(DISPLAY_ORIENTATION_0);
8251 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8252 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8253
8254 NotifyMotionArgs motionArgs;
8255
8256 // default tool type is finger
8257 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8258 processId(mapper, FIRST_TRACKING_ID);
8259 processPosition(mapper, x1, y1);
8260 processSync(mapper);
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8264
8265 // Second finger down.
8266 processSlot(mapper, SECOND_SLOT);
8267 processId(mapper, SECOND_TRACKING_ID);
8268 processPosition(mapper, x2, y2);
8269 processSync(mapper);
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8271 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8272 motionArgs.action);
8273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8274
8275 // If the tool type of the second finger changes to MT_TOOL_PALM,
8276 // we expect to receive ACTION_POINTER_UP with cancel flag.
8277 processId(mapper, SECOND_TRACKING_ID);
8278 processToolType(mapper, MT_TOOL_PALM);
8279 processSync(mapper);
8280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8281 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8282 motionArgs.action);
8283 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8284
8285 // The following MOVE event should be processed.
8286 processSlot(mapper, FIRST_SLOT);
8287 processId(mapper, FIRST_TRACKING_ID);
8288 processPosition(mapper, x1 + 1, y1 + 1);
8289 processSync(mapper);
8290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8292 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8293
8294 // second finger up.
8295 processSlot(mapper, SECOND_SLOT);
8296 processId(mapper, INVALID_TRACKING_ID);
8297 processSync(mapper);
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8300
8301 // first finger keep moving
8302 processSlot(mapper, FIRST_SLOT);
8303 processId(mapper, FIRST_TRACKING_ID);
8304 processPosition(mapper, x1 + 2, y1 + 2);
8305 processSync(mapper);
8306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8308
8309 // first finger up.
8310 processId(mapper, INVALID_TRACKING_ID);
8311 processSync(mapper);
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8313 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8314 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008315}
8316
Arthur Hung9ad18942021-06-19 02:04:46 +00008317/**
8318 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8319 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8320 * cause slot be valid again.
8321 */
8322TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8323 addConfigurationProperty("touch.deviceType", "touchScreen");
8324 prepareDisplay(DISPLAY_ORIENTATION_0);
8325 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8326 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8327
8328 NotifyMotionArgs motionArgs;
8329
8330 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8331 // First finger down.
8332 processId(mapper, FIRST_TRACKING_ID);
8333 processPosition(mapper, x1, y1);
8334 processPressure(mapper, RAW_PRESSURE_MAX);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8337 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8338 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8339
8340 // First finger move.
8341 processId(mapper, FIRST_TRACKING_ID);
8342 processPosition(mapper, x1 + 1, y1 + 1);
8343 processPressure(mapper, RAW_PRESSURE_MAX);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8347 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8348
8349 // Second finger down.
8350 processSlot(mapper, SECOND_SLOT);
8351 processId(mapper, SECOND_TRACKING_ID);
8352 processPosition(mapper, x2, y2);
8353 processPressure(mapper, RAW_PRESSURE_MAX);
8354 processSync(mapper);
8355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8356 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8357 motionArgs.action);
8358 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8359
8360 // second finger up with some unexpected data.
8361 processSlot(mapper, SECOND_SLOT);
8362 processId(mapper, INVALID_TRACKING_ID);
8363 processPosition(mapper, x2, y2);
8364 processSync(mapper);
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8366 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8367 motionArgs.action);
8368 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8369
8370 // first finger up with some unexpected data.
8371 processSlot(mapper, FIRST_SLOT);
8372 processId(mapper, INVALID_TRACKING_ID);
8373 processPosition(mapper, x2, y2);
8374 processPressure(mapper, RAW_PRESSURE_MAX);
8375 processSync(mapper);
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8378 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8379}
8380
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008381// --- MultiTouchInputMapperTest_ExternalDevice ---
8382
8383class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8384protected:
Chris Yea52ade12020-08-27 16:49:20 -07008385 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008386};
8387
8388/**
8389 * Expect fallback to internal viewport if device is external and external viewport is not present.
8390 */
8391TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8392 prepareAxes(POSITION);
8393 addConfigurationProperty("touch.deviceType", "touchScreen");
8394 prepareDisplay(DISPLAY_ORIENTATION_0);
8395 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8396
8397 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8398
8399 NotifyMotionArgs motionArgs;
8400
8401 // Expect the event to be sent to the internal viewport,
8402 // because an external viewport is not present.
8403 processPosition(mapper, 100, 100);
8404 processSync(mapper);
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8406 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8407
8408 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008409 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008410 processPosition(mapper, 100, 100);
8411 processSync(mapper);
8412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8413 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8414}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008415
8416/**
8417 * Test touch should not work if outside of surface.
8418 */
8419class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8420protected:
8421 void halfDisplayToCenterHorizontal(int32_t orientation) {
8422 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008423 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008424
8425 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8426 internalViewport->orientation = orientation;
8427 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8428 internalViewport->logicalLeft = 0;
8429 internalViewport->logicalTop = 0;
8430 internalViewport->logicalRight = DISPLAY_HEIGHT;
8431 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8432
8433 internalViewport->physicalLeft = 0;
8434 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8435 internalViewport->physicalRight = DISPLAY_HEIGHT;
8436 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8437
8438 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8439 internalViewport->deviceHeight = DISPLAY_WIDTH;
8440 } else {
8441 internalViewport->logicalLeft = 0;
8442 internalViewport->logicalTop = 0;
8443 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8444 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8445
8446 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8447 internalViewport->physicalTop = 0;
8448 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8449 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8450
8451 internalViewport->deviceWidth = DISPLAY_WIDTH;
8452 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8453 }
8454
8455 mFakePolicy->updateViewport(internalViewport.value());
8456 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8457 }
8458
arthurhung5d547942020-12-14 17:04:45 +08008459 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8460 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008461 int32_t yExpected) {
8462 // touch on outside area should not work.
8463 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8464 processSync(mapper);
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8466
8467 // touch on inside area should receive the event.
8468 NotifyMotionArgs args;
8469 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8470 processSync(mapper);
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8472 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8473 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8474
8475 // Reset.
8476 mapper.reset(ARBITRARY_TIME);
8477 }
8478};
8479
arthurhung5d547942020-12-14 17:04:45 +08008480TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008481 addConfigurationProperty("touch.deviceType", "touchScreen");
8482 prepareDisplay(DISPLAY_ORIENTATION_0);
8483 prepareAxes(POSITION);
8484 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8485
8486 // Touch on center of normal display should work.
8487 const int32_t x = DISPLAY_WIDTH / 4;
8488 const int32_t y = DISPLAY_HEIGHT / 2;
8489 processPosition(mapper, toRawX(x), toRawY(y));
8490 processSync(mapper);
8491 NotifyMotionArgs args;
8492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8494 0.0f, 0.0f, 0.0f, 0.0f));
8495 // Reset.
8496 mapper.reset(ARBITRARY_TIME);
8497
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008498 // Let physical display be different to device, and make surface and physical could be 1:1 in
8499 // all four orientations.
8500 for (int orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
8501 DISPLAY_ORIENTATION_270}) {
8502 halfDisplayToCenterHorizontal(orientation);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008503
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008504 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8505 const int32_t yExpected = y;
8506 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8507 }
Arthur Hung4197f6b2020-03-16 15:39:59 +08008508}
8509
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008510TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90_NotOrientationAware) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008511 addConfigurationProperty("touch.deviceType", "touchScreen");
8512 prepareDisplay(DISPLAY_ORIENTATION_0);
8513 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008514 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8515 // orientation-aware are affected by display rotation.
8516 addConfigurationProperty("touch.orientationAware", "0");
Arthur Hung4197f6b2020-03-16 15:39:59 +08008517 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8518
8519 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8520 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8521
8522 const int32_t x = DISPLAY_WIDTH / 4;
8523 const int32_t y = DISPLAY_HEIGHT / 2;
8524
Arthur Hung4197f6b2020-03-16 15:39:59 +08008525 // expect x/y = swap x/y then reverse x.
8526 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8527 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8528 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8529}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008530
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008531TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270_NotOrientationAware) {
arthurhunga36b28e2020-12-29 20:28:15 +08008532 addConfigurationProperty("touch.deviceType", "touchScreen");
8533 prepareDisplay(DISPLAY_ORIENTATION_0);
8534 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008535 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8536 // orientation-aware are affected by display rotation.
8537 addConfigurationProperty("touch.orientationAware", "0");
8538 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8539
8540 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8541 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8542
8543 const int32_t x = DISPLAY_WIDTH / 4;
8544 const int32_t y = DISPLAY_HEIGHT / 2;
8545
8546 // expect x/y = swap x/y then reverse y.
8547 const int32_t xExpected = y;
8548 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8549 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8550}
8551
8552TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner_NotOrientationAware) {
8553 addConfigurationProperty("touch.deviceType", "touchScreen");
8554 prepareDisplay(DISPLAY_ORIENTATION_0);
8555 prepareAxes(POSITION);
8556 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8557 // orientation-aware are affected by display rotation.
8558 addConfigurationProperty("touch.orientationAware", "0");
arthurhunga36b28e2020-12-29 20:28:15 +08008559 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8560
8561 const int32_t x = 0;
8562 const int32_t y = 0;
8563
8564 const int32_t xExpected = x;
8565 const int32_t yExpected = y;
8566 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8567
8568 clearViewports();
8569 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008570 // expect x/y = swap x/y then reverse x.
8571 const int32_t xExpected90 = DISPLAY_HEIGHT - 1;
8572 const int32_t yExpected90 = x;
arthurhunga36b28e2020-12-29 20:28:15 +08008573 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8574
8575 clearViewports();
8576 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008577 // expect x/y = swap x/y then reverse y.
8578 const int32_t xExpected270 = y;
8579 const int32_t yExpected270 = DISPLAY_WIDTH - 1;
arthurhunga36b28e2020-12-29 20:28:15 +08008580 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8581}
8582
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008583TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8584 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8585 std::shared_ptr<FakePointerController> fakePointerController =
8586 std::make_shared<FakePointerController>();
8587 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8588 fakePointerController->setPosition(0, 0);
8589 fakePointerController->setButtonState(0);
8590
8591 // prepare device and capture
8592 prepareDisplay(DISPLAY_ORIENTATION_0);
8593 prepareAxes(POSITION | ID | SLOT);
8594 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8595 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8596 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008597 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008598 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8599
8600 // captured touchpad should be a touchpad source
8601 NotifyDeviceResetArgs resetArgs;
8602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8603 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8604
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008605 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008606
8607 const InputDeviceInfo::MotionRange* relRangeX =
8608 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8609 ASSERT_NE(relRangeX, nullptr);
8610 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8611 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8612 const InputDeviceInfo::MotionRange* relRangeY =
8613 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8614 ASSERT_NE(relRangeY, nullptr);
8615 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8616 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8617
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008618 // run captured pointer tests - note that this is unscaled, so input listener events should be
8619 // identical to what the hardware sends (accounting for any
8620 // calibration).
8621 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008622 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008623 processId(mapper, 1);
8624 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8625 processKey(mapper, BTN_TOUCH, 1);
8626 processSync(mapper);
8627
8628 // expect coord[0] to contain initial location of touch 0
8629 NotifyMotionArgs args;
8630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8631 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8632 ASSERT_EQ(1U, args.pointerCount);
8633 ASSERT_EQ(0, args.pointerProperties[0].id);
8634 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8635 ASSERT_NO_FATAL_FAILURE(
8636 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8637
8638 // FINGER 1 DOWN
8639 processSlot(mapper, 1);
8640 processId(mapper, 2);
8641 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8642 processSync(mapper);
8643
8644 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008646 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8647 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008648 ASSERT_EQ(2U, args.pointerCount);
8649 ASSERT_EQ(0, args.pointerProperties[0].id);
8650 ASSERT_EQ(1, args.pointerProperties[1].id);
8651 ASSERT_NO_FATAL_FAILURE(
8652 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8653 ASSERT_NO_FATAL_FAILURE(
8654 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8655
8656 // FINGER 1 MOVE
8657 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8658 processSync(mapper);
8659
8660 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8661 // from move
8662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8663 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8664 ASSERT_NO_FATAL_FAILURE(
8665 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8666 ASSERT_NO_FATAL_FAILURE(
8667 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8668
8669 // FINGER 0 MOVE
8670 processSlot(mapper, 0);
8671 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8672 processSync(mapper);
8673
8674 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8677 ASSERT_NO_FATAL_FAILURE(
8678 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8679 ASSERT_NO_FATAL_FAILURE(
8680 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8681
8682 // BUTTON DOWN
8683 processKey(mapper, BTN_LEFT, 1);
8684 processSync(mapper);
8685
8686 // touchinputmapper design sends a move before button press
8687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8690 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8691
8692 // BUTTON UP
8693 processKey(mapper, BTN_LEFT, 0);
8694 processSync(mapper);
8695
8696 // touchinputmapper design sends a move after button release
8697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8698 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8701
8702 // FINGER 0 UP
8703 processId(mapper, -1);
8704 processSync(mapper);
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8706 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8707
8708 // FINGER 1 MOVE
8709 processSlot(mapper, 1);
8710 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8711 processSync(mapper);
8712
8713 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8716 ASSERT_EQ(1U, args.pointerCount);
8717 ASSERT_EQ(1, args.pointerProperties[0].id);
8718 ASSERT_NO_FATAL_FAILURE(
8719 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8720
8721 // FINGER 1 UP
8722 processId(mapper, -1);
8723 processKey(mapper, BTN_TOUCH, 0);
8724 processSync(mapper);
8725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8726 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8727
8728 // non captured touchpad should be a mouse source
8729 mFakePolicy->setPointerCapture(false);
8730 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8732 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8733}
8734
8735TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8736 std::shared_ptr<FakePointerController> fakePointerController =
8737 std::make_shared<FakePointerController>();
8738 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8739 fakePointerController->setPosition(0, 0);
8740 fakePointerController->setButtonState(0);
8741
8742 // prepare device and capture
8743 prepareDisplay(DISPLAY_ORIENTATION_0);
8744 prepareAxes(POSITION | ID | SLOT);
8745 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8746 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008747 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008748 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8749 // run uncaptured pointer tests - pushes out generic events
8750 // FINGER 0 DOWN
8751 processId(mapper, 3);
8752 processPosition(mapper, 100, 100);
8753 processKey(mapper, BTN_TOUCH, 1);
8754 processSync(mapper);
8755
8756 // start at (100,100), cursor should be at (0,0) * scale
8757 NotifyMotionArgs args;
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8759 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8760 ASSERT_NO_FATAL_FAILURE(
8761 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8762
8763 // FINGER 0 MOVE
8764 processPosition(mapper, 200, 200);
8765 processSync(mapper);
8766
8767 // compute scaling to help with touch position checking
8768 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8769 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8770 float scale =
8771 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8772
8773 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8775 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8777 0, 0, 0, 0, 0, 0, 0));
8778}
8779
8780TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8781 std::shared_ptr<FakePointerController> fakePointerController =
8782 std::make_shared<FakePointerController>();
8783
8784 prepareDisplay(DISPLAY_ORIENTATION_0);
8785 prepareAxes(POSITION | ID | SLOT);
8786 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008787 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008788 mFakePolicy->setPointerCapture(false);
8789 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8790
8791 // uncaptured touchpad should be a pointer device
8792 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8793
8794 // captured touchpad should be a touchpad device
8795 mFakePolicy->setPointerCapture(true);
8796 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8797 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8798}
8799
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008800// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008801
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008802class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008803protected:
8804 static const char* DEVICE_NAME;
8805 static const char* DEVICE_LOCATION;
8806 static const int32_t DEVICE_ID;
8807 static const int32_t DEVICE_GENERATION;
8808 static const int32_t DEVICE_CONTROLLER_NUMBER;
8809 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8810 static const int32_t EVENTHUB_ID;
8811
8812 std::shared_ptr<FakeEventHub> mFakeEventHub;
8813 sp<FakeInputReaderPolicy> mFakePolicy;
8814 sp<TestInputListener> mFakeListener;
8815 std::unique_ptr<InstrumentedInputReader> mReader;
8816 std::shared_ptr<InputDevice> mDevice;
8817
8818 virtual void SetUp(Flags<InputDeviceClass> classes) {
8819 mFakeEventHub = std::make_unique<FakeEventHub>();
8820 mFakePolicy = new FakeInputReaderPolicy();
8821 mFakeListener = new TestInputListener();
8822 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8823 mFakeListener);
8824 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8825 }
8826
8827 void SetUp() override { SetUp(DEVICE_CLASSES); }
8828
8829 void TearDown() override {
8830 mFakeListener.clear();
8831 mFakePolicy.clear();
8832 }
8833
8834 void configureDevice(uint32_t changes) {
8835 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8836 mReader->requestRefreshConfiguration(changes);
8837 mReader->loopOnce();
8838 }
8839 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8840 }
8841
8842 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8843 const std::string& location, int32_t eventHubId,
8844 Flags<InputDeviceClass> classes) {
8845 InputDeviceIdentifier identifier;
8846 identifier.name = name;
8847 identifier.location = location;
8848 std::shared_ptr<InputDevice> device =
8849 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8850 identifier);
8851 mReader->pushNextDevice(device);
8852 mFakeEventHub->addDevice(eventHubId, name, classes);
8853 mReader->loopOnce();
8854 return device;
8855 }
8856
8857 template <class T, typename... Args>
8858 T& addControllerAndConfigure(Args... args) {
8859 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8860
8861 return controller;
8862 }
8863};
8864
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008865const char* PeripheralControllerTest::DEVICE_NAME = "device";
8866const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8867const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8868const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8869const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8870const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008871 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008872const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008873
8874// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008875class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008876protected:
8877 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008878 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008879 }
8880};
8881
8882TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008883 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008884
8885 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8886 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8887}
8888
8889TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008890 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008891
8892 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8893 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8894}
8895
8896// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008897class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008898protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008899 void SetUp() override {
8900 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8901 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008902};
8903
Chris Ye85758332021-05-16 23:05:17 -07008904TEST_F(LightControllerTest, MonoLight) {
8905 RawLightInfo infoMono = {.id = 1,
8906 .name = "Mono",
8907 .maxBrightness = 255,
8908 .flags = InputLightClass::BRIGHTNESS,
8909 .path = ""};
8910 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008911
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008912 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008913 InputDeviceInfo info;
8914 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008915 std::vector<InputDeviceLightInfo> lights = info.getLights();
8916 ASSERT_EQ(1U, lights.size());
8917 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008918
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008919 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
8920 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008921}
8922
8923TEST_F(LightControllerTest, RGBLight) {
8924 RawLightInfo infoRed = {.id = 1,
8925 .name = "red",
8926 .maxBrightness = 255,
8927 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8928 .path = ""};
8929 RawLightInfo infoGreen = {.id = 2,
8930 .name = "green",
8931 .maxBrightness = 255,
8932 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8933 .path = ""};
8934 RawLightInfo infoBlue = {.id = 3,
8935 .name = "blue",
8936 .maxBrightness = 255,
8937 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8938 .path = ""};
8939 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8940 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8941 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8942
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008943 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008944 InputDeviceInfo info;
8945 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008946 std::vector<InputDeviceLightInfo> lights = info.getLights();
8947 ASSERT_EQ(1U, lights.size());
8948 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008949
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008950 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8951 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008952}
8953
8954TEST_F(LightControllerTest, MultiColorRGBLight) {
8955 RawLightInfo infoColor = {.id = 1,
8956 .name = "red",
8957 .maxBrightness = 255,
8958 .flags = InputLightClass::BRIGHTNESS |
8959 InputLightClass::MULTI_INTENSITY |
8960 InputLightClass::MULTI_INDEX,
8961 .path = ""};
8962
8963 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
8964
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008965 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008966 InputDeviceInfo info;
8967 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008968 std::vector<InputDeviceLightInfo> lights = info.getLights();
8969 ASSERT_EQ(1U, lights.size());
8970 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008971
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008972 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8973 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008974}
8975
8976TEST_F(LightControllerTest, PlayerIdLight) {
8977 RawLightInfo info1 = {.id = 1,
8978 .name = "player1",
8979 .maxBrightness = 255,
8980 .flags = InputLightClass::BRIGHTNESS,
8981 .path = ""};
8982 RawLightInfo info2 = {.id = 2,
8983 .name = "player2",
8984 .maxBrightness = 255,
8985 .flags = InputLightClass::BRIGHTNESS,
8986 .path = ""};
8987 RawLightInfo info3 = {.id = 3,
8988 .name = "player3",
8989 .maxBrightness = 255,
8990 .flags = InputLightClass::BRIGHTNESS,
8991 .path = ""};
8992 RawLightInfo info4 = {.id = 4,
8993 .name = "player4",
8994 .maxBrightness = 255,
8995 .flags = InputLightClass::BRIGHTNESS,
8996 .path = ""};
8997 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
8998 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
8999 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9000 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9001
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009002 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009003 InputDeviceInfo info;
9004 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009005 std::vector<InputDeviceLightInfo> lights = info.getLights();
9006 ASSERT_EQ(1U, lights.size());
9007 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009008
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009009 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9010 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9011 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009012}
9013
Michael Wrightd02c5b62014-02-10 15:10:22 -08009014} // namespace android