blob: c76ed38adbcccd9df777b04de8a9279968088336 [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
Arthur Hungcb40a002021-08-03 14:31:01 +0000874 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
875 Device* device = getDevice(deviceId);
876 if (!device) {
877 return false;
878 }
879 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
880 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
881 return true;
882 }
883 }
884 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
885 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
886 return true;
887 }
888 }
889 return false;
890 }
891
Chris Yea52ade12020-08-27 16:49:20 -0700892 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800893 Device* device = getDevice(deviceId);
894 return device && device->leds.indexOfKey(led) >= 0;
895 }
896
Chris Yea52ade12020-08-27 16:49:20 -0700897 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 Device* device = getDevice(deviceId);
899 if (device) {
900 ssize_t index = device->leds.indexOfKey(led);
901 if (index >= 0) {
902 device->leds.replaceValueAt(led, on);
903 } else {
904 ADD_FAILURE()
905 << "Attempted to set the state of an LED that the EventHub declared "
906 "was not present. led=" << led;
907 }
908 }
909 }
910
Chris Yea52ade12020-08-27 16:49:20 -0700911 void getVirtualKeyDefinitions(
912 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 outVirtualKeys.clear();
914
915 Device* device = getDevice(deviceId);
916 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800917 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918 }
919 }
920
Chris Yea52ade12020-08-27 16:49:20 -0700921 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700922 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 }
924
Chris Yea52ade12020-08-27 16:49:20 -0700925 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 return false;
927 }
928
Chris Yea52ade12020-08-27 16:49:20 -0700929 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930
Chris Yea52ade12020-08-27 16:49:20 -0700931 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932
Chris Ye87143712020-11-10 05:05:58 +0000933 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
934
Chris Yee2b1e5c2021-03-10 22:45:12 -0800935 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
936 return BATTERY_CAPACITY;
937 }
Kim Low03ea0352020-11-06 12:45:07 -0800938
Chris Yee2b1e5c2021-03-10 22:45:12 -0800939 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
940 return BATTERY_STATUS;
941 }
942
943 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
944
945 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
946 return std::nullopt;
947 }
Kim Low03ea0352020-11-06 12:45:07 -0800948
Chris Ye3fdbfef2021-01-06 18:45:18 -0800949 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
950 std::vector<int32_t> ids;
951 for (const auto& [rawId, info] : mRawLightInfos) {
952 ids.push_back(rawId);
953 }
954 return ids;
955 }
956
957 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
958 auto it = mRawLightInfos.find(lightId);
959 if (it == mRawLightInfos.end()) {
960 return std::nullopt;
961 }
962 return it->second;
963 }
964
965 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
966 mLightBrightness.emplace(lightId, brightness);
967 }
968
969 void setLightIntensities(int32_t deviceId, int32_t lightId,
970 std::unordered_map<LightColor, int32_t> intensities) override {
971 mLightIntensities.emplace(lightId, intensities);
972 };
973
974 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
975 auto lightIt = mLightBrightness.find(lightId);
976 if (lightIt == mLightBrightness.end()) {
977 return std::nullopt;
978 }
979 return lightIt->second;
980 }
981
982 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
983 int32_t deviceId, int32_t lightId) override {
984 auto lightIt = mLightIntensities.find(lightId);
985 if (lightIt == mLightIntensities.end()) {
986 return std::nullopt;
987 }
988 return lightIt->second;
989 };
990
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100991 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800992 return false;
993 }
994
Chris Yea52ade12020-08-27 16:49:20 -0700995 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800996
Chris Yea52ade12020-08-27 16:49:20 -0700997 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998
Chris Yea52ade12020-08-27 16:49:20 -0700999 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000
Chris Yea52ade12020-08-27 16:49:20 -07001001 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002};
1003
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004// --- FakeInputMapper ---
1005
1006class FakeInputMapper : public InputMapper {
1007 uint32_t mSources;
1008 int32_t mKeyboardType;
1009 int32_t mMetaState;
1010 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1011 KeyedVector<int32_t, int32_t> mScanCodeStates;
1012 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001013 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001014
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001015 std::mutex mLock;
1016 std::condition_variable mStateChangedCondition;
1017 bool mConfigureWasCalled GUARDED_BY(mLock);
1018 bool mResetWasCalled GUARDED_BY(mLock);
1019 bool mProcessWasCalled GUARDED_BY(mLock);
1020 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021
Arthur Hungc23540e2018-11-29 20:42:11 +08001022 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001024 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1025 : InputMapper(deviceContext),
1026 mSources(sources),
1027 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001028 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001029 mConfigureWasCalled(false),
1030 mResetWasCalled(false),
1031 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032
Chris Yea52ade12020-08-27 16:49:20 -07001033 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034
1035 void setKeyboardType(int32_t keyboardType) {
1036 mKeyboardType = keyboardType;
1037 }
1038
1039 void setMetaState(int32_t metaState) {
1040 mMetaState = metaState;
1041 }
1042
1043 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001044 std::unique_lock<std::mutex> lock(mLock);
1045 base::ScopedLockAssertion assumeLocked(mLock);
1046 const bool configureCalled =
1047 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1048 return mConfigureWasCalled;
1049 });
1050 if (!configureCalled) {
1051 FAIL() << "Expected configure() to have been called.";
1052 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053 mConfigureWasCalled = false;
1054 }
1055
1056 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001057 std::unique_lock<std::mutex> lock(mLock);
1058 base::ScopedLockAssertion assumeLocked(mLock);
1059 const bool resetCalled =
1060 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1061 return mResetWasCalled;
1062 });
1063 if (!resetCalled) {
1064 FAIL() << "Expected reset() to have been called.";
1065 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001066 mResetWasCalled = false;
1067 }
1068
Yi Kong9b14ac62018-07-17 13:48:38 -07001069 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001070 std::unique_lock<std::mutex> lock(mLock);
1071 base::ScopedLockAssertion assumeLocked(mLock);
1072 const bool processCalled =
1073 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1074 return mProcessWasCalled;
1075 });
1076 if (!processCalled) {
1077 FAIL() << "Expected process() to have been called.";
1078 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079 if (outLastEvent) {
1080 *outLastEvent = mLastEvent;
1081 }
1082 mProcessWasCalled = false;
1083 }
1084
1085 void setKeyCodeState(int32_t keyCode, int32_t state) {
1086 mKeyCodeStates.replaceValueFor(keyCode, state);
1087 }
1088
1089 void setScanCodeState(int32_t scanCode, int32_t state) {
1090 mScanCodeStates.replaceValueFor(scanCode, state);
1091 }
1092
1093 void setSwitchState(int32_t switchCode, int32_t state) {
1094 mSwitchStates.replaceValueFor(switchCode, state);
1095 }
1096
1097 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001098 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099 }
1100
1101private:
Chris Yea52ade12020-08-27 16:49:20 -07001102 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103
Chris Yea52ade12020-08-27 16:49:20 -07001104 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105 InputMapper::populateDeviceInfo(deviceInfo);
1106
1107 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1108 deviceInfo->setKeyboardType(mKeyboardType);
1109 }
1110 }
1111
Chris Yea52ade12020-08-27 16:49:20 -07001112 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001113 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001114 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001115
1116 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001117 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001118 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1119 mViewport = config->getDisplayViewportByPort(*displayPort);
1120 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001121
1122 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 }
1124
Chris Yea52ade12020-08-27 16:49:20 -07001125 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001126 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001128 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 }
1130
Chris Yea52ade12020-08-27 16:49:20 -07001131 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001132 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133 mLastEvent = *rawEvent;
1134 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001135 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001136 }
1137
Chris Yea52ade12020-08-27 16:49:20 -07001138 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001139 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1140 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1141 }
1142
Chris Yea52ade12020-08-27 16:49:20 -07001143 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1145 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1146 }
1147
Chris Yea52ade12020-08-27 16:49:20 -07001148 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1150 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1151 }
1152
Chris Yea52ade12020-08-27 16:49:20 -07001153 // Return true if the device has non-empty key layout.
1154 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1155 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156 for (size_t i = 0; i < numCodes; i++) {
1157 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1158 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1159 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160 }
1161 }
1162 }
Chris Yea52ade12020-08-27 16:49:20 -07001163 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001164 return result;
1165 }
1166
1167 virtual int32_t getMetaState() {
1168 return mMetaState;
1169 }
1170
1171 virtual void fadePointer() {
1172 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001173
1174 virtual std::optional<int32_t> getAssociatedDisplay() {
1175 if (mViewport) {
1176 return std::make_optional(mViewport->displayId);
1177 }
1178 return std::nullopt;
1179 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180};
1181
1182
1183// --- InstrumentedInputReader ---
1184
1185class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001186 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187
1188public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001189 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1190 const sp<InputReaderPolicyInterface>& policy,
1191 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001192 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001194 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001196 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001197
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001198 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001199 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200 InputDeviceIdentifier identifier;
1201 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001202 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001203 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001204 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 }
1206
Prabir Pradhan28efc192019-11-05 01:10:04 +00001207 // Make the protected loopOnce method accessible to tests.
1208 using InputReader::loopOnce;
1209
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001211 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1212 const InputDeviceIdentifier& identifier)
1213 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001214 if (!mNextDevices.empty()) {
1215 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1216 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217 return device;
1218 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001219 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001220 }
1221
arthurhungdcef2dc2020-08-11 14:47:50 +08001222 // --- FakeInputReaderContext ---
1223 class FakeInputReaderContext : public ContextImpl {
1224 int32_t mGlobalMetaState;
1225 bool mUpdateGlobalMetaStateWasCalled;
1226 int32_t mGeneration;
1227
1228 public:
1229 FakeInputReaderContext(InputReader* reader)
1230 : ContextImpl(reader),
1231 mGlobalMetaState(0),
1232 mUpdateGlobalMetaStateWasCalled(false),
1233 mGeneration(1) {}
1234
1235 virtual ~FakeInputReaderContext() {}
1236
1237 void assertUpdateGlobalMetaStateWasCalled() {
1238 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1239 << "Expected updateGlobalMetaState() to have been called.";
1240 mUpdateGlobalMetaStateWasCalled = false;
1241 }
1242
1243 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1244
1245 uint32_t getGeneration() { return mGeneration; }
1246
1247 void updateGlobalMetaState() override {
1248 mUpdateGlobalMetaStateWasCalled = true;
1249 ContextImpl::updateGlobalMetaState();
1250 }
1251
1252 int32_t getGlobalMetaState() override {
1253 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1254 }
1255
1256 int32_t bumpGeneration() override {
1257 mGeneration = ContextImpl::bumpGeneration();
1258 return mGeneration;
1259 }
1260 } mFakeContext;
1261
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001263
1264public:
1265 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001266};
1267
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001268// --- InputReaderPolicyTest ---
1269class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001270protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001271 sp<FakeInputReaderPolicy> mFakePolicy;
1272
Chris Yea52ade12020-08-27 16:49:20 -07001273 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1274 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001275};
1276
1277/**
1278 * Check that empty set of viewports is an acceptable configuration.
1279 * Also try to get internal viewport two different ways - by type and by uniqueId.
1280 *
1281 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1282 * Such configuration is not currently allowed.
1283 */
1284TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001285 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001286
1287 // We didn't add any viewports yet, so there shouldn't be any.
1288 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001289 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001290 ASSERT_FALSE(internalViewport);
1291
1292 // Add an internal viewport, then clear it
1293 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001294 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001295 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001296
1297 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001298 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001299 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001300 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001301
1302 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001303 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001304 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001305 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001306
1307 mFakePolicy->clearViewports();
1308 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001309 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001310 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001311 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001312 ASSERT_FALSE(internalViewport);
1313}
1314
1315TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1316 const std::string internalUniqueId = "local:0";
1317 const std::string externalUniqueId = "local:1";
1318 const std::string virtualUniqueId1 = "virtual:2";
1319 const std::string virtualUniqueId2 = "virtual:3";
1320 constexpr int32_t virtualDisplayId1 = 2;
1321 constexpr int32_t virtualDisplayId2 = 3;
1322
1323 // Add an internal viewport
1324 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001325 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1326 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001327 // Add an external viewport
1328 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001329 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1330 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001331 // Add an virtual viewport
1332 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001333 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1334 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001335 // Add another virtual viewport
1336 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001337 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1338 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001339
1340 // Check matching by type for internal
1341 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001342 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001343 ASSERT_TRUE(internalViewport);
1344 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1345
1346 // Check matching by type for external
1347 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001348 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001349 ASSERT_TRUE(externalViewport);
1350 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1351
1352 // Check matching by uniqueId for virtual viewport #1
1353 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001354 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001356 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1358 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1359
1360 // Check matching by uniqueId for virtual viewport #2
1361 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001362 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001364 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001365 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1366 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1367}
1368
1369
1370/**
1371 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1372 * that lookup works by checking display id.
1373 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1374 */
1375TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1376 const std::string uniqueId1 = "uniqueId1";
1377 const std::string uniqueId2 = "uniqueId2";
1378 constexpr int32_t displayId1 = 2;
1379 constexpr int32_t displayId2 = 3;
1380
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001381 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1382 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001383 for (const ViewportType& type : types) {
1384 mFakePolicy->clearViewports();
1385 // Add a viewport
1386 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001387 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1388 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 // Add another viewport
1390 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001391 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1392 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393
1394 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001395 std::optional<DisplayViewport> viewport1 =
1396 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397 ASSERT_TRUE(viewport1);
1398 ASSERT_EQ(displayId1, viewport1->displayId);
1399 ASSERT_EQ(type, viewport1->type);
1400
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001401 std::optional<DisplayViewport> viewport2 =
1402 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001403 ASSERT_TRUE(viewport2);
1404 ASSERT_EQ(displayId2, viewport2->displayId);
1405 ASSERT_EQ(type, viewport2->type);
1406
1407 // When there are multiple viewports of the same kind, and uniqueId is not specified
1408 // in the call to getDisplayViewport, then that situation is not supported.
1409 // The viewports can be stored in any order, so we cannot rely on the order, since that
1410 // is just implementation detail.
1411 // However, we can check that it still returns *a* viewport, we just cannot assert
1412 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001413 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001414 ASSERT_TRUE(someViewport);
1415 }
1416}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001417
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001418/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001419 * When we have multiple internal displays make sure we always return the default display when
1420 * querying by type.
1421 */
1422TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1423 const std::string uniqueId1 = "uniqueId1";
1424 const std::string uniqueId2 = "uniqueId2";
1425 constexpr int32_t nonDefaultDisplayId = 2;
1426 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1427 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1428
1429 // Add the default display first and ensure it gets returned.
1430 mFakePolicy->clearViewports();
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 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001435 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001436 ViewportType::INTERNAL);
1437
1438 std::optional<DisplayViewport> viewport =
1439 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1440 ASSERT_TRUE(viewport);
1441 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1442 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1443
1444 // Add the default display second to make sure order doesn't matter.
1445 mFakePolicy->clearViewports();
1446 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001447 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001448 ViewportType::INTERNAL);
1449 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001450 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001451 ViewportType::INTERNAL);
1452
1453 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1454 ASSERT_TRUE(viewport);
1455 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1456 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1457}
1458
1459/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001460 * Check getDisplayViewportByPort
1461 */
1462TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001463 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001464 const std::string uniqueId1 = "uniqueId1";
1465 const std::string uniqueId2 = "uniqueId2";
1466 constexpr int32_t displayId1 = 1;
1467 constexpr int32_t displayId2 = 2;
1468 const uint8_t hdmi1 = 0;
1469 const uint8_t hdmi2 = 1;
1470 const uint8_t hdmi3 = 2;
1471
1472 mFakePolicy->clearViewports();
1473 // Add a viewport that's associated with some display port that's not of interest.
1474 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001475 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1476 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001477 // Add another viewport, connected to HDMI1 port
1478 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001479 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1480 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001481
1482 // Check that correct display viewport was returned by comparing the display ports.
1483 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1484 ASSERT_TRUE(hdmi1Viewport);
1485 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1486 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1487
1488 // Check that we can still get the same viewport using the uniqueId
1489 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1490 ASSERT_TRUE(hdmi1Viewport);
1491 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1492 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1493 ASSERT_EQ(type, hdmi1Viewport->type);
1494
1495 // Check that we cannot find a port with "HDMI2", because we never added one
1496 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1497 ASSERT_FALSE(hdmi2Viewport);
1498}
1499
Michael Wrightd02c5b62014-02-10 15:10:22 -08001500// --- InputReaderTest ---
1501
1502class InputReaderTest : public testing::Test {
1503protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001504 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001505 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001506 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001507 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001508
Chris Yea52ade12020-08-27 16:49:20 -07001509 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001510 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001511 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001512 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001513
Prabir Pradhan28efc192019-11-05 01:10:04 +00001514 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1515 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516 }
1517
Chris Yea52ade12020-08-27 16:49:20 -07001518 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519 mFakeListener.clear();
1520 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001521 }
1522
Chris Ye1b0c7342020-07-28 21:57:03 -07001523 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001524 const PropertyMap* configuration) {
1525 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001526
1527 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001528 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529 }
1530 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001531 mReader->loopOnce();
1532 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001533 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1534 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001535 }
1536
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001537 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001538 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001539 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001540 }
1541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001542 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001543 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001544 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001545 }
1546
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001547 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001548 const std::string& name,
1549 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001550 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001551 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1552 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001553 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001554 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001555 return mapper;
1556 }
1557};
1558
Chris Ye98d3f532020-10-01 21:48:59 -07001559TEST_F(InputReaderTest, PolicyGetInputDevices) {
1560 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1561 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1562 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001563
1564 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001565 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001567 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001568 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1570 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001571 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001572}
1573
Chris Yee7310032020-09-22 15:36:28 -07001574TEST_F(InputReaderTest, GetMergedInputDevices) {
1575 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1576 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1577 // Add two subdevices to device
1578 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1579 // Must add at least one mapper or the device will be ignored!
1580 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1581 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1582
1583 // Push same device instance for next device to be added, so they'll have same identifier.
1584 mReader->pushNextDevice(device);
1585 mReader->pushNextDevice(device);
1586 ASSERT_NO_FATAL_FAILURE(
1587 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1588 ASSERT_NO_FATAL_FAILURE(
1589 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1590
1591 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001592 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001593}
1594
Chris Yee14523a2020-12-19 13:46:00 -08001595TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1596 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1597 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1598 // Add two subdevices to device
1599 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1600 // Must add at least one mapper or the device will be ignored!
1601 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1602 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1603
1604 // Push same device instance for next device to be added, so they'll have same identifier.
1605 mReader->pushNextDevice(device);
1606 mReader->pushNextDevice(device);
1607 // Sensor device is initially disabled
1608 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1609 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1610 nullptr));
1611 // Device is disabled because the only sub device is a sensor device and disabled initially.
1612 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1613 ASSERT_FALSE(device->isEnabled());
1614 ASSERT_NO_FATAL_FAILURE(
1615 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1616 // The merged device is enabled if any sub device is enabled
1617 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1618 ASSERT_TRUE(device->isEnabled());
1619}
1620
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001621TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001622 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001623 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001624 constexpr int32_t eventHubId = 1;
1625 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001626 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001628 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001629 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001630
Yi Kong9b14ac62018-07-17 13:48:38 -07001631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001632
1633 NotifyDeviceResetArgs resetArgs;
1634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001635 ASSERT_EQ(deviceId, resetArgs.deviceId);
1636
1637 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001638 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001639 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001640
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001642 ASSERT_EQ(deviceId, resetArgs.deviceId);
1643 ASSERT_EQ(device->isEnabled(), false);
1644
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001645 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001646 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001649 ASSERT_EQ(device->isEnabled(), false);
1650
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001651 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001652 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001654 ASSERT_EQ(deviceId, resetArgs.deviceId);
1655 ASSERT_EQ(device->isEnabled(), true);
1656}
1657
Michael Wrightd02c5b62014-02-10 15:10:22 -08001658TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001659 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001660 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001661 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001662 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001663 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001664 AINPUT_SOURCE_KEYBOARD, nullptr);
1665 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001666
1667 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1668 AINPUT_SOURCE_ANY, AKEYCODE_A))
1669 << "Should return unknown when the device id is >= 0 but unknown.";
1670
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001671 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1672 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1673 << "Should return unknown when the device id is valid but the sources are not "
1674 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001675
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001676 ASSERT_EQ(AKEY_STATE_DOWN,
1677 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1678 AKEYCODE_A))
1679 << "Should return value provided by mapper when device id is valid and the device "
1680 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681
1682 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1683 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1684 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1685
1686 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1687 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1688 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1689}
1690
1691TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001692 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001693 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001694 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001695 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001697 AINPUT_SOURCE_KEYBOARD, nullptr);
1698 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001699
1700 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1701 AINPUT_SOURCE_ANY, KEY_A))
1702 << "Should return unknown when the device id is >= 0 but unknown.";
1703
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001704 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1705 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1706 << "Should return unknown when the device id is valid but the sources are not "
1707 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001708
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001709 ASSERT_EQ(AKEY_STATE_DOWN,
1710 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1711 KEY_A))
1712 << "Should return value provided by mapper when device id is valid and the device "
1713 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714
1715 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1716 AINPUT_SOURCE_TRACKBALL, KEY_A))
1717 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1718
1719 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1720 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1721 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1722}
1723
1724TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001725 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001726 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001727 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001728 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001729 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001730 AINPUT_SOURCE_KEYBOARD, nullptr);
1731 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001732
1733 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1734 AINPUT_SOURCE_ANY, SW_LID))
1735 << "Should return unknown when the device id is >= 0 but unknown.";
1736
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001737 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1738 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1739 << "Should return unknown when the device id is valid but the sources are not "
1740 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001741
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 ASSERT_EQ(AKEY_STATE_DOWN,
1743 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1744 SW_LID))
1745 << "Should return value provided by mapper when device id is valid and the device "
1746 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001747
1748 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1749 AINPUT_SOURCE_TRACKBALL, SW_LID))
1750 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1751
1752 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1753 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1754 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1755}
1756
1757TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001758 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001759 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001760 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001761 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001762 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001763 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001764
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001765 mapper.addSupportedKeyCode(AKEYCODE_A);
1766 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001767
1768 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1769 uint8_t flags[4] = { 0, 0, 0, 1 };
1770
1771 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1772 << "Should return false when device id is >= 0 but unknown.";
1773 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1774
1775 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001776 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1777 << "Should return false when device id is valid but the sources are not supported by "
1778 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001779 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1780
1781 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001782 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1783 keyCodes, flags))
1784 << "Should return value provided by mapper when device id is valid and the device "
1785 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001786 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1787
1788 flags[3] = 1;
1789 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1790 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1791 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1792
1793 flags[3] = 1;
1794 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1795 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1796 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1797}
1798
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001799TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001800 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001801 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001802
1803 NotifyConfigurationChangedArgs args;
1804
1805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1806 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1807}
1808
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001809TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001811 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001812 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001813 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001814 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001815 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001817 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001818
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001819 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001820 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001821 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1822
1823 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001824 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001825 ASSERT_EQ(when, event.when);
1826 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001827 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001828 ASSERT_EQ(EV_KEY, event.type);
1829 ASSERT_EQ(KEY_A, event.code);
1830 ASSERT_EQ(1, event.value);
1831}
1832
Garfield Tan1c7bc862020-01-28 13:24:04 -08001833TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001834 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001835 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001836 constexpr int32_t eventHubId = 1;
1837 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001838 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001839 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001840 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001841 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001842
1843 NotifyDeviceResetArgs resetArgs;
1844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001845 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001846
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001847 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001848 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001850 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001851 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001853 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001854 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001856 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001857 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001859 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001860 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001862 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001863 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001864}
1865
Garfield Tan1c7bc862020-01-28 13:24:04 -08001866TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1867 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001868 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001869 constexpr int32_t eventHubId = 1;
1870 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1871 // Must add at least one mapper or the device will be ignored!
1872 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001873 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001874 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1875
1876 NotifyDeviceResetArgs resetArgs;
1877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1878 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1879}
1880
Arthur Hungc23540e2018-11-29 20:42:11 +08001881TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001882 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001883 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001884 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001885 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001886 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1887 FakeInputMapper& mapper =
1888 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001889 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001890
1891 const uint8_t hdmi1 = 1;
1892
1893 // Associated touch screen with second display.
1894 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1895
1896 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001897 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001898 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001899 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001900 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001901 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001902 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001903 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001904 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001905 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001906
1907 // Add the device, and make sure all of the callbacks are triggered.
1908 // The device is added after the input port associations are processed since
1909 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001913 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001914
Arthur Hung2c9a3342019-07-23 14:18:59 +08001915 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001916 ASSERT_EQ(deviceId, device->getId());
1917 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1918 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001919
1920 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001921 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001922 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001923 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001924}
1925
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001926TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1927 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1928 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1929 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1930 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1931 // Must add at least one mapper or the device will be ignored!
1932 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1933 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1934 mReader->pushNextDevice(device);
1935 mReader->pushNextDevice(device);
1936 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1937 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1938
1939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1940
1941 NotifyDeviceResetArgs resetArgs;
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 disableDevice(deviceId);
1949 mReader->loopOnce();
1950
1951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1952 ASSERT_EQ(deviceId, resetArgs.deviceId);
1953 ASSERT_FALSE(device->isEnabled());
1954 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1955 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1956
1957 enableDevice(deviceId);
1958 mReader->loopOnce();
1959
1960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1961 ASSERT_EQ(deviceId, resetArgs.deviceId);
1962 ASSERT_TRUE(device->isEnabled());
1963 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1964 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1965}
1966
1967TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1968 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1969 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1970 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1971 // Add two subdevices to device
1972 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1973 FakeInputMapper& mapperDevice1 =
1974 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1975 FakeInputMapper& mapperDevice2 =
1976 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1977 mReader->pushNextDevice(device);
1978 mReader->pushNextDevice(device);
1979 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1980 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1981
1982 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1983 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1984
1985 ASSERT_EQ(AKEY_STATE_DOWN,
1986 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1987 ASSERT_EQ(AKEY_STATE_DOWN,
1988 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1989 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1990 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1991}
1992
Prabir Pradhan7e186182020-11-10 13:56:45 -08001993TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1994 NotifyPointerCaptureChangedArgs args;
1995
1996 mFakePolicy->setPointerCapture(true);
1997 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1998 mReader->loopOnce();
1999 mFakeListener->assertNotifyCaptureWasCalled(&args);
2000 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
2001
2002 mFakePolicy->setPointerCapture(false);
2003 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2004 mReader->loopOnce();
2005 mFakeListener->assertNotifyCaptureWasCalled(&args);
2006 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
2007
2008 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
2009 // does not change.
2010 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2011 mReader->loopOnce();
2012 mFakeListener->assertNotifyCaptureWasCalled(&args);
2013 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
2014}
2015
Chris Ye87143712020-11-10 05:05:58 +00002016class FakeVibratorInputMapper : public FakeInputMapper {
2017public:
2018 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2019 : FakeInputMapper(deviceContext, sources) {}
2020
2021 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2022};
2023
2024TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2025 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2026 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2027 constexpr int32_t eventHubId = 1;
2028 const char* DEVICE_LOCATION = "BLUETOOTH";
2029 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2030 FakeVibratorInputMapper& mapper =
2031 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2032 mReader->pushNextDevice(device);
2033
2034 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2035 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2036
2037 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2038 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2039}
2040
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002041// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002042
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002043class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002044public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002045 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002046
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002047 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002048
2049 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2050
2051 void dump(std::string& dump) override {}
2052
2053 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2054 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002055 }
2056
Chris Yee2b1e5c2021-03-10 22:45:12 -08002057 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2058 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002059 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002060
2061 bool setLightColor(int32_t lightId, int32_t color) override {
2062 getDeviceContext().setLightBrightness(lightId, color >> 24);
2063 return true;
2064 }
2065
2066 std::optional<int32_t> getLightColor(int32_t lightId) override {
2067 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2068 if (!result.has_value()) {
2069 return std::nullopt;
2070 }
2071 return result.value() << 24;
2072 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002073
2074 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2075
2076 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2077
2078private:
2079 InputDeviceContext& mDeviceContext;
2080 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2081 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002082};
2083
Chris Yee2b1e5c2021-03-10 22:45:12 -08002084TEST_F(InputReaderTest, BatteryGetCapacity) {
2085 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2086 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2087 constexpr int32_t eventHubId = 1;
2088 const char* DEVICE_LOCATION = "BLUETOOTH";
2089 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002090 FakePeripheralController& controller =
2091 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002092 mReader->pushNextDevice(device);
2093
2094 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2095
2096 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2097 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2098}
2099
2100TEST_F(InputReaderTest, BatteryGetStatus) {
2101 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2102 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2103 constexpr int32_t eventHubId = 1;
2104 const char* DEVICE_LOCATION = "BLUETOOTH";
2105 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002106 FakePeripheralController& controller =
2107 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002108 mReader->pushNextDevice(device);
2109
2110 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2111
2112 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2113 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2114}
2115
Chris Ye3fdbfef2021-01-06 18:45:18 -08002116TEST_F(InputReaderTest, LightGetColor) {
2117 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2118 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2119 constexpr int32_t eventHubId = 1;
2120 const char* DEVICE_LOCATION = "BLUETOOTH";
2121 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002122 FakePeripheralController& controller =
2123 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002124 mReader->pushNextDevice(device);
2125 RawLightInfo info = {.id = 1,
2126 .name = "Mono",
2127 .maxBrightness = 255,
2128 .flags = InputLightClass::BRIGHTNESS,
2129 .path = ""};
2130 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2131 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2132
2133 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002134
Chris Yee2b1e5c2021-03-10 22:45:12 -08002135 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2136 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002137 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2138 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2139}
2140
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002141// --- InputReaderIntegrationTest ---
2142
2143// These tests create and interact with the InputReader only through its interface.
2144// The InputReader is started during SetUp(), which starts its processing in its own
2145// thread. The tests use linux uinput to emulate input devices.
2146// NOTE: Interacting with the physical device while these tests are running may cause
2147// the tests to fail.
2148class InputReaderIntegrationTest : public testing::Test {
2149protected:
2150 sp<TestInputListener> mTestListener;
2151 sp<FakeInputReaderPolicy> mFakePolicy;
2152 sp<InputReaderInterface> mReader;
2153
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002154 std::shared_ptr<FakePointerController> mFakePointerController;
2155
Chris Yea52ade12020-08-27 16:49:20 -07002156 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002157 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002158 mFakePointerController = std::make_shared<FakePointerController>();
2159 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002160 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2161 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002162
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002163 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002164 ASSERT_EQ(mReader->start(), OK);
2165
2166 // Since this test is run on a real device, all the input devices connected
2167 // to the test device will show up in mReader. We wait for those input devices to
2168 // show up before beginning the tests.
2169 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2170 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2171 }
2172
Chris Yea52ade12020-08-27 16:49:20 -07002173 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002174 ASSERT_EQ(mReader->stop(), OK);
2175 mTestListener.clear();
2176 mFakePolicy.clear();
2177 }
2178};
2179
2180TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2181 // An invalid input device that is only used for this test.
2182 class InvalidUinputDevice : public UinputDevice {
2183 public:
2184 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2185
2186 private:
2187 void configureDevice(int fd, uinput_user_dev* device) override {}
2188 };
2189
2190 const size_t numDevices = mFakePolicy->getInputDevices().size();
2191
2192 // UinputDevice does not set any event or key bits, so InputReader should not
2193 // consider it as a valid device.
2194 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2195 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2196 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2197 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2198
2199 invalidDevice.reset();
2200 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2201 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2202 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2203}
2204
2205TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2206 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2207
2208 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2209 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2210 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2211 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2212
2213 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002214 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002215 const auto& it =
2216 std::find_if(inputDevices.begin(), inputDevices.end(),
2217 [&keyboard](const InputDeviceInfo& info) {
2218 return info.getIdentifier().name == keyboard->getName();
2219 });
2220
2221 ASSERT_NE(it, inputDevices.end());
2222 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2223 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2224 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002225
2226 keyboard.reset();
2227 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2228 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2229 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2230}
2231
2232TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2233 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2234 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2235
2236 NotifyConfigurationChangedArgs configChangedArgs;
2237 ASSERT_NO_FATAL_FAILURE(
2238 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002239 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002240 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2241
2242 NotifyKeyArgs keyArgs;
2243 keyboard->pressAndReleaseHomeKey();
2244 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2245 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002246 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002247 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002248 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002249 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002250 prevTimestamp = keyArgs.eventTime;
2251
2252 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2253 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002254 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002255 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002256 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002257}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002258
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002259/**
2260 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2261 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2262 * are passed to the listener.
2263 */
2264static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2265TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2266 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2267 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2268 NotifyKeyArgs keyArgs;
2269
2270 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2271 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2272 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2273 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2274
2275 controller->pressAndReleaseKey(BTN_GEAR_UP);
2276 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2277 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2278 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2279}
2280
Arthur Hungaab25622020-01-16 11:22:11 +08002281// --- TouchProcessTest ---
2282class TouchIntegrationTest : public InputReaderIntegrationTest {
2283protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002284 const std::string UNIQUE_ID = "local:0";
2285
Chris Yea52ade12020-08-27 16:49:20 -07002286 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002287 InputReaderIntegrationTest::SetUp();
2288 // At least add an internal display.
2289 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2290 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002291 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002292
2293 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2294 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2295 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2296 }
2297
2298 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2299 int32_t orientation, const std::string& uniqueId,
2300 std::optional<uint8_t> physicalPort,
2301 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002302 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2303 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002304 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2305 }
2306
2307 std::unique_ptr<UinputTouchScreen> mDevice;
2308};
2309
2310TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2311 NotifyMotionArgs args;
2312 const Point centerPoint = mDevice->getCenterPoint();
2313
2314 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002315 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002316 mDevice->sendDown(centerPoint);
2317 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2318 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2319
2320 // ACTION_MOVE
2321 mDevice->sendMove(centerPoint + Point(1, 1));
2322 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2324
2325 // ACTION_UP
2326 mDevice->sendUp();
2327 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2328 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2329}
2330
2331TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2332 NotifyMotionArgs args;
2333 const Point centerPoint = mDevice->getCenterPoint();
2334
2335 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002336 mDevice->sendSlot(FIRST_SLOT);
2337 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002338 mDevice->sendDown(centerPoint);
2339 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2340 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2341
2342 // ACTION_POINTER_DOWN (Second slot)
2343 const Point secondPoint = centerPoint + Point(100, 100);
2344 mDevice->sendSlot(SECOND_SLOT);
2345 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2346 mDevice->sendDown(secondPoint + Point(1, 1));
2347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2348 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2349 args.action);
2350
2351 // ACTION_MOVE (Second slot)
2352 mDevice->sendMove(secondPoint);
2353 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2355
2356 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002357 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002358 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002359 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002360 args.action);
2361
2362 // ACTION_UP
2363 mDevice->sendSlot(FIRST_SLOT);
2364 mDevice->sendUp();
2365 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2366 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2367}
2368
2369TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2370 NotifyMotionArgs args;
2371 const Point centerPoint = mDevice->getCenterPoint();
2372
2373 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002374 mDevice->sendSlot(FIRST_SLOT);
2375 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002376 mDevice->sendDown(centerPoint);
2377 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2378 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2379
arthurhungcc7f9802020-04-30 17:55:40 +08002380 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002381 const Point secondPoint = centerPoint + Point(100, 100);
2382 mDevice->sendSlot(SECOND_SLOT);
2383 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2384 mDevice->sendDown(secondPoint);
2385 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2386 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2387 args.action);
2388
arthurhungcc7f9802020-04-30 17:55:40 +08002389 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002390 mDevice->sendMove(secondPoint + Point(1, 1));
2391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2393
arthurhungcc7f9802020-04-30 17:55:40 +08002394 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2395 // a palm event.
2396 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002397 mDevice->sendToolType(MT_TOOL_PALM);
2398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002399 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2400 args.action);
2401 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002402
arthurhungcc7f9802020-04-30 17:55:40 +08002403 // Send up to second slot, expect first slot send moving.
2404 mDevice->sendPointerUp();
2405 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002407
arthurhungcc7f9802020-04-30 17:55:40 +08002408 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002409 mDevice->sendSlot(FIRST_SLOT);
2410 mDevice->sendUp();
2411
arthurhungcc7f9802020-04-30 17:55:40 +08002412 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2413 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002414}
2415
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417class InputDeviceTest : public testing::Test {
2418protected:
2419 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002420 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002421 static const int32_t DEVICE_ID;
2422 static const int32_t DEVICE_GENERATION;
2423 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002424 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002425 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002426
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002427 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002429 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002430 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002431 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432
Chris Yea52ade12020-08-27 16:49:20 -07002433 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002434 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002436 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002437 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2438 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439 InputDeviceIdentifier identifier;
2440 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002441 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002442 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002443 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002444 mReader->pushNextDevice(mDevice);
2445 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2446 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002447 }
2448
Chris Yea52ade12020-08-27 16:49:20 -07002449 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450 mFakeListener.clear();
2451 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452 }
2453};
2454
2455const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002456const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002457const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2459const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002460const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2461 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002462const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463
2464TEST_F(InputDeviceTest, ImmutableProperties) {
2465 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002466 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002467 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468}
2469
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002470TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2471 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002472}
2473
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2475 // Configuration.
2476 InputReaderConfiguration config;
2477 mDevice->configure(ARBITRARY_TIME, &config, 0);
2478
2479 // Reset.
2480 mDevice->reset(ARBITRARY_TIME);
2481
2482 NotifyDeviceResetArgs resetArgs;
2483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2484 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2485 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2486
2487 // Metadata.
2488 ASSERT_TRUE(mDevice->isIgnored());
2489 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2490
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002491 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002492 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002493 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2495 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2496
2497 // State queries.
2498 ASSERT_EQ(0, mDevice->getMetaState());
2499
2500 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2501 << "Ignored device should return unknown key code state.";
2502 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2503 << "Ignored device should return unknown scan code state.";
2504 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2505 << "Ignored device should return unknown switch state.";
2506
2507 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2508 uint8_t flags[2] = { 0, 1 };
2509 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2510 << "Ignored device should never mark any key codes.";
2511 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2512 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2513}
2514
2515TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2516 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002517 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002518
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002519 FakeInputMapper& mapper1 =
2520 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002521 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2522 mapper1.setMetaState(AMETA_ALT_ON);
2523 mapper1.addSupportedKeyCode(AKEYCODE_A);
2524 mapper1.addSupportedKeyCode(AKEYCODE_B);
2525 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2526 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2527 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2528 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2529 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002531 FakeInputMapper& mapper2 =
2532 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002533 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002534
2535 InputReaderConfiguration config;
2536 mDevice->configure(ARBITRARY_TIME, &config, 0);
2537
2538 String8 propertyValue;
2539 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2540 << "Device should have read configuration during configuration phase.";
2541 ASSERT_STREQ("value", propertyValue.string());
2542
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002543 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2544 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545
2546 // Reset
2547 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002548 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2549 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002550
2551 NotifyDeviceResetArgs resetArgs;
2552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2553 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2554 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2555
2556 // Metadata.
2557 ASSERT_FALSE(mDevice->isIgnored());
2558 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2559
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002560 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002562 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2564 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2565
2566 // State queries.
2567 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2568 << "Should query mappers and combine meta states.";
2569
2570 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2571 << "Should return unknown key code state when source not supported.";
2572 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2573 << "Should return unknown scan code state when source not supported.";
2574 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2575 << "Should return unknown switch state when source not supported.";
2576
2577 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2578 << "Should query mapper when source is supported.";
2579 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2580 << "Should query mapper when source is supported.";
2581 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2582 << "Should query mapper when source is supported.";
2583
2584 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2585 uint8_t flags[4] = { 0, 0, 0, 1 };
2586 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2587 << "Should do nothing when source is unsupported.";
2588 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2589 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2590 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2591 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2592
2593 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2594 << "Should query mapper when source is supported.";
2595 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2596 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2597 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2598 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2599
2600 // Event handling.
2601 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002602 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603 mDevice->process(&event, 1);
2604
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002605 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2606 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607}
2608
Arthur Hung2c9a3342019-07-23 14:18:59 +08002609// A single input device is associated with a specific display. Check that:
2610// 1. Device is disabled if the viewport corresponding to the associated display is not found
2611// 2. Device is disabled when setEnabled API is called
2612TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002613 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002614
2615 // First Configuration.
2616 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2617
2618 // Device should be enabled by default.
2619 ASSERT_TRUE(mDevice->isEnabled());
2620
2621 // Prepare associated info.
2622 constexpr uint8_t hdmi = 1;
2623 const std::string UNIQUE_ID = "local:1";
2624
2625 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2626 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2627 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2628 // Device should be disabled because it is associated with a specific display via
2629 // input port <-> display port association, but the corresponding display is not found
2630 ASSERT_FALSE(mDevice->isEnabled());
2631
2632 // Prepare displays.
2633 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002634 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2635 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002636 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2637 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2638 ASSERT_TRUE(mDevice->isEnabled());
2639
2640 // Device should be disabled after set disable.
2641 mFakePolicy->addDisabledDevice(mDevice->getId());
2642 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2643 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2644 ASSERT_FALSE(mDevice->isEnabled());
2645
2646 // Device should still be disabled even found the associated display.
2647 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2648 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2649 ASSERT_FALSE(mDevice->isEnabled());
2650}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651
Christine Franks1ba71cc2021-04-07 14:37:42 -07002652TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2653 // Device should be enabled by default.
2654 mFakePolicy->clearViewports();
2655 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2656 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2657 ASSERT_TRUE(mDevice->isEnabled());
2658
2659 // Device should be disabled because it is associated with a specific display, but the
2660 // corresponding display is not found.
2661 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2662 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2663 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2664 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2665 ASSERT_FALSE(mDevice->isEnabled());
2666
2667 // Device should be enabled when a display is found.
2668 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2669 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2670 NO_PORT, ViewportType::INTERNAL);
2671 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2672 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2673 ASSERT_TRUE(mDevice->isEnabled());
2674
2675 // Device should be disabled after set disable.
2676 mFakePolicy->addDisabledDevice(mDevice->getId());
2677 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2678 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2679 ASSERT_FALSE(mDevice->isEnabled());
2680
2681 // Device should still be disabled even found the associated display.
2682 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2683 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2684 ASSERT_FALSE(mDevice->isEnabled());
2685}
2686
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687// --- InputMapperTest ---
2688
2689class InputMapperTest : public testing::Test {
2690protected:
2691 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002692 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693 static const int32_t DEVICE_ID;
2694 static const int32_t DEVICE_GENERATION;
2695 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002696 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002697 static const int32_t EVENTHUB_ID;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002698 static const std::optional<bool> INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002699
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002700 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002701 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002702 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002703 std::unique_ptr<InstrumentedInputReader> mReader;
2704 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705
Chris Ye1b0c7342020-07-28 21:57:03 -07002706 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002707 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002709 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002710 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2711 mFakeListener);
2712 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 }
2714
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002715 void SetUp() override {
2716 // Ensure per_window_input_rotation is enabled.
2717 sysprop::InputFlingerProperties::per_window_input_rotation(true);
2718
2719 SetUp(DEVICE_CLASSES);
2720 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002721
Chris Yea52ade12020-08-27 16:49:20 -07002722 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002723 mFakeListener.clear();
2724 mFakePolicy.clear();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002725
2726 sysprop::InputFlingerProperties::per_window_input_rotation(
2727 INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728 }
2729
2730 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002731 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732 }
2733
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002734 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002735 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002736 mReader->requestRefreshConfiguration(changes);
2737 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002738 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002739 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2740 }
2741
arthurhungdcef2dc2020-08-11 14:47:50 +08002742 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2743 const std::string& location, int32_t eventHubId,
2744 Flags<InputDeviceClass> classes) {
2745 InputDeviceIdentifier identifier;
2746 identifier.name = name;
2747 identifier.location = location;
2748 std::shared_ptr<InputDevice> device =
2749 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2750 identifier);
2751 mReader->pushNextDevice(device);
2752 mFakeEventHub->addDevice(eventHubId, name, classes);
2753 mReader->loopOnce();
2754 return device;
2755 }
2756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 template <class T, typename... Args>
2758 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002759 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002760 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002762 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002763 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764 }
2765
2766 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002767 int32_t orientation, const std::string& uniqueId,
2768 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002769 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2770 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002771 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2772 }
2773
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002774 void clearViewports() {
2775 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776 }
2777
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002778 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2779 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780 RawEvent event;
2781 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002782 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002783 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 event.type = type;
2785 event.code = code;
2786 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002788 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002789 }
2790
2791 static void assertMotionRange(const InputDeviceInfo& info,
2792 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2793 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002794 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2796 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2797 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2798 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2799 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2800 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2801 }
2802
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002803 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2804 float size, float touchMajor, float touchMinor, float toolMajor,
2805 float toolMinor, float orientation, float distance,
2806 float scaledAxisEpsilon = 1.f) {
2807 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2808 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2810 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002811 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2812 scaledAxisEpsilon);
2813 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2814 scaledAxisEpsilon);
2815 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2816 scaledAxisEpsilon);
2817 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2818 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2820 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2821 }
2822
Michael Wright17db18e2020-06-26 20:51:44 +01002823 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002825 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826 ASSERT_NEAR(x, actualX, 1);
2827 ASSERT_NEAR(y, actualY, 1);
2828 }
2829};
2830
2831const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002832const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002833const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2835const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002836const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2837 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002838const int32_t InputMapperTest::EVENTHUB_ID = 1;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002839const std::optional<bool> InputMapperTest::INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE =
2840 sysprop::InputFlingerProperties::per_window_input_rotation();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841
2842// --- SwitchInputMapperTest ---
2843
2844class SwitchInputMapperTest : public InputMapperTest {
2845protected:
2846};
2847
2848TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002849 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002851 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852}
2853
2854TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002855 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002857 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002858 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002860 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002861 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862}
2863
2864TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002865 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002871
2872 NotifySwitchArgs args;
2873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2874 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002875 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2876 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877 args.switchMask);
2878 ASSERT_EQ(uint32_t(0), args.policyFlags);
2879}
2880
Chris Ye87143712020-11-10 05:05:58 +00002881// --- VibratorInputMapperTest ---
2882class VibratorInputMapperTest : public InputMapperTest {
2883protected:
2884 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2885};
2886
2887TEST_F(VibratorInputMapperTest, GetSources) {
2888 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2889
2890 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2891}
2892
2893TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2894 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2895
2896 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2897}
2898
2899TEST_F(VibratorInputMapperTest, Vibrate) {
2900 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002901 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002902 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2903
2904 VibrationElement pattern(2);
2905 VibrationSequence sequence(2);
2906 pattern.duration = std::chrono::milliseconds(200);
2907 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2908 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2909 sequence.addElement(pattern);
2910 pattern.duration = std::chrono::milliseconds(500);
2911 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2912 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2913 sequence.addElement(pattern);
2914
2915 std::vector<int64_t> timings = {0, 1};
2916 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2917
2918 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002919 // Start vibrating
2920 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002921 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002922 // Verify vibrator state listener was notified.
2923 mReader->loopOnce();
2924 NotifyVibratorStateArgs args;
2925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2926 ASSERT_EQ(DEVICE_ID, args.deviceId);
2927 ASSERT_TRUE(args.isOn);
2928 // Stop vibrating
2929 mapper.cancelVibrate(VIBRATION_TOKEN);
2930 ASSERT_FALSE(mapper.isVibrating());
2931 // Verify vibrator state listener was notified.
2932 mReader->loopOnce();
2933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2934 ASSERT_EQ(DEVICE_ID, args.deviceId);
2935 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002936}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937
Chris Yef59a2f42020-10-16 12:55:26 -07002938// --- SensorInputMapperTest ---
2939
2940class SensorInputMapperTest : public InputMapperTest {
2941protected:
2942 static const int32_t ACCEL_RAW_MIN;
2943 static const int32_t ACCEL_RAW_MAX;
2944 static const int32_t ACCEL_RAW_FUZZ;
2945 static const int32_t ACCEL_RAW_FLAT;
2946 static const int32_t ACCEL_RAW_RESOLUTION;
2947
2948 static const int32_t GYRO_RAW_MIN;
2949 static const int32_t GYRO_RAW_MAX;
2950 static const int32_t GYRO_RAW_FUZZ;
2951 static const int32_t GYRO_RAW_FLAT;
2952 static const int32_t GYRO_RAW_RESOLUTION;
2953
2954 static const float GRAVITY_MS2_UNIT;
2955 static const float DEGREE_RADIAN_UNIT;
2956
2957 void prepareAccelAxes();
2958 void prepareGyroAxes();
2959 void setAccelProperties();
2960 void setGyroProperties();
2961 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2962};
2963
2964const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2965const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2966const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2967const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2968const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2969
2970const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2971const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2972const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2973const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2974const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2975
2976const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2977const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2978
2979void SensorInputMapperTest::prepareAccelAxes() {
2980 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2981 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2982 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2983 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2984 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2985 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2986}
2987
2988void SensorInputMapperTest::prepareGyroAxes() {
2989 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2990 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2991 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2992 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2993 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2994 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2995}
2996
2997void SensorInputMapperTest::setAccelProperties() {
2998 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2999 /* sensorDataIndex */ 0);
3000 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3001 /* sensorDataIndex */ 1);
3002 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3003 /* sensorDataIndex */ 2);
3004 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3005 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3006 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3007 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3008 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3009}
3010
3011void SensorInputMapperTest::setGyroProperties() {
3012 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3013 /* sensorDataIndex */ 0);
3014 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3015 /* sensorDataIndex */ 1);
3016 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3017 /* sensorDataIndex */ 2);
3018 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3019 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3020 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3021 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3022 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3023}
3024
3025TEST_F(SensorInputMapperTest, GetSources) {
3026 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3027
3028 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3029}
3030
3031TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3032 setAccelProperties();
3033 prepareAccelAxes();
3034 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3035
3036 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3037 std::chrono::microseconds(10000),
3038 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003039 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003045
3046 NotifySensorArgs args;
3047 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3048 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3049 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3050
3051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3052 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3053 ASSERT_EQ(args.deviceId, DEVICE_ID);
3054 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3055 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3056 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3057 ASSERT_EQ(args.values, values);
3058 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3059}
3060
3061TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3062 setGyroProperties();
3063 prepareGyroAxes();
3064 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3065
3066 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3067 std::chrono::microseconds(10000),
3068 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003069 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003075
3076 NotifySensorArgs args;
3077 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3078 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3079 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3080
3081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3082 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3083 ASSERT_EQ(args.deviceId, DEVICE_ID);
3084 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3085 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3086 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3087 ASSERT_EQ(args.values, values);
3088 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3089}
3090
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091// --- KeyboardInputMapperTest ---
3092
3093class KeyboardInputMapperTest : public InputMapperTest {
3094protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003095 const std::string UNIQUE_ID = "local:0";
3096
3097 void prepareDisplay(int32_t orientation);
3098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003100 int32_t originalKeyCode, int32_t rotatedKeyCode,
3101 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102};
3103
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003104/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3105 * orientation.
3106 */
3107void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003108 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3109 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003110}
3111
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003112void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003113 int32_t originalScanCode, int32_t originalKeyCode,
3114 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 NotifyKeyArgs args;
3116
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3119 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3120 ASSERT_EQ(originalScanCode, args.scanCode);
3121 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003122 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3126 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3127 ASSERT_EQ(originalScanCode, args.scanCode);
3128 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003129 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130}
3131
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003133 KeyboardInputMapper& mapper =
3134 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3135 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003137 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138}
3139
3140TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3141 const int32_t USAGE_A = 0x070004;
3142 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003143 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3144 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003145 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3146 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3147 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003149 KeyboardInputMapper& mapper =
3150 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3151 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003152 // Initial metastate to AMETA_NONE.
3153 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3154 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155
3156 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158 NotifyKeyArgs args;
3159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3160 ASSERT_EQ(DEVICE_ID, args.deviceId);
3161 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3162 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3163 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3164 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3165 ASSERT_EQ(KEY_HOME, args.scanCode);
3166 ASSERT_EQ(AMETA_NONE, args.metaState);
3167 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3168 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3169 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3170
3171 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003172 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3174 ASSERT_EQ(DEVICE_ID, args.deviceId);
3175 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3176 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3177 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3178 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3179 ASSERT_EQ(KEY_HOME, args.scanCode);
3180 ASSERT_EQ(AMETA_NONE, args.metaState);
3181 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3182 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3183 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3184
3185 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3189 ASSERT_EQ(DEVICE_ID, args.deviceId);
3190 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3191 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3192 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3193 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3194 ASSERT_EQ(0, args.scanCode);
3195 ASSERT_EQ(AMETA_NONE, args.metaState);
3196 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3197 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3198 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3199
3200 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3202 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3204 ASSERT_EQ(DEVICE_ID, args.deviceId);
3205 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3206 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3207 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3208 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3209 ASSERT_EQ(0, args.scanCode);
3210 ASSERT_EQ(AMETA_NONE, args.metaState);
3211 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3212 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3213 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3214
3215 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3219 ASSERT_EQ(DEVICE_ID, args.deviceId);
3220 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3221 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3222 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3223 ASSERT_EQ(0, args.keyCode);
3224 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3225 ASSERT_EQ(AMETA_NONE, args.metaState);
3226 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3227 ASSERT_EQ(0U, args.policyFlags);
3228 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3229
3230 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3232 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3234 ASSERT_EQ(DEVICE_ID, args.deviceId);
3235 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3236 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3237 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3238 ASSERT_EQ(0, args.keyCode);
3239 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3240 ASSERT_EQ(AMETA_NONE, args.metaState);
3241 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3242 ASSERT_EQ(0U, args.policyFlags);
3243 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3244}
3245
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003246/**
3247 * Ensure that the readTime is set to the time when the EV_KEY is received.
3248 */
3249TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3250 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3251
3252 KeyboardInputMapper& mapper =
3253 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3254 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3255 NotifyKeyArgs args;
3256
3257 // Key down
3258 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3260 ASSERT_EQ(12, args.readTime);
3261
3262 // Key up
3263 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3265 ASSERT_EQ(15, args.readTime);
3266}
3267
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003269 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3270 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003271 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3272 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3273 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003275 KeyboardInputMapper& mapper =
3276 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3277 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278
arthurhungc903df12020-08-11 15:08:42 +08003279 // Initial metastate to AMETA_NONE.
3280 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3281 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
3283 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 NotifyKeyArgs args;
3286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3287 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003289 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290
3291 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003292 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3294 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296
3297 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003298 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3300 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003301 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302
3303 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003304 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3306 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003307 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003308 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309}
3310
3311TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003312 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3313 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3314 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3315 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003317 KeyboardInputMapper& mapper =
3318 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3319 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003321 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3323 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3324 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3325 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3326 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3327 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3328 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3329 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3330}
3331
3332TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003333 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3334 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3335 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3336 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003339 KeyboardInputMapper& mapper =
3340 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3341 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003343 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003344 ASSERT_NO_FATAL_FAILURE(
3345 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3346 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3347 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3348 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3349 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3350 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3351 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003353 clearViewports();
3354 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003355 ASSERT_NO_FATAL_FAILURE(
3356 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3357 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3358 AKEYCODE_DPAD_UP, DISPLAY_ID));
3359 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3360 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3361 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3362 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003364 clearViewports();
3365 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003366 ASSERT_NO_FATAL_FAILURE(
3367 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3368 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3369 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3370 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3371 AKEYCODE_DPAD_UP, DISPLAY_ID));
3372 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3373 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003375 clearViewports();
3376 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003377 ASSERT_NO_FATAL_FAILURE(
3378 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3379 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3380 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3381 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3382 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3383 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3384 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385
3386 // Special case: if orientation changes while key is down, we still emit the same keycode
3387 // in the key up as we did in the key down.
3388 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003389 clearViewports();
3390 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3393 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3394 ASSERT_EQ(KEY_UP, args.scanCode);
3395 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3396
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003397 clearViewports();
3398 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3402 ASSERT_EQ(KEY_UP, args.scanCode);
3403 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3404}
3405
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003406TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3407 // If the keyboard is not orientation aware,
3408 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003409 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003410
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003411 KeyboardInputMapper& mapper =
3412 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3413 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003414 NotifyKeyArgs args;
3415
3416 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3421 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3422
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003423 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3428 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3429}
3430
3431TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3432 // If the keyboard is orientation aware,
3433 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003434 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003435
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003436 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003437 KeyboardInputMapper& mapper =
3438 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3439 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003440 NotifyKeyArgs args;
3441
3442 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3443 // ^--- already checked by the previous test
3444
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003445 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003446 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3451 ASSERT_EQ(DISPLAY_ID, args.displayId);
3452
3453 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003454 clearViewports();
3455 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003456 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3461 ASSERT_EQ(newDisplayId, args.displayId);
3462}
3463
Michael Wrightd02c5b62014-02-10 15:10:22 -08003464TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003465 KeyboardInputMapper& mapper =
3466 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3467 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003469 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003470 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003472 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003473 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474}
3475
3476TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003477 KeyboardInputMapper& mapper =
3478 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3479 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003481 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003482 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003485 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486}
3487
3488TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003489 KeyboardInputMapper& mapper =
3490 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3491 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003493 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494
3495 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3496 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003497 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498 ASSERT_TRUE(flags[0]);
3499 ASSERT_FALSE(flags[1]);
3500}
3501
3502TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003503 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3504 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3505 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3506 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3507 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3508 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003510 KeyboardInputMapper& mapper =
3511 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3512 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003513 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003514 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3515 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516
3517 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003518 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3519 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3520 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521
3522 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003525 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3526 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3527 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003528 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529
3530 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003533 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3534 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3535 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003536 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537
3538 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003541 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3542 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3543 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003544 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545
3546 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003549 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3550 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3551 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003552 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553
3554 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003557 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3558 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3559 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003560 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561
3562 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003565 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3566 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3567 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003568 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569}
3570
Chris Yea52ade12020-08-27 16:49:20 -07003571TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3572 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3573 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3574 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3575 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3576
3577 KeyboardInputMapper& mapper =
3578 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3579 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3580
3581 // Initial metastate should be AMETA_NONE as no meta keys added.
3582 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3583 // Meta state should be AMETA_NONE after reset
3584 mapper.reset(ARBITRARY_TIME);
3585 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3586 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3587 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3588 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3589
3590 NotifyKeyArgs args;
3591 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3594 ASSERT_EQ(AMETA_NONE, args.metaState);
3595 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3596 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3597 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3598
3599 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003600 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3602 ASSERT_EQ(AMETA_NONE, args.metaState);
3603 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3604 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3605 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3606}
3607
Arthur Hung2c9a3342019-07-23 14:18:59 +08003608TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3609 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003610 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3611 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3612 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3613 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003614
3615 // keyboard 2.
3616 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003617 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003618 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003619 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003620 std::shared_ptr<InputDevice> device2 =
3621 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3622 Flags<InputDeviceClass>(0));
3623
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003624 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3625 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3626 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3627 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003629 KeyboardInputMapper& mapper =
3630 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3631 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003632
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003633 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003634 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003635 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003636 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3637 device2->reset(ARBITRARY_TIME);
3638
3639 // Prepared displays and associated info.
3640 constexpr uint8_t hdmi1 = 0;
3641 constexpr uint8_t hdmi2 = 1;
3642 const std::string SECONDARY_UNIQUE_ID = "local:1";
3643
3644 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3645 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3646
3647 // No associated display viewport found, should disable the device.
3648 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3649 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3650 ASSERT_FALSE(device2->isEnabled());
3651
3652 // Prepare second display.
3653 constexpr int32_t newDisplayId = 2;
3654 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003655 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003656 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003657 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003658 // Default device will reconfigure above, need additional reconfiguration for another device.
3659 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3660 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3661
3662 // Device should be enabled after the associated display is found.
3663 ASSERT_TRUE(mDevice->isEnabled());
3664 ASSERT_TRUE(device2->isEnabled());
3665
3666 // Test pad key events
3667 ASSERT_NO_FATAL_FAILURE(
3668 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3669 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3670 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3671 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3672 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3673 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3674 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3675
3676 ASSERT_NO_FATAL_FAILURE(
3677 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3678 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3679 AKEYCODE_DPAD_RIGHT, newDisplayId));
3680 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3681 AKEYCODE_DPAD_DOWN, newDisplayId));
3682 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3683 AKEYCODE_DPAD_LEFT, newDisplayId));
3684}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003685
arthurhungc903df12020-08-11 15:08:42 +08003686TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3687 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3688 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3689 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3690 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3691 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3692 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3693
3694 KeyboardInputMapper& mapper =
3695 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3696 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3697 // Initial metastate to AMETA_NONE.
3698 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3699 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3700
3701 // Initialization should have turned all of the lights off.
3702 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3703 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3704 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3705
3706 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003709 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3710 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3711
3712 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003715 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3716 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3717
3718 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003721 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3722 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3723
3724 mFakeEventHub->removeDevice(EVENTHUB_ID);
3725 mReader->loopOnce();
3726
3727 // keyboard 2 should default toggle keys.
3728 const std::string USB2 = "USB2";
3729 const std::string DEVICE_NAME2 = "KEYBOARD2";
3730 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3731 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3732 std::shared_ptr<InputDevice> device2 =
3733 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3734 Flags<InputDeviceClass>(0));
3735 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3736 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3737 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3738 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3739 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3740 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3741
arthurhung6fe95782020-10-05 22:41:16 +08003742 KeyboardInputMapper& mapper2 =
3743 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3744 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003745 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3746 device2->reset(ARBITRARY_TIME);
3747
3748 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3749 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3750 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003751 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3752 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003753}
3754
Arthur Hungcb40a002021-08-03 14:31:01 +00003755TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3756 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3757 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3758 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3759
3760 // Suppose we have two mappers. (DPAD + KEYBOARD)
3761 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3762 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3763 KeyboardInputMapper& mapper =
3764 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3765 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3766 // Initialize metastate to AMETA_NUM_LOCK_ON.
3767 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3768 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3769
3770 mReader->toggleCapsLockState(DEVICE_ID);
3771 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3772}
3773
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003774// --- KeyboardInputMapperTest_ExternalDevice ---
3775
3776class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3777protected:
Chris Yea52ade12020-08-27 16:49:20 -07003778 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003779};
3780
3781TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003782 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3783 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003784
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3786 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3787 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3788 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003789
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003790 KeyboardInputMapper& mapper =
3791 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3792 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003795 NotifyKeyArgs args;
3796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3797 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3798
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3801 ASSERT_EQ(uint32_t(0), args.policyFlags);
3802
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3805 ASSERT_EQ(uint32_t(0), args.policyFlags);
3806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3809 ASSERT_EQ(uint32_t(0), args.policyFlags);
3810
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3813 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3814
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003815 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3817 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3818}
3819
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003820TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003821 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003822
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003823 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3824 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3825 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003826
Powei Fengd041c5d2019-05-03 17:11:33 -07003827 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003828 KeyboardInputMapper& mapper =
3829 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3830 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003831
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003833 NotifyKeyArgs args;
3834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3835 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3836
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003837 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3839 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3840
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3843 ASSERT_EQ(uint32_t(0), args.policyFlags);
3844
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003845 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3847 ASSERT_EQ(uint32_t(0), args.policyFlags);
3848
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3851 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3852
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003853 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3855 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3856}
3857
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858// --- CursorInputMapperTest ---
3859
3860class CursorInputMapperTest : public InputMapperTest {
3861protected:
3862 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3863
Michael Wright17db18e2020-06-26 20:51:44 +01003864 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865
Chris Yea52ade12020-08-27 16:49:20 -07003866 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867 InputMapperTest::SetUp();
3868
Michael Wright17db18e2020-06-26 20:51:44 +01003869 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003870 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871 }
3872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3874 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003875
3876 void prepareDisplay(int32_t orientation) {
3877 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003878 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003879 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3880 orientation, uniqueId, NO_PORT, viewportType);
3881 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003882
3883 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3884 float pressure) {
3885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3886 0.0f, 0.0f, 0.0f, EPSILON));
3887 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888};
3889
3890const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003892void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3893 int32_t originalY, int32_t rotatedX,
3894 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895 NotifyMotionArgs args;
3896
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003902 ASSERT_NO_FATAL_FAILURE(
3903 assertCursorPointerCoords(args.pointerCoords[0],
3904 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3905 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906}
3907
3908TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003910 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003912 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913}
3914
3915TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
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
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003919 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920}
3921
3922TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003924 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003925
3926 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003927 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928
3929 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003930 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3931 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003932 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3933 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3934
3935 // When the bounds are set, then there should be a valid motion range.
3936 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3937
3938 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003939 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940
3941 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3942 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3943 1, 800 - 1, 0.0f, 0.0f));
3944 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3945 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3946 2, 480 - 1, 0.0f, 0.0f));
3947 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3948 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3949 0.0f, 1.0f, 0.0f, 0.0f));
3950}
3951
3952TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003954 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955
3956 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003957 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958
3959 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3960 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3961 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3962 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3963 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3964 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3965 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3966 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3967 0.0f, 1.0f, 0.0f, 0.0f));
3968}
3969
3970TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003972 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973
arthurhungdcef2dc2020-08-11 14:47:50 +08003974 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975
3976 NotifyMotionArgs args;
3977
3978 // Button press.
3979 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3983 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3984 ASSERT_EQ(DEVICE_ID, args.deviceId);
3985 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3986 ASSERT_EQ(uint32_t(0), args.policyFlags);
3987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3988 ASSERT_EQ(0, args.flags);
3989 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3990 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3991 ASSERT_EQ(0, args.edgeFlags);
3992 ASSERT_EQ(uint32_t(1), args.pointerCount);
3993 ASSERT_EQ(0, args.pointerProperties[0].id);
3994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003995 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3997 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3998 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3999
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4001 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4002 ASSERT_EQ(DEVICE_ID, args.deviceId);
4003 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4004 ASSERT_EQ(uint32_t(0), args.policyFlags);
4005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4006 ASSERT_EQ(0, args.flags);
4007 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4008 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4009 ASSERT_EQ(0, args.edgeFlags);
4010 ASSERT_EQ(uint32_t(1), args.pointerCount);
4011 ASSERT_EQ(0, args.pointerProperties[0].id);
4012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004013 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004014 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4015 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4016 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4017
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004019 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4020 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4022 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4023 ASSERT_EQ(DEVICE_ID, args.deviceId);
4024 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4025 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4027 ASSERT_EQ(0, args.flags);
4028 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4029 ASSERT_EQ(0, args.buttonState);
4030 ASSERT_EQ(0, args.edgeFlags);
4031 ASSERT_EQ(uint32_t(1), args.pointerCount);
4032 ASSERT_EQ(0, args.pointerProperties[0].id);
4033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004034 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004035 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4036 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4037 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4038
4039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4040 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4041 ASSERT_EQ(DEVICE_ID, args.deviceId);
4042 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4043 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4045 ASSERT_EQ(0, args.flags);
4046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4047 ASSERT_EQ(0, args.buttonState);
4048 ASSERT_EQ(0, args.edgeFlags);
4049 ASSERT_EQ(uint32_t(1), args.pointerCount);
4050 ASSERT_EQ(0, args.pointerProperties[0].id);
4051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004052 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4054 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4055 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4056}
4057
4058TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004060 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061
4062 NotifyMotionArgs args;
4063
4064 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004069 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4070 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4071 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072
4073 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004078 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4079 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080}
4081
4082TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004084 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085
4086 NotifyMotionArgs args;
4087
4088 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4092 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004093 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004097 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004098
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004103 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004104 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004105
4106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004108 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109}
4110
4111TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004113 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114
4115 NotifyMotionArgs args;
4116
4117 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004124 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4125 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4126 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4129 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004130 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4131 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4132 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004133
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004140 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4141 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4142 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143
4144 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004149 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004150
4151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004153 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154}
4155
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004156TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004158 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4159 // need to be rotated.
4160 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004161 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004163 prepareDisplay(DISPLAY_ORIENTATION_90);
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}
4173
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004174TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004176 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4177 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004178 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004180 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4189
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004190 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4192 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4193 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004200 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4203 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4204 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4207 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4209
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004210 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4217 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4218 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219}
4220
4221TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004223 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224
4225 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4226 mFakePointerController->setPosition(100, 200);
4227 mFakePointerController->setButtonState(0);
4228
4229 NotifyMotionArgs motionArgs;
4230 NotifyKeyArgs keyArgs;
4231
4232 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4236 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4237 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4238 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004239 ASSERT_NO_FATAL_FAILURE(
4240 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4243 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4244 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4245 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004246 ASSERT_NO_FATAL_FAILURE(
4247 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004248
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_EQ(0, motionArgs.buttonState);
4254 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004255 ASSERT_NO_FATAL_FAILURE(
4256 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257
4258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 ASSERT_EQ(0, motionArgs.buttonState);
4261 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004262 ASSERT_NO_FATAL_FAILURE(
4263 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264
4265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004267 ASSERT_EQ(0, motionArgs.buttonState);
4268 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004269 ASSERT_NO_FATAL_FAILURE(
4270 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
4272 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4278 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4279 motionArgs.buttonState);
4280 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4281 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004282 ASSERT_NO_FATAL_FAILURE(
4283 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4287 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4288 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4289 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004290 ASSERT_NO_FATAL_FAILURE(
4291 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004292
4293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4295 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4296 motionArgs.buttonState);
4297 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4298 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004299 ASSERT_NO_FATAL_FAILURE(
4300 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004301
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4307 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004308 ASSERT_NO_FATAL_FAILURE(
4309 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004310
4311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4314 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004315 ASSERT_NO_FATAL_FAILURE(
4316 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4322 ASSERT_EQ(0, motionArgs.buttonState);
4323 ASSERT_EQ(0, 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));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004328
4329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_EQ(0, motionArgs.buttonState);
4331 ASSERT_EQ(0, mFakePointerController->getButtonState());
4332 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4337 ASSERT_EQ(0, motionArgs.buttonState);
4338 ASSERT_EQ(0, mFakePointerController->getButtonState());
4339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004340 ASSERT_NO_FATAL_FAILURE(
4341 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342
4343 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4347 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4348 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004349
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4353 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004354 ASSERT_NO_FATAL_FAILURE(
4355 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356
4357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4360 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004361 ASSERT_NO_FATAL_FAILURE(
4362 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_EQ(0, motionArgs.buttonState);
4369 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004370 ASSERT_NO_FATAL_FAILURE(
4371 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004372
4373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375 ASSERT_EQ(0, motionArgs.buttonState);
4376 ASSERT_EQ(0, mFakePointerController->getButtonState());
4377
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004378 ASSERT_NO_FATAL_FAILURE(
4379 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4381 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4382 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4383
4384 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4388 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4389 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004390
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4394 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004395 ASSERT_NO_FATAL_FAILURE(
4396 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004397
4398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4400 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4401 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004402 ASSERT_NO_FATAL_FAILURE(
4403 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_EQ(0, motionArgs.buttonState);
4410 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004411 ASSERT_NO_FATAL_FAILURE(
4412 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004413
4414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4415 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4416 ASSERT_EQ(0, motionArgs.buttonState);
4417 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004418 ASSERT_NO_FATAL_FAILURE(
4419 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004420
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4422 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4423 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4424
4425 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4429 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4430 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004431
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4435 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004436 ASSERT_NO_FATAL_FAILURE(
4437 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004438
4439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4441 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4442 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004443 ASSERT_NO_FATAL_FAILURE(
4444 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_EQ(0, motionArgs.buttonState);
4451 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004452 ASSERT_NO_FATAL_FAILURE(
4453 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004454
4455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4457 ASSERT_EQ(0, motionArgs.buttonState);
4458 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004459 ASSERT_NO_FATAL_FAILURE(
4460 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4463 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4464 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4465
4466 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4470 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4471 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004472
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004474 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4476 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004477 ASSERT_NO_FATAL_FAILURE(
4478 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004479
4480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4481 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4482 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4483 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004484 ASSERT_NO_FATAL_FAILURE(
4485 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004490 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_EQ(0, motionArgs.buttonState);
4492 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004493 ASSERT_NO_FATAL_FAILURE(
4494 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004495
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4497 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4498 ASSERT_EQ(0, motionArgs.buttonState);
4499 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004500 ASSERT_NO_FATAL_FAILURE(
4501 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004502
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4504 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4505 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4506}
4507
4508TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004510 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511
4512 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4513 mFakePointerController->setPosition(100, 200);
4514 mFakePointerController->setButtonState(0);
4515
4516 NotifyMotionArgs args;
4517
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004522 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4525 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 +01004526 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004527}
4528
4529TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004530 addConfigurationProperty("cursor.mode", "pointer");
4531 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004532 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004533
4534 NotifyDeviceResetArgs resetArgs;
4535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4536 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4537 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4538
4539 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4540 mFakePointerController->setPosition(100, 200);
4541 mFakePointerController->setButtonState(0);
4542
4543 NotifyMotionArgs args;
4544
4545 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4550 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4553 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 +01004554 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004555
4556 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4560 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4561 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4563 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4565 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4566 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4568 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4569
4570 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004571 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4572 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4574 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4577 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4579 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4580 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4582 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4583
4584 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4589 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4592 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 +01004593 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004594
4595 // Disable pointer capture and check that the device generation got bumped
4596 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004597 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004598 mFakePolicy->setPointerCapture(false);
4599 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004600 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004601
4602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4603 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4604 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4605
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4610 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4613 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 +01004614 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615}
4616
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004617TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004618 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004619
Garfield Tan888a6a42020-01-09 11:39:16 -08004620 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004621 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004622 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4623 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004624 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4625 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004626 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4627 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4628
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004629 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4630 mFakePointerController->setPosition(100, 200);
4631 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004632
4633 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4638 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4641 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 +01004642 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004643 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4644}
4645
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646// --- TouchInputMapperTest ---
4647
4648class TouchInputMapperTest : public InputMapperTest {
4649protected:
4650 static const int32_t RAW_X_MIN;
4651 static const int32_t RAW_X_MAX;
4652 static const int32_t RAW_Y_MIN;
4653 static const int32_t RAW_Y_MAX;
4654 static const int32_t RAW_TOUCH_MIN;
4655 static const int32_t RAW_TOUCH_MAX;
4656 static const int32_t RAW_TOOL_MIN;
4657 static const int32_t RAW_TOOL_MAX;
4658 static const int32_t RAW_PRESSURE_MIN;
4659 static const int32_t RAW_PRESSURE_MAX;
4660 static const int32_t RAW_ORIENTATION_MIN;
4661 static const int32_t RAW_ORIENTATION_MAX;
4662 static const int32_t RAW_DISTANCE_MIN;
4663 static const int32_t RAW_DISTANCE_MAX;
4664 static const int32_t RAW_TILT_MIN;
4665 static const int32_t RAW_TILT_MAX;
4666 static const int32_t RAW_ID_MIN;
4667 static const int32_t RAW_ID_MAX;
4668 static const int32_t RAW_SLOT_MIN;
4669 static const int32_t RAW_SLOT_MAX;
4670 static const float X_PRECISION;
4671 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004672 static const float X_PRECISION_VIRTUAL;
4673 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674
4675 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004676 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677
4678 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4679
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004680 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004681 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004682
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683 enum Axes {
4684 POSITION = 1 << 0,
4685 TOUCH = 1 << 1,
4686 TOOL = 1 << 2,
4687 PRESSURE = 1 << 3,
4688 ORIENTATION = 1 << 4,
4689 MINOR = 1 << 5,
4690 ID = 1 << 6,
4691 DISTANCE = 1 << 7,
4692 TILT = 1 << 8,
4693 SLOT = 1 << 9,
4694 TOOL_TYPE = 1 << 10,
4695 };
4696
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004697 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4698 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004699 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004701 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004702 int32_t toRawX(float displayX);
4703 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004704 float toCookedX(float rawX, float rawY);
4705 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004707 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004709 float toDisplayY(int32_t rawY, int32_t displayHeight);
4710
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711};
4712
4713const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4714const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4715const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4716const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4717const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4718const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4719const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4720const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004721const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4722const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4724const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4725const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4726const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4727const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4728const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4729const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4730const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4731const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4732const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4733const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4734const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004735const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4736 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4737const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4738 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004739const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4740 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741
4742const float TouchInputMapperTest::GEOMETRIC_SCALE =
4743 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4744 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4745
4746const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4747 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4748 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4749};
4750
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004751void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004752 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4753 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004754}
4755
4756void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4757 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4758 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759}
4760
Santos Cordonfa5cf462017-04-05 10:37:00 -07004761void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004762 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4763 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4764 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004765}
4766
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004768 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4769 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4771 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772}
4773
Jason Gerecke489fda82012-09-07 17:19:40 -07004774void TouchInputMapperTest::prepareLocationCalibration() {
4775 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4776}
4777
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778int32_t TouchInputMapperTest::toRawX(float displayX) {
4779 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4780}
4781
4782int32_t TouchInputMapperTest::toRawY(float displayY) {
4783 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4784}
4785
Jason Gerecke489fda82012-09-07 17:19:40 -07004786float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4787 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4788 return rawX;
4789}
4790
4791float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4792 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4793 return rawY;
4794}
4795
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004797 return toDisplayX(rawX, DISPLAY_WIDTH);
4798}
4799
4800float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4801 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802}
4803
4804float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004805 return toDisplayY(rawY, DISPLAY_HEIGHT);
4806}
4807
4808float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4809 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810}
4811
4812
4813// --- SingleTouchInputMapperTest ---
4814
4815class SingleTouchInputMapperTest : public TouchInputMapperTest {
4816protected:
4817 void prepareButtons();
4818 void prepareAxes(int axes);
4819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004820 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4821 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4822 void processUp(SingleTouchInputMapper& mappery);
4823 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4824 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4825 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4826 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4827 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4828 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829};
4830
4831void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004832 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833}
4834
4835void SingleTouchInputMapperTest::prepareAxes(int axes) {
4836 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004837 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4838 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 }
4840 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004841 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4842 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 }
4844 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004845 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4846 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847 }
4848 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004849 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4850 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 }
4852 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004853 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4854 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855 }
4856}
4857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004858void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862}
4863
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004864void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867}
4868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004869void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871}
4872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004873void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875}
4876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004877void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4878 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880}
4881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004882void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884}
4885
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004886void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4887 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890}
4891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004892void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4893 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004895}
4896
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004897void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899}
4900
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 prepareButtons();
4903 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004904 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004906 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907}
4908
4909TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004910 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4911 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 prepareButtons();
4913 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004914 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004916 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917}
4918
4919TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 prepareButtons();
4921 prepareAxes(POSITION);
4922 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004923 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004925 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926}
4927
4928TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929 prepareButtons();
4930 prepareAxes(POSITION);
4931 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004932 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004934 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935}
4936
4937TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 addConfigurationProperty("touch.deviceType", "touchScreen");
4939 prepareDisplay(DISPLAY_ORIENTATION_0);
4940 prepareButtons();
4941 prepareAxes(POSITION);
4942 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004943 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944
4945 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004946 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947
4948 // Virtual key is down.
4949 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4950 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4951 processDown(mapper, x, y);
4952 processSync(mapper);
4953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4954
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004955 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956
4957 // Virtual key is up.
4958 processUp(mapper);
4959 processSync(mapper);
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004962 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963}
4964
4965TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 addConfigurationProperty("touch.deviceType", "touchScreen");
4967 prepareDisplay(DISPLAY_ORIENTATION_0);
4968 prepareButtons();
4969 prepareAxes(POSITION);
4970 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004971 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972
4973 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004974 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975
4976 // Virtual key is down.
4977 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4978 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4979 processDown(mapper, x, y);
4980 processSync(mapper);
4981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4982
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004983 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984
4985 // Virtual key is up.
4986 processUp(mapper);
4987 processSync(mapper);
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004990 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991}
4992
4993TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 addConfigurationProperty("touch.deviceType", "touchScreen");
4995 prepareDisplay(DISPLAY_ORIENTATION_0);
4996 prepareButtons();
4997 prepareAxes(POSITION);
4998 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004999 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000
5001 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5002 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005003 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 ASSERT_TRUE(flags[0]);
5005 ASSERT_FALSE(flags[1]);
5006}
5007
5008TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009 addConfigurationProperty("touch.deviceType", "touchScreen");
5010 prepareDisplay(DISPLAY_ORIENTATION_0);
5011 prepareButtons();
5012 prepareAxes(POSITION);
5013 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005014 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015
arthurhungdcef2dc2020-08-11 14:47:50 +08005016 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017
5018 NotifyKeyArgs args;
5019
5020 // Press virtual key.
5021 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5022 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5023 processDown(mapper, x, y);
5024 processSync(mapper);
5025
5026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5027 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5028 ASSERT_EQ(DEVICE_ID, args.deviceId);
5029 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5030 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5031 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5032 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5033 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5034 ASSERT_EQ(KEY_HOME, args.scanCode);
5035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5036 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5037
5038 // Release virtual key.
5039 processUp(mapper);
5040 processSync(mapper);
5041
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5043 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5044 ASSERT_EQ(DEVICE_ID, args.deviceId);
5045 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5046 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5047 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5048 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5049 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5050 ASSERT_EQ(KEY_HOME, args.scanCode);
5051 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5052 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5053
5054 // Should not have sent any motions.
5055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5056}
5057
5058TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059 addConfigurationProperty("touch.deviceType", "touchScreen");
5060 prepareDisplay(DISPLAY_ORIENTATION_0);
5061 prepareButtons();
5062 prepareAxes(POSITION);
5063 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005064 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065
arthurhungdcef2dc2020-08-11 14:47:50 +08005066 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067
5068 NotifyKeyArgs keyArgs;
5069
5070 // Press virtual key.
5071 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5072 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5073 processDown(mapper, x, y);
5074 processSync(mapper);
5075
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5077 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5078 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5079 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5080 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5081 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5082 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5083 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5084 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5085 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5086 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5087
5088 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5089 // into the display area.
5090 y -= 100;
5091 processMove(mapper, x, y);
5092 processSync(mapper);
5093
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5095 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5096 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5097 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5098 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5099 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5100 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5101 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5102 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5103 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5104 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5105 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5106
5107 NotifyMotionArgs motionArgs;
5108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5109 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5110 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5111 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5112 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5113 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5114 ASSERT_EQ(0, motionArgs.flags);
5115 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5116 ASSERT_EQ(0, motionArgs.buttonState);
5117 ASSERT_EQ(0, motionArgs.edgeFlags);
5118 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5119 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5122 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5123 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5124 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5125 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5126
5127 // Keep moving out of bounds. Should generate a pointer move.
5128 y -= 50;
5129 processMove(mapper, x, y);
5130 processSync(mapper);
5131
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5133 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5134 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5135 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5136 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5138 ASSERT_EQ(0, motionArgs.flags);
5139 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5140 ASSERT_EQ(0, motionArgs.buttonState);
5141 ASSERT_EQ(0, motionArgs.edgeFlags);
5142 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5143 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5146 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5147 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5148 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5149 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5150
5151 // Release out of bounds. Should generate a pointer up.
5152 processUp(mapper);
5153 processSync(mapper);
5154
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5157 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5158 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5159 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5160 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5161 ASSERT_EQ(0, motionArgs.flags);
5162 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5163 ASSERT_EQ(0, motionArgs.buttonState);
5164 ASSERT_EQ(0, motionArgs.edgeFlags);
5165 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5166 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5169 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5170 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5171 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5172 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5173
5174 // Should not have sent any more keys or motions.
5175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5177}
5178
5179TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180 addConfigurationProperty("touch.deviceType", "touchScreen");
5181 prepareDisplay(DISPLAY_ORIENTATION_0);
5182 prepareButtons();
5183 prepareAxes(POSITION);
5184 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005185 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005186
arthurhungdcef2dc2020-08-11 14:47:50 +08005187 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188
5189 NotifyMotionArgs motionArgs;
5190
5191 // Initially go down out of bounds.
5192 int32_t x = -10;
5193 int32_t y = -10;
5194 processDown(mapper, x, y);
5195 processSync(mapper);
5196
5197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5198
5199 // Move into the display area. Should generate a pointer down.
5200 x = 50;
5201 y = 75;
5202 processMove(mapper, x, y);
5203 processSync(mapper);
5204
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5206 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5207 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5208 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5209 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5210 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5211 ASSERT_EQ(0, motionArgs.flags);
5212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5213 ASSERT_EQ(0, motionArgs.buttonState);
5214 ASSERT_EQ(0, motionArgs.edgeFlags);
5215 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5216 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5219 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5220 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5221 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5222 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5223
5224 // Release. Should generate a pointer up.
5225 processUp(mapper);
5226 processSync(mapper);
5227
5228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5229 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5230 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5231 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5232 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5233 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5234 ASSERT_EQ(0, motionArgs.flags);
5235 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5236 ASSERT_EQ(0, motionArgs.buttonState);
5237 ASSERT_EQ(0, motionArgs.edgeFlags);
5238 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5239 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5242 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5243 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5244 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5246
5247 // Should not have sent any more keys or motions.
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5250}
5251
Santos Cordonfa5cf462017-04-05 10:37:00 -07005252TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005253 addConfigurationProperty("touch.deviceType", "touchScreen");
5254 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5255
5256 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5257 prepareButtons();
5258 prepareAxes(POSITION);
5259 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005260 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005261
arthurhungdcef2dc2020-08-11 14:47:50 +08005262 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005263
5264 NotifyMotionArgs motionArgs;
5265
5266 // Down.
5267 int32_t x = 100;
5268 int32_t y = 125;
5269 processDown(mapper, x, y);
5270 processSync(mapper);
5271
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5275 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5276 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5277 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5278 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5279 ASSERT_EQ(0, motionArgs.flags);
5280 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5281 ASSERT_EQ(0, motionArgs.buttonState);
5282 ASSERT_EQ(0, motionArgs.edgeFlags);
5283 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5287 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5288 1, 0, 0, 0, 0, 0, 0, 0));
5289 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5290 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5292
5293 // Move.
5294 x += 50;
5295 y += 75;
5296 processMove(mapper, x, y);
5297 processSync(mapper);
5298
5299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5301 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5302 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5303 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5304 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5306 ASSERT_EQ(0, motionArgs.flags);
5307 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5308 ASSERT_EQ(0, motionArgs.buttonState);
5309 ASSERT_EQ(0, motionArgs.edgeFlags);
5310 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5311 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5314 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5315 1, 0, 0, 0, 0, 0, 0, 0));
5316 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5317 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5318 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5319
5320 // Up.
5321 processUp(mapper);
5322 processSync(mapper);
5323
5324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5325 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5326 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5327 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5328 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5329 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5331 ASSERT_EQ(0, motionArgs.flags);
5332 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5333 ASSERT_EQ(0, motionArgs.buttonState);
5334 ASSERT_EQ(0, motionArgs.edgeFlags);
5335 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5336 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5339 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5340 1, 0, 0, 0, 0, 0, 0, 0));
5341 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5342 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5344
5345 // Should not have sent any more keys or motions.
5346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5348}
5349
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351 addConfigurationProperty("touch.deviceType", "touchScreen");
5352 prepareDisplay(DISPLAY_ORIENTATION_0);
5353 prepareButtons();
5354 prepareAxes(POSITION);
5355 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005356 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357
arthurhungdcef2dc2020-08-11 14:47:50 +08005358 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359
5360 NotifyMotionArgs motionArgs;
5361
5362 // Down.
5363 int32_t x = 100;
5364 int32_t y = 125;
5365 processDown(mapper, x, y);
5366 processSync(mapper);
5367
5368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5370 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5371 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5372 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5373 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5374 ASSERT_EQ(0, motionArgs.flags);
5375 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5376 ASSERT_EQ(0, motionArgs.buttonState);
5377 ASSERT_EQ(0, motionArgs.edgeFlags);
5378 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5379 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5382 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5383 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5384 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5385 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5386
5387 // Move.
5388 x += 50;
5389 y += 75;
5390 processMove(mapper, x, y);
5391 processSync(mapper);
5392
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5395 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5396 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5397 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5399 ASSERT_EQ(0, motionArgs.flags);
5400 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5401 ASSERT_EQ(0, motionArgs.buttonState);
5402 ASSERT_EQ(0, motionArgs.edgeFlags);
5403 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5404 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5407 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5408 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5409 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5410 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5411
5412 // Up.
5413 processUp(mapper);
5414 processSync(mapper);
5415
5416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5417 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5418 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5419 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5420 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5421 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5422 ASSERT_EQ(0, motionArgs.flags);
5423 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5424 ASSERT_EQ(0, motionArgs.buttonState);
5425 ASSERT_EQ(0, motionArgs.edgeFlags);
5426 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5427 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5430 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5431 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5432 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5433 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5434
5435 // Should not have sent any more keys or motions.
5436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5438}
5439
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005440TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005441 addConfigurationProperty("touch.deviceType", "touchScreen");
5442 prepareButtons();
5443 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005444 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5445 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005446 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447
5448 NotifyMotionArgs args;
5449
5450 // Rotation 90.
5451 prepareDisplay(DISPLAY_ORIENTATION_90);
5452 processDown(mapper, toRawX(50), toRawY(75));
5453 processSync(mapper);
5454
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5456 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5457 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5458
5459 processUp(mapper);
5460 processSync(mapper);
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5462}
5463
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005464TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465 addConfigurationProperty("touch.deviceType", "touchScreen");
5466 prepareButtons();
5467 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005468 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5469 // orientation-aware are affected by display rotation.
5470 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005471 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472
5473 NotifyMotionArgs args;
5474
5475 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005476 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477 prepareDisplay(DISPLAY_ORIENTATION_0);
5478 processDown(mapper, toRawX(50), toRawY(75));
5479 processSync(mapper);
5480
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5482 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5483 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5484
5485 processUp(mapper);
5486 processSync(mapper);
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5488
5489 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005490 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005492 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493 processSync(mapper);
5494
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5496 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5497 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5498
5499 processUp(mapper);
5500 processSync(mapper);
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5502
5503 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005504 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505 prepareDisplay(DISPLAY_ORIENTATION_180);
5506 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5507 processSync(mapper);
5508
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5510 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5511 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5512
5513 processUp(mapper);
5514 processSync(mapper);
5515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5516
5517 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005518 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005520 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005521 processSync(mapper);
5522
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5524 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5525 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5526
5527 processUp(mapper);
5528 processSync(mapper);
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5530}
5531
5532TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005533 addConfigurationProperty("touch.deviceType", "touchScreen");
5534 prepareDisplay(DISPLAY_ORIENTATION_0);
5535 prepareButtons();
5536 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005537 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005538
5539 // These calculations are based on the input device calibration documentation.
5540 int32_t rawX = 100;
5541 int32_t rawY = 200;
5542 int32_t rawPressure = 10;
5543 int32_t rawToolMajor = 12;
5544 int32_t rawDistance = 2;
5545 int32_t rawTiltX = 30;
5546 int32_t rawTiltY = 110;
5547
5548 float x = toDisplayX(rawX);
5549 float y = toDisplayY(rawY);
5550 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5551 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5552 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5553 float distance = float(rawDistance);
5554
5555 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5556 float tiltScale = M_PI / 180;
5557 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5558 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5559 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5560 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5561
5562 processDown(mapper, rawX, rawY);
5563 processPressure(mapper, rawPressure);
5564 processToolMajor(mapper, rawToolMajor);
5565 processDistance(mapper, rawDistance);
5566 processTilt(mapper, rawTiltX, rawTiltY);
5567 processSync(mapper);
5568
5569 NotifyMotionArgs args;
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5572 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5573 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5574}
5575
Jason Gerecke489fda82012-09-07 17:19:40 -07005576TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005577 addConfigurationProperty("touch.deviceType", "touchScreen");
5578 prepareDisplay(DISPLAY_ORIENTATION_0);
5579 prepareLocationCalibration();
5580 prepareButtons();
5581 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005582 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005583
5584 int32_t rawX = 100;
5585 int32_t rawY = 200;
5586
5587 float x = toDisplayX(toCookedX(rawX, rawY));
5588 float y = toDisplayY(toCookedY(rawX, rawY));
5589
5590 processDown(mapper, rawX, rawY);
5591 processSync(mapper);
5592
5593 NotifyMotionArgs args;
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5596 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5597}
5598
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600 addConfigurationProperty("touch.deviceType", "touchScreen");
5601 prepareDisplay(DISPLAY_ORIENTATION_0);
5602 prepareButtons();
5603 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005604 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605
5606 NotifyMotionArgs motionArgs;
5607 NotifyKeyArgs keyArgs;
5608
5609 processDown(mapper, 100, 200);
5610 processSync(mapper);
5611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5612 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5613 ASSERT_EQ(0, motionArgs.buttonState);
5614
5615 // press BTN_LEFT, release BTN_LEFT
5616 processKey(mapper, BTN_LEFT, 1);
5617 processSync(mapper);
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5620 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5621
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5624 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5625
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626 processKey(mapper, BTN_LEFT, 0);
5627 processSync(mapper);
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005630 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005631
5632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005634 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635
5636 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5637 processKey(mapper, BTN_RIGHT, 1);
5638 processKey(mapper, BTN_MIDDLE, 1);
5639 processSync(mapper);
5640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5642 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5643 motionArgs.buttonState);
5644
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5647 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5648
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5650 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5651 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5652 motionArgs.buttonState);
5653
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654 processKey(mapper, BTN_RIGHT, 0);
5655 processSync(mapper);
5656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005657 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005659
5660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005662 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663
5664 processKey(mapper, BTN_MIDDLE, 0);
5665 processSync(mapper);
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005669
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005672 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005673
5674 // press BTN_BACK, release BTN_BACK
5675 processKey(mapper, BTN_BACK, 1);
5676 processSync(mapper);
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5678 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5679 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005680
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005683 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5684
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5686 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5687 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688
5689 processKey(mapper, BTN_BACK, 0);
5690 processSync(mapper);
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005697 ASSERT_EQ(0, motionArgs.buttonState);
5698
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5700 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5701 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5702
5703 // press BTN_SIDE, release BTN_SIDE
5704 processKey(mapper, BTN_SIDE, 1);
5705 processSync(mapper);
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5707 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5708 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005709
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005712 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5713
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5715 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5716 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717
5718 processKey(mapper, BTN_SIDE, 0);
5719 processSync(mapper);
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005723
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005726 ASSERT_EQ(0, motionArgs.buttonState);
5727
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5729 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5730 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5731
5732 // press BTN_FORWARD, release BTN_FORWARD
5733 processKey(mapper, BTN_FORWARD, 1);
5734 processSync(mapper);
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5736 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5737 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005738
Michael Wrightd02c5b62014-02-10 15:10:22 -08005739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005741 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5742
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5745 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746
5747 processKey(mapper, BTN_FORWARD, 0);
5748 processSync(mapper);
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005751 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005752
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005755 ASSERT_EQ(0, motionArgs.buttonState);
5756
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5758 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5759 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5760
5761 // press BTN_EXTRA, release BTN_EXTRA
5762 processKey(mapper, BTN_EXTRA, 1);
5763 processSync(mapper);
5764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5765 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5766 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005767
Michael Wrightd02c5b62014-02-10 15:10:22 -08005768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005770 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5771
5772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5773 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5774 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775
5776 processKey(mapper, BTN_EXTRA, 0);
5777 processSync(mapper);
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005780 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005781
5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005784 ASSERT_EQ(0, motionArgs.buttonState);
5785
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5787 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5788 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5789
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5791
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792 // press BTN_STYLUS, release BTN_STYLUS
5793 processKey(mapper, BTN_STYLUS, 1);
5794 processSync(mapper);
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005797 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5798
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5801 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802
5803 processKey(mapper, BTN_STYLUS, 0);
5804 processSync(mapper);
5805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005808
5809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005811 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005812
5813 // press BTN_STYLUS2, release BTN_STYLUS2
5814 processKey(mapper, BTN_STYLUS2, 1);
5815 processSync(mapper);
5816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005818 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5819
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5822 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005823
5824 processKey(mapper, BTN_STYLUS2, 0);
5825 processSync(mapper);
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005827 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005829
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005832 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005833
5834 // release touch
5835 processUp(mapper);
5836 processSync(mapper);
5837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5838 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5839 ASSERT_EQ(0, motionArgs.buttonState);
5840}
5841
5842TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843 addConfigurationProperty("touch.deviceType", "touchScreen");
5844 prepareDisplay(DISPLAY_ORIENTATION_0);
5845 prepareButtons();
5846 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005847 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848
5849 NotifyMotionArgs motionArgs;
5850
5851 // default tool type is finger
5852 processDown(mapper, 100, 200);
5853 processSync(mapper);
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5855 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5857
5858 // eraser
5859 processKey(mapper, BTN_TOOL_RUBBER, 1);
5860 processSync(mapper);
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5863 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5864
5865 // stylus
5866 processKey(mapper, BTN_TOOL_RUBBER, 0);
5867 processKey(mapper, BTN_TOOL_PEN, 1);
5868 processSync(mapper);
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5871 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5872
5873 // brush
5874 processKey(mapper, BTN_TOOL_PEN, 0);
5875 processKey(mapper, BTN_TOOL_BRUSH, 1);
5876 processSync(mapper);
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5879 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5880
5881 // pencil
5882 processKey(mapper, BTN_TOOL_BRUSH, 0);
5883 processKey(mapper, BTN_TOOL_PENCIL, 1);
5884 processSync(mapper);
5885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5888
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005889 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890 processKey(mapper, BTN_TOOL_PENCIL, 0);
5891 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5892 processSync(mapper);
5893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5896
5897 // mouse
5898 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5899 processKey(mapper, BTN_TOOL_MOUSE, 1);
5900 processSync(mapper);
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5903 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5904
5905 // lens
5906 processKey(mapper, BTN_TOOL_MOUSE, 0);
5907 processKey(mapper, BTN_TOOL_LENS, 1);
5908 processSync(mapper);
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5912
5913 // double-tap
5914 processKey(mapper, BTN_TOOL_LENS, 0);
5915 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5916 processSync(mapper);
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5920
5921 // triple-tap
5922 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5923 processKey(mapper, BTN_TOOL_TRIPLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
5928
5929 // quad-tap
5930 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5931 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5932 processSync(mapper);
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5936
5937 // finger
5938 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5939 processKey(mapper, BTN_TOOL_FINGER, 1);
5940 processSync(mapper);
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5944
5945 // stylus trumps finger
5946 processKey(mapper, BTN_TOOL_PEN, 1);
5947 processSync(mapper);
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5950 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5951
5952 // eraser trumps stylus
5953 processKey(mapper, BTN_TOOL_RUBBER, 1);
5954 processSync(mapper);
5955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5958
5959 // mouse trumps eraser
5960 processKey(mapper, BTN_TOOL_MOUSE, 1);
5961 processSync(mapper);
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5965
5966 // back to default tool type
5967 processKey(mapper, BTN_TOOL_MOUSE, 0);
5968 processKey(mapper, BTN_TOOL_RUBBER, 0);
5969 processKey(mapper, BTN_TOOL_PEN, 0);
5970 processKey(mapper, BTN_TOOL_FINGER, 0);
5971 processSync(mapper);
5972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5975}
5976
5977TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005978 addConfigurationProperty("touch.deviceType", "touchScreen");
5979 prepareDisplay(DISPLAY_ORIENTATION_0);
5980 prepareButtons();
5981 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005982 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005983 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984
5985 NotifyMotionArgs motionArgs;
5986
5987 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5988 processKey(mapper, BTN_TOOL_FINGER, 1);
5989 processMove(mapper, 100, 200);
5990 processSync(mapper);
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5992 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5994 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5995
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5997 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5999 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6000
6001 // move a little
6002 processMove(mapper, 150, 250);
6003 processSync(mapper);
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6005 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6007 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6008
6009 // down when BTN_TOUCH is pressed, pressure defaults to 1
6010 processKey(mapper, BTN_TOUCH, 1);
6011 processSync(mapper);
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6013 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6015 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6016
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6018 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6020 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6021
6022 // up when BTN_TOUCH is released, hover restored
6023 processKey(mapper, BTN_TOUCH, 0);
6024 processSync(mapper);
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6028 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6029
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6031 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6033 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6034
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6036 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6038 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6039
6040 // exit hover when pointer goes away
6041 processKey(mapper, BTN_TOOL_FINGER, 0);
6042 processSync(mapper);
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6044 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6046 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6047}
6048
6049TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050 addConfigurationProperty("touch.deviceType", "touchScreen");
6051 prepareDisplay(DISPLAY_ORIENTATION_0);
6052 prepareButtons();
6053 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006054 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055
6056 NotifyMotionArgs motionArgs;
6057
6058 // initially hovering because pressure is 0
6059 processDown(mapper, 100, 200);
6060 processPressure(mapper, 0);
6061 processSync(mapper);
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6063 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6065 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6066
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6068 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6070 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6071
6072 // move a little
6073 processMove(mapper, 150, 250);
6074 processSync(mapper);
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6076 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6078 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6079
6080 // down when pressure is non-zero
6081 processPressure(mapper, RAW_PRESSURE_MAX);
6082 processSync(mapper);
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6086 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6087
6088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6089 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6091 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6092
6093 // up when pressure becomes 0, hover restored
6094 processPressure(mapper, 0);
6095 processSync(mapper);
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6097 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6099 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6100
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6102 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6104 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6105
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6107 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6109 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6110
6111 // exit hover when pointer goes away
6112 processUp(mapper);
6113 processSync(mapper);
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6115 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6117 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6118}
6119
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120// --- MultiTouchInputMapperTest ---
6121
6122class MultiTouchInputMapperTest : public TouchInputMapperTest {
6123protected:
6124 void prepareAxes(int axes);
6125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006126 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6127 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6128 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6129 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6130 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6131 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6132 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6133 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6134 void processId(MultiTouchInputMapper& mapper, int32_t id);
6135 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6136 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6137 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6138 void processMTSync(MultiTouchInputMapper& mapper);
6139 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140};
6141
6142void MultiTouchInputMapperTest::prepareAxes(int axes) {
6143 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006144 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6145 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146 }
6147 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006148 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6149 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006151 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6152 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153 }
6154 }
6155 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006156 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6157 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006159 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6160 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161 }
6162 }
6163 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006164 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6165 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 }
6167 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006168 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6169 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170 }
6171 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006172 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6173 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174 }
6175 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006176 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6177 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 }
6179 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006180 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6181 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 }
6183 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006184 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 }
6186}
6187
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006188void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6189 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192}
6193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006194void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6195 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197}
6198
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006199void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6200 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202}
6203
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006204void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206}
6207
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006208void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210}
6211
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006212void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6213 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215}
6216
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006217void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006219}
6220
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006221void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006222 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006223}
6224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006225void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227}
6228
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006229void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231}
6232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006233void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235}
6236
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006237void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6238 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240}
6241
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006242void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244}
6245
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006246void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248}
6249
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251 addConfigurationProperty("touch.deviceType", "touchScreen");
6252 prepareDisplay(DISPLAY_ORIENTATION_0);
6253 prepareAxes(POSITION);
6254 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006255 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256
arthurhungdcef2dc2020-08-11 14:47:50 +08006257 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258
6259 NotifyMotionArgs motionArgs;
6260
6261 // Two fingers down at once.
6262 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6263 processPosition(mapper, x1, y1);
6264 processMTSync(mapper);
6265 processPosition(mapper, x2, y2);
6266 processMTSync(mapper);
6267 processSync(mapper);
6268
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6271 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6272 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6273 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6274 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6275 ASSERT_EQ(0, motionArgs.flags);
6276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6277 ASSERT_EQ(0, motionArgs.buttonState);
6278 ASSERT_EQ(0, motionArgs.edgeFlags);
6279 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6283 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6284 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6285 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6287
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6290 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6291 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6292 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6294 motionArgs.action);
6295 ASSERT_EQ(0, motionArgs.flags);
6296 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6297 ASSERT_EQ(0, motionArgs.buttonState);
6298 ASSERT_EQ(0, motionArgs.edgeFlags);
6299 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6300 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6302 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6305 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6307 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6308 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6309 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6311
6312 // Move.
6313 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6314 processPosition(mapper, x1, y1);
6315 processMTSync(mapper);
6316 processPosition(mapper, x2, y2);
6317 processMTSync(mapper);
6318 processSync(mapper);
6319
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6322 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6326 ASSERT_EQ(0, motionArgs.flags);
6327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6328 ASSERT_EQ(0, motionArgs.buttonState);
6329 ASSERT_EQ(0, motionArgs.edgeFlags);
6330 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6333 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6336 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6338 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6339 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6340 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6342
6343 // First finger up.
6344 x2 += 15; y2 -= 20;
6345 processPosition(mapper, x2, y2);
6346 processMTSync(mapper);
6347 processSync(mapper);
6348
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6355 motionArgs.action);
6356 ASSERT_EQ(0, motionArgs.flags);
6357 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6358 ASSERT_EQ(0, motionArgs.buttonState);
6359 ASSERT_EQ(0, motionArgs.edgeFlags);
6360 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6361 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6363 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6366 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6368 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6369 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6370 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6371 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6372
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6375 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6376 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6377 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6379 ASSERT_EQ(0, motionArgs.flags);
6380 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6381 ASSERT_EQ(0, motionArgs.buttonState);
6382 ASSERT_EQ(0, motionArgs.edgeFlags);
6383 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6384 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6387 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6388 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6389 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6390 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6391
6392 // Move.
6393 x2 += 20; y2 -= 25;
6394 processPosition(mapper, x2, y2);
6395 processMTSync(mapper);
6396 processSync(mapper);
6397
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6399 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6400 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6401 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6402 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6404 ASSERT_EQ(0, motionArgs.flags);
6405 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6406 ASSERT_EQ(0, motionArgs.buttonState);
6407 ASSERT_EQ(0, motionArgs.edgeFlags);
6408 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6409 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6412 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6413 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6414 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6416
6417 // New finger down.
6418 int32_t x3 = 700, y3 = 300;
6419 processPosition(mapper, x2, y2);
6420 processMTSync(mapper);
6421 processPosition(mapper, x3, y3);
6422 processMTSync(mapper);
6423 processSync(mapper);
6424
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6426 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6427 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6428 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6429 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6431 motionArgs.action);
6432 ASSERT_EQ(0, motionArgs.flags);
6433 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6434 ASSERT_EQ(0, motionArgs.buttonState);
6435 ASSERT_EQ(0, motionArgs.edgeFlags);
6436 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6437 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6439 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6442 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6444 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6445 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6446 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6447 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6448
6449 // Second finger up.
6450 x3 += 30; y3 -= 20;
6451 processPosition(mapper, x3, y3);
6452 processMTSync(mapper);
6453 processSync(mapper);
6454
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6456 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6457 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6458 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6459 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6460 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6461 motionArgs.action);
6462 ASSERT_EQ(0, motionArgs.flags);
6463 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6464 ASSERT_EQ(0, motionArgs.buttonState);
6465 ASSERT_EQ(0, motionArgs.edgeFlags);
6466 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6467 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6469 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6472 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6474 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6475 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6476 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6477 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6478
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6481 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6482 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6483 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6485 ASSERT_EQ(0, motionArgs.flags);
6486 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6487 ASSERT_EQ(0, motionArgs.buttonState);
6488 ASSERT_EQ(0, motionArgs.edgeFlags);
6489 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6490 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6491 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6493 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6494 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6495 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6496 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6497
6498 // Last finger up.
6499 processMTSync(mapper);
6500 processSync(mapper);
6501
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6504 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6505 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6506 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6507 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6508 ASSERT_EQ(0, motionArgs.flags);
6509 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6510 ASSERT_EQ(0, motionArgs.buttonState);
6511 ASSERT_EQ(0, motionArgs.edgeFlags);
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(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6517 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6518 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6519 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6520
6521 // Should not have sent any more keys or motions.
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6524}
6525
6526TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527 addConfigurationProperty("touch.deviceType", "touchScreen");
6528 prepareDisplay(DISPLAY_ORIENTATION_0);
6529 prepareAxes(POSITION | ID);
6530 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006531 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006532
arthurhungdcef2dc2020-08-11 14:47:50 +08006533 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006534
6535 NotifyMotionArgs motionArgs;
6536
6537 // Two fingers down at once.
6538 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6539 processPosition(mapper, x1, y1);
6540 processId(mapper, 1);
6541 processMTSync(mapper);
6542 processPosition(mapper, x2, y2);
6543 processId(mapper, 2);
6544 processMTSync(mapper);
6545 processSync(mapper);
6546
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6548 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6549 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6550 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6553 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6554
6555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6556 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6557 motionArgs.action);
6558 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6559 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6561 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6564 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6566 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6567
6568 // Move.
6569 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6570 processPosition(mapper, x1, y1);
6571 processId(mapper, 1);
6572 processMTSync(mapper);
6573 processPosition(mapper, x2, y2);
6574 processId(mapper, 2);
6575 processMTSync(mapper);
6576 processSync(mapper);
6577
6578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6580 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6583 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6586 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6588 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6589
6590 // First finger up.
6591 x2 += 15; y2 -= 20;
6592 processPosition(mapper, x2, y2);
6593 processId(mapper, 2);
6594 processMTSync(mapper);
6595 processSync(mapper);
6596
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6599 motionArgs.action);
6600 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6603 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6606 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6608 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6609
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6612 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6613 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6616 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6617
6618 // Move.
6619 x2 += 20; y2 -= 25;
6620 processPosition(mapper, x2, y2);
6621 processId(mapper, 2);
6622 processMTSync(mapper);
6623 processSync(mapper);
6624
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6627 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6628 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6631 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6632
6633 // New finger down.
6634 int32_t x3 = 700, y3 = 300;
6635 processPosition(mapper, x2, y2);
6636 processId(mapper, 2);
6637 processMTSync(mapper);
6638 processPosition(mapper, x3, y3);
6639 processId(mapper, 3);
6640 processMTSync(mapper);
6641 processSync(mapper);
6642
6643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6644 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6645 motionArgs.action);
6646 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6647 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6649 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6652 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6654 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6655
6656 // Second finger up.
6657 x3 += 30; y3 -= 20;
6658 processPosition(mapper, x3, y3);
6659 processId(mapper, 3);
6660 processMTSync(mapper);
6661 processSync(mapper);
6662
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6664 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6665 motionArgs.action);
6666 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6667 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6669 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6672 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6674 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6675
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6678 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6679 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6682 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6683
6684 // Last finger up.
6685 processMTSync(mapper);
6686 processSync(mapper);
6687
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6689 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6690 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6694 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6695
6696 // Should not have sent any more keys or motions.
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6699}
6700
6701TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006702 addConfigurationProperty("touch.deviceType", "touchScreen");
6703 prepareDisplay(DISPLAY_ORIENTATION_0);
6704 prepareAxes(POSITION | ID | SLOT);
6705 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006706 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006707
arthurhungdcef2dc2020-08-11 14:47:50 +08006708 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006709
6710 NotifyMotionArgs motionArgs;
6711
6712 // Two fingers down at once.
6713 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6714 processPosition(mapper, x1, y1);
6715 processId(mapper, 1);
6716 processSlot(mapper, 1);
6717 processPosition(mapper, x2, y2);
6718 processId(mapper, 2);
6719 processSync(mapper);
6720
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6722 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6723 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6724 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6727 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6728
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6730 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6731 motionArgs.action);
6732 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6733 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6735 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6738 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6740 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6741
6742 // Move.
6743 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6744 processSlot(mapper, 0);
6745 processPosition(mapper, x1, y1);
6746 processSlot(mapper, 1);
6747 processPosition(mapper, x2, y2);
6748 processSync(mapper);
6749
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6752 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6755 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6758 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6760 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6761
6762 // First finger up.
6763 x2 += 15; y2 -= 20;
6764 processSlot(mapper, 0);
6765 processId(mapper, -1);
6766 processSlot(mapper, 1);
6767 processPosition(mapper, x2, y2);
6768 processSync(mapper);
6769
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6771 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6772 motionArgs.action);
6773 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6774 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6775 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6776 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6779 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6781 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6782
6783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6785 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6786 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6789 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6790
6791 // Move.
6792 x2 += 20; y2 -= 25;
6793 processPosition(mapper, x2, y2);
6794 processSync(mapper);
6795
6796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6798 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6799 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6802 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6803
6804 // New finger down.
6805 int32_t x3 = 700, y3 = 300;
6806 processPosition(mapper, x2, y2);
6807 processSlot(mapper, 0);
6808 processId(mapper, 3);
6809 processPosition(mapper, x3, y3);
6810 processSync(mapper);
6811
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6813 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6814 motionArgs.action);
6815 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6816 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6818 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6821 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6823 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6824
6825 // Second finger up.
6826 x3 += 30; y3 -= 20;
6827 processSlot(mapper, 1);
6828 processId(mapper, -1);
6829 processSlot(mapper, 0);
6830 processPosition(mapper, x3, y3);
6831 processSync(mapper);
6832
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6834 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6835 motionArgs.action);
6836 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6837 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6839 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6842 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6844 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6845
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6848 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6849 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6852 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6853
6854 // Last finger up.
6855 processId(mapper, -1);
6856 processSync(mapper);
6857
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6859 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6860 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6861 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6864 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6865
6866 // Should not have sent any more keys or motions.
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6869}
6870
6871TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006872 addConfigurationProperty("touch.deviceType", "touchScreen");
6873 prepareDisplay(DISPLAY_ORIENTATION_0);
6874 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006875 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006876
6877 // These calculations are based on the input device calibration documentation.
6878 int32_t rawX = 100;
6879 int32_t rawY = 200;
6880 int32_t rawTouchMajor = 7;
6881 int32_t rawTouchMinor = 6;
6882 int32_t rawToolMajor = 9;
6883 int32_t rawToolMinor = 8;
6884 int32_t rawPressure = 11;
6885 int32_t rawDistance = 0;
6886 int32_t rawOrientation = 3;
6887 int32_t id = 5;
6888
6889 float x = toDisplayX(rawX);
6890 float y = toDisplayY(rawY);
6891 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6892 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6893 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6894 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6895 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6896 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6897 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6898 float distance = float(rawDistance);
6899
6900 processPosition(mapper, rawX, rawY);
6901 processTouchMajor(mapper, rawTouchMajor);
6902 processTouchMinor(mapper, rawTouchMinor);
6903 processToolMajor(mapper, rawToolMajor);
6904 processToolMinor(mapper, rawToolMinor);
6905 processPressure(mapper, rawPressure);
6906 processOrientation(mapper, rawOrientation);
6907 processDistance(mapper, rawDistance);
6908 processId(mapper, id);
6909 processMTSync(mapper);
6910 processSync(mapper);
6911
6912 NotifyMotionArgs args;
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6914 ASSERT_EQ(0, args.pointerProperties[0].id);
6915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6916 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6917 orientation, distance));
6918}
6919
6920TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006921 addConfigurationProperty("touch.deviceType", "touchScreen");
6922 prepareDisplay(DISPLAY_ORIENTATION_0);
6923 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6924 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006925 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006926
6927 // These calculations are based on the input device calibration documentation.
6928 int32_t rawX = 100;
6929 int32_t rawY = 200;
6930 int32_t rawTouchMajor = 140;
6931 int32_t rawTouchMinor = 120;
6932 int32_t rawToolMajor = 180;
6933 int32_t rawToolMinor = 160;
6934
6935 float x = toDisplayX(rawX);
6936 float y = toDisplayY(rawY);
6937 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6938 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6939 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6940 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6941 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6942
6943 processPosition(mapper, rawX, rawY);
6944 processTouchMajor(mapper, rawTouchMajor);
6945 processTouchMinor(mapper, rawTouchMinor);
6946 processToolMajor(mapper, rawToolMajor);
6947 processToolMinor(mapper, rawToolMinor);
6948 processMTSync(mapper);
6949 processSync(mapper);
6950
6951 NotifyMotionArgs args;
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6954 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6955}
6956
6957TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006958 addConfigurationProperty("touch.deviceType", "touchScreen");
6959 prepareDisplay(DISPLAY_ORIENTATION_0);
6960 prepareAxes(POSITION | TOUCH | TOOL);
6961 addConfigurationProperty("touch.size.calibration", "diameter");
6962 addConfigurationProperty("touch.size.scale", "10");
6963 addConfigurationProperty("touch.size.bias", "160");
6964 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006965 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006966
6967 // These calculations are based on the input device calibration documentation.
6968 // Note: We only provide a single common touch/tool value because the device is assumed
6969 // not to emit separate values for each pointer (isSummed = 1).
6970 int32_t rawX = 100;
6971 int32_t rawY = 200;
6972 int32_t rawX2 = 150;
6973 int32_t rawY2 = 250;
6974 int32_t rawTouchMajor = 5;
6975 int32_t rawToolMajor = 8;
6976
6977 float x = toDisplayX(rawX);
6978 float y = toDisplayY(rawY);
6979 float x2 = toDisplayX(rawX2);
6980 float y2 = toDisplayY(rawY2);
6981 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6982 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6983 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6984
6985 processPosition(mapper, rawX, rawY);
6986 processTouchMajor(mapper, rawTouchMajor);
6987 processToolMajor(mapper, rawToolMajor);
6988 processMTSync(mapper);
6989 processPosition(mapper, rawX2, rawY2);
6990 processTouchMajor(mapper, rawTouchMajor);
6991 processToolMajor(mapper, rawToolMajor);
6992 processMTSync(mapper);
6993 processSync(mapper);
6994
6995 NotifyMotionArgs args;
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6997 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6998
6999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7000 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7001 args.action);
7002 ASSERT_EQ(size_t(2), args.pointerCount);
7003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7004 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7006 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7007}
7008
7009TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007010 addConfigurationProperty("touch.deviceType", "touchScreen");
7011 prepareDisplay(DISPLAY_ORIENTATION_0);
7012 prepareAxes(POSITION | TOUCH | TOOL);
7013 addConfigurationProperty("touch.size.calibration", "area");
7014 addConfigurationProperty("touch.size.scale", "43");
7015 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007016 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007017
7018 // These calculations are based on the input device calibration documentation.
7019 int32_t rawX = 100;
7020 int32_t rawY = 200;
7021 int32_t rawTouchMajor = 5;
7022 int32_t rawToolMajor = 8;
7023
7024 float x = toDisplayX(rawX);
7025 float y = toDisplayY(rawY);
7026 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7027 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7028 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7029
7030 processPosition(mapper, rawX, rawY);
7031 processTouchMajor(mapper, rawTouchMajor);
7032 processToolMajor(mapper, rawToolMajor);
7033 processMTSync(mapper);
7034 processSync(mapper);
7035
7036 NotifyMotionArgs args;
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7039 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7040}
7041
7042TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007043 addConfigurationProperty("touch.deviceType", "touchScreen");
7044 prepareDisplay(DISPLAY_ORIENTATION_0);
7045 prepareAxes(POSITION | PRESSURE);
7046 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7047 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007048 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007049
Michael Wrightaa449c92017-12-13 21:21:43 +00007050 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007051 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007052 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7053 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7054 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7055
Michael Wrightd02c5b62014-02-10 15:10:22 -08007056 // These calculations are based on the input device calibration documentation.
7057 int32_t rawX = 100;
7058 int32_t rawY = 200;
7059 int32_t rawPressure = 60;
7060
7061 float x = toDisplayX(rawX);
7062 float y = toDisplayY(rawY);
7063 float pressure = float(rawPressure) * 0.01f;
7064
7065 processPosition(mapper, rawX, rawY);
7066 processPressure(mapper, rawPressure);
7067 processMTSync(mapper);
7068 processSync(mapper);
7069
7070 NotifyMotionArgs args;
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7073 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7074}
7075
7076TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007077 addConfigurationProperty("touch.deviceType", "touchScreen");
7078 prepareDisplay(DISPLAY_ORIENTATION_0);
7079 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007080 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081
7082 NotifyMotionArgs motionArgs;
7083 NotifyKeyArgs keyArgs;
7084
7085 processId(mapper, 1);
7086 processPosition(mapper, 100, 200);
7087 processSync(mapper);
7088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7089 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7090 ASSERT_EQ(0, motionArgs.buttonState);
7091
7092 // press BTN_LEFT, release BTN_LEFT
7093 processKey(mapper, BTN_LEFT, 1);
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7097 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7098
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7100 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7101 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7102
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103 processKey(mapper, BTN_LEFT, 0);
7104 processSync(mapper);
7105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007107 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007108
7109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007111 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112
7113 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7114 processKey(mapper, BTN_RIGHT, 1);
7115 processKey(mapper, BTN_MIDDLE, 1);
7116 processSync(mapper);
7117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7119 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7120 motionArgs.buttonState);
7121
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7123 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7124 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7125
7126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7128 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7129 motionArgs.buttonState);
7130
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131 processKey(mapper, BTN_RIGHT, 0);
7132 processSync(mapper);
7133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007136
7137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007139 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140
7141 processKey(mapper, BTN_MIDDLE, 0);
7142 processSync(mapper);
7143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007144 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007146
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007149 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150
7151 // press BTN_BACK, release BTN_BACK
7152 processKey(mapper, BTN_BACK, 1);
7153 processSync(mapper);
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7155 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7156 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007157
Michael Wrightd02c5b62014-02-10 15:10:22 -08007158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007160 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7161
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7163 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7164 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007165
7166 processKey(mapper, BTN_BACK, 0);
7167 processSync(mapper);
7168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007169 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007171
7172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007174 ASSERT_EQ(0, motionArgs.buttonState);
7175
Michael Wrightd02c5b62014-02-10 15:10:22 -08007176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7177 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7178 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7179
7180 // press BTN_SIDE, release BTN_SIDE
7181 processKey(mapper, BTN_SIDE, 1);
7182 processSync(mapper);
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7184 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7185 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007186
Michael Wrightd02c5b62014-02-10 15:10:22 -08007187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007189 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7190
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7192 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7193 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194
7195 processKey(mapper, BTN_SIDE, 0);
7196 processSync(mapper);
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007199 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007200
7201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007203 ASSERT_EQ(0, motionArgs.buttonState);
7204
Michael Wrightd02c5b62014-02-10 15:10:22 -08007205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7206 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7207 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7208
7209 // press BTN_FORWARD, release BTN_FORWARD
7210 processKey(mapper, BTN_FORWARD, 1);
7211 processSync(mapper);
7212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7213 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7214 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007215
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007218 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7219
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7222 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007223
7224 processKey(mapper, BTN_FORWARD, 0);
7225 processSync(mapper);
7226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007227 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007228 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007229
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007232 ASSERT_EQ(0, motionArgs.buttonState);
7233
Michael Wrightd02c5b62014-02-10 15:10:22 -08007234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7235 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7236 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7237
7238 // press BTN_EXTRA, release BTN_EXTRA
7239 processKey(mapper, BTN_EXTRA, 1);
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7242 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7243 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007244
Michael Wrightd02c5b62014-02-10 15:10:22 -08007245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007247 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7248
7249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7251 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007252
7253 processKey(mapper, BTN_EXTRA, 0);
7254 processSync(mapper);
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007257 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007258
7259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007261 ASSERT_EQ(0, motionArgs.buttonState);
7262
Michael Wrightd02c5b62014-02-10 15:10:22 -08007263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7264 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7265 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7266
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7268
Michael Wrightd02c5b62014-02-10 15:10:22 -08007269 // press BTN_STYLUS, release BTN_STYLUS
7270 processKey(mapper, BTN_STYLUS, 1);
7271 processSync(mapper);
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007274 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7275
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7278 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007279
7280 processKey(mapper, BTN_STYLUS, 0);
7281 processSync(mapper);
7282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007283 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007284 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007285
7286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007288 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007289
7290 // press BTN_STYLUS2, release BTN_STYLUS2
7291 processKey(mapper, BTN_STYLUS2, 1);
7292 processSync(mapper);
7293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007295 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7296
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7299 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007300
7301 processKey(mapper, BTN_STYLUS2, 0);
7302 processSync(mapper);
7303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007305 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007306
7307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007309 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007310
7311 // release touch
7312 processId(mapper, -1);
7313 processSync(mapper);
7314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7315 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7316 ASSERT_EQ(0, motionArgs.buttonState);
7317}
7318
7319TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007320 addConfigurationProperty("touch.deviceType", "touchScreen");
7321 prepareDisplay(DISPLAY_ORIENTATION_0);
7322 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007323 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007324
7325 NotifyMotionArgs motionArgs;
7326
7327 // default tool type is finger
7328 processId(mapper, 1);
7329 processPosition(mapper, 100, 200);
7330 processSync(mapper);
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7332 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7334
7335 // eraser
7336 processKey(mapper, BTN_TOOL_RUBBER, 1);
7337 processSync(mapper);
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7341
7342 // stylus
7343 processKey(mapper, BTN_TOOL_RUBBER, 0);
7344 processKey(mapper, BTN_TOOL_PEN, 1);
7345 processSync(mapper);
7346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7349
7350 // brush
7351 processKey(mapper, BTN_TOOL_PEN, 0);
7352 processKey(mapper, BTN_TOOL_BRUSH, 1);
7353 processSync(mapper);
7354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7357
7358 // pencil
7359 processKey(mapper, BTN_TOOL_BRUSH, 0);
7360 processKey(mapper, BTN_TOOL_PENCIL, 1);
7361 processSync(mapper);
7362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7365
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007366 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007367 processKey(mapper, BTN_TOOL_PENCIL, 0);
7368 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7369 processSync(mapper);
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7373
7374 // mouse
7375 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7376 processKey(mapper, BTN_TOOL_MOUSE, 1);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7381
7382 // lens
7383 processKey(mapper, BTN_TOOL_MOUSE, 0);
7384 processKey(mapper, BTN_TOOL_LENS, 1);
7385 processSync(mapper);
7386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7389
7390 // double-tap
7391 processKey(mapper, BTN_TOOL_LENS, 0);
7392 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7393 processSync(mapper);
7394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7397
7398 // triple-tap
7399 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7400 processKey(mapper, BTN_TOOL_TRIPLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
7405
7406 // quad-tap
7407 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7408 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7409 processSync(mapper);
7410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7413
7414 // finger
7415 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7416 processKey(mapper, BTN_TOOL_FINGER, 1);
7417 processSync(mapper);
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7421
7422 // stylus trumps finger
7423 processKey(mapper, BTN_TOOL_PEN, 1);
7424 processSync(mapper);
7425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7428
7429 // eraser trumps stylus
7430 processKey(mapper, BTN_TOOL_RUBBER, 1);
7431 processSync(mapper);
7432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7435
7436 // mouse trumps eraser
7437 processKey(mapper, BTN_TOOL_MOUSE, 1);
7438 processSync(mapper);
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7442
7443 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7444 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7445 processSync(mapper);
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7449
7450 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7451 processToolType(mapper, MT_TOOL_PEN);
7452 processSync(mapper);
7453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7456
7457 // back to default tool type
7458 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7459 processKey(mapper, BTN_TOOL_MOUSE, 0);
7460 processKey(mapper, BTN_TOOL_RUBBER, 0);
7461 processKey(mapper, BTN_TOOL_PEN, 0);
7462 processKey(mapper, BTN_TOOL_FINGER, 0);
7463 processSync(mapper);
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7467}
7468
7469TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007470 addConfigurationProperty("touch.deviceType", "touchScreen");
7471 prepareDisplay(DISPLAY_ORIENTATION_0);
7472 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007473 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007474 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007475
7476 NotifyMotionArgs motionArgs;
7477
7478 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7479 processId(mapper, 1);
7480 processPosition(mapper, 100, 200);
7481 processSync(mapper);
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7483 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7485 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7486
7487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7488 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7490 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7491
7492 // move a little
7493 processPosition(mapper, 150, 250);
7494 processSync(mapper);
7495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7496 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7498 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7499
7500 // down when BTN_TOUCH is pressed, pressure defaults to 1
7501 processKey(mapper, BTN_TOUCH, 1);
7502 processSync(mapper);
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7504 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7506 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7507
7508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7511 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7512
7513 // up when BTN_TOUCH is released, hover restored
7514 processKey(mapper, BTN_TOUCH, 0);
7515 processSync(mapper);
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7517 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7519 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7520
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7522 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7524 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7525
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7529 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7530
7531 // exit hover when pointer goes away
7532 processId(mapper, -1);
7533 processSync(mapper);
7534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7535 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7537 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7538}
7539
7540TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541 addConfigurationProperty("touch.deviceType", "touchScreen");
7542 prepareDisplay(DISPLAY_ORIENTATION_0);
7543 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007544 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007545
7546 NotifyMotionArgs motionArgs;
7547
7548 // initially hovering because pressure is 0
7549 processId(mapper, 1);
7550 processPosition(mapper, 100, 200);
7551 processPressure(mapper, 0);
7552 processSync(mapper);
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7554 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7556 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7557
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7561 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7562
7563 // move a little
7564 processPosition(mapper, 150, 250);
7565 processSync(mapper);
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7569 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7570
7571 // down when pressure becomes non-zero
7572 processPressure(mapper, RAW_PRESSURE_MAX);
7573 processSync(mapper);
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7575 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7577 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7578
7579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7580 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7582 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7583
7584 // up when pressure becomes 0, hover restored
7585 processPressure(mapper, 0);
7586 processSync(mapper);
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7588 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7590 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7591
7592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7593 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7595 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7596
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7600 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7601
7602 // exit hover when pointer goes away
7603 processId(mapper, -1);
7604 processSync(mapper);
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7606 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7608 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7609}
7610
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007611/**
7612 * Set the input device port <--> display port associations, and check that the
7613 * events are routed to the display that matches the display port.
7614 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7615 */
7616TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007617 const std::string usb2 = "USB2";
7618 const uint8_t hdmi1 = 0;
7619 const uint8_t hdmi2 = 1;
7620 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007621 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007622
7623 addConfigurationProperty("touch.deviceType", "touchScreen");
7624 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007625 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007626
7627 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7628 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7629
7630 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7631 // for this input device is specified, and the matching viewport is not present,
7632 // the input device should be disabled (at the mapper level).
7633
7634 // Add viewport for display 2 on hdmi2
7635 prepareSecondaryDisplay(type, hdmi2);
7636 // Send a touch event
7637 processPosition(mapper, 100, 100);
7638 processSync(mapper);
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7640
7641 // Add viewport for display 1 on hdmi1
7642 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7643 // Send a touch event again
7644 processPosition(mapper, 100, 100);
7645 processSync(mapper);
7646
7647 NotifyMotionArgs args;
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7649 ASSERT_EQ(DISPLAY_ID, args.displayId);
7650}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007652TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007653 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007654 std::shared_ptr<FakePointerController> fakePointerController =
7655 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007656 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007657 fakePointerController->setPosition(100, 200);
7658 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00007659 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007660
Garfield Tan888a6a42020-01-09 11:39:16 -08007661 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007662 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007663
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007664 prepareDisplay(DISPLAY_ORIENTATION_0);
7665 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007666 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007667
7668 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007669 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007670
7671 NotifyMotionArgs motionArgs;
7672 processPosition(mapper, 100, 100);
7673 processSync(mapper);
7674
7675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7676 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7677 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7678}
7679
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007680/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007681 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7682 */
7683TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7684 addConfigurationProperty("touch.deviceType", "touchScreen");
7685 prepareAxes(POSITION);
7686 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7687
7688 prepareDisplay(DISPLAY_ORIENTATION_0);
7689 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7690 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7691 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7692 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7693
7694 NotifyMotionArgs args;
7695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7696 ASSERT_EQ(26, args.readTime);
7697
7698 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7699 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7700 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7701
7702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7703 ASSERT_EQ(33, args.readTime);
7704}
7705
7706/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007707 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7708 * events should not be delivered to the listener.
7709 */
7710TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7711 addConfigurationProperty("touch.deviceType", "touchScreen");
7712 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7713 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7714 ViewportType::INTERNAL);
7715 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7716 prepareAxes(POSITION);
7717 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7718
7719 NotifyMotionArgs motionArgs;
7720 processPosition(mapper, 100, 100);
7721 processSync(mapper);
7722
7723 mFakeListener->assertNotifyMotionWasNotCalled();
7724}
7725
Garfield Tanc734e4f2021-01-15 20:01:39 -08007726TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7727 addConfigurationProperty("touch.deviceType", "touchScreen");
7728 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7729 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7730 ViewportType::INTERNAL);
7731 std::optional<DisplayViewport> optionalDisplayViewport =
7732 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7733 ASSERT_TRUE(optionalDisplayViewport.has_value());
7734 DisplayViewport displayViewport = *optionalDisplayViewport;
7735
7736 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7737 prepareAxes(POSITION);
7738 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7739
7740 // Finger down
7741 int32_t x = 100, y = 100;
7742 processPosition(mapper, x, y);
7743 processSync(mapper);
7744
7745 NotifyMotionArgs motionArgs;
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7747 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7748
7749 // Deactivate display viewport
7750 displayViewport.isActive = false;
7751 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7752 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7753
7754 // Finger move
7755 x += 10, y += 10;
7756 processPosition(mapper, x, y);
7757 processSync(mapper);
7758
7759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7760 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7761
7762 // Reactivate display viewport
7763 displayViewport.isActive = true;
7764 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7765 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7766
7767 // Finger move again
7768 x += 10, y += 10;
7769 processPosition(mapper, x, y);
7770 processSync(mapper);
7771
7772 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7773 // no pointer on the touch device.
7774 mFakeListener->assertNotifyMotionWasNotCalled();
7775}
7776
Arthur Hung7c645402019-01-25 17:45:42 +08007777TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7778 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007779 prepareAxes(POSITION | ID | SLOT);
7780 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007781 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007782
7783 // Create the second touch screen device, and enable multi fingers.
7784 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007785 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007786 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007787 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007788 std::shared_ptr<InputDevice> device2 =
7789 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7790 Flags<InputDeviceClass>(0));
7791
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007792 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7793 0 /*flat*/, 0 /*fuzz*/);
7794 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7795 0 /*flat*/, 0 /*fuzz*/);
7796 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7797 0 /*flat*/, 0 /*fuzz*/);
7798 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7799 0 /*flat*/, 0 /*fuzz*/);
7800 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7801 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7802 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007803
7804 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007805 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007806 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7807 device2->reset(ARBITRARY_TIME);
7808
7809 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007810 std::shared_ptr<FakePointerController> fakePointerController =
7811 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00007812 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08007813
7814 // Setup policy for associated displays and show touches.
7815 const uint8_t hdmi1 = 0;
7816 const uint8_t hdmi2 = 1;
7817 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7818 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7819 mFakePolicy->setShowTouches(true);
7820
7821 // Create displays.
7822 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007823 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007824
7825 // Default device will reconfigure above, need additional reconfiguration for another device.
7826 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007827 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007828
7829 // Two fingers down at default display.
7830 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7831 processPosition(mapper, x1, y1);
7832 processId(mapper, 1);
7833 processSlot(mapper, 1);
7834 processPosition(mapper, x2, y2);
7835 processId(mapper, 2);
7836 processSync(mapper);
7837
7838 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7839 fakePointerController->getSpots().find(DISPLAY_ID);
7840 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7841 ASSERT_EQ(size_t(2), iter->second.size());
7842
7843 // Two fingers down at second display.
7844 processPosition(mapper2, x1, y1);
7845 processId(mapper2, 1);
7846 processSlot(mapper2, 1);
7847 processPosition(mapper2, x2, y2);
7848 processId(mapper2, 2);
7849 processSync(mapper2);
7850
7851 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7852 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7853 ASSERT_EQ(size_t(2), iter->second.size());
7854}
7855
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007856TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007857 prepareAxes(POSITION);
7858 addConfigurationProperty("touch.deviceType", "touchScreen");
7859 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007860 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007861
7862 NotifyMotionArgs motionArgs;
7863 // Unrotated video frame
7864 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7865 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007866 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007867 processPosition(mapper, 100, 200);
7868 processSync(mapper);
7869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7870 ASSERT_EQ(frames, motionArgs.videoFrames);
7871
7872 // Subsequent touch events should not have any videoframes
7873 // This is implemented separately in FakeEventHub,
7874 // but that should match the behaviour of TouchVideoDevice.
7875 processPosition(mapper, 200, 200);
7876 processSync(mapper);
7877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7878 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7879}
7880
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007881TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007882 prepareAxes(POSITION);
7883 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007884 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007885 // Unrotated video frame
7886 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7887 NotifyMotionArgs motionArgs;
7888
7889 // Test all 4 orientations
7890 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007891 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7892 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7893 clearViewports();
7894 prepareDisplay(orientation);
7895 std::vector<TouchVideoFrame> frames{frame};
7896 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
7897 processPosition(mapper, 100, 200);
7898 processSync(mapper);
7899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7900 ASSERT_EQ(frames, motionArgs.videoFrames);
7901 }
7902}
7903
7904TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
7905 prepareAxes(POSITION);
7906 addConfigurationProperty("touch.deviceType", "touchScreen");
7907 // Since InputReader works in the un-rotated coordinate space, only devices that are not
7908 // orientation-aware are affected by display rotation.
7909 addConfigurationProperty("touch.orientationAware", "0");
7910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7911 // Unrotated video frame
7912 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7913 NotifyMotionArgs motionArgs;
7914
7915 // Test all 4 orientations
7916 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007917 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7918 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7919 clearViewports();
7920 prepareDisplay(orientation);
7921 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007922 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007923 processPosition(mapper, 100, 200);
7924 processSync(mapper);
7925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007926 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
7927 // compared to the display. This is so that when the window transform (which contains the
7928 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
7929 // window's coordinate space.
7930 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007931 ASSERT_EQ(frames, motionArgs.videoFrames);
7932 }
7933}
7934
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007935TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007936 prepareAxes(POSITION);
7937 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007938 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007939 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7940 // so mix these.
7941 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7942 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7943 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7944 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7945 NotifyMotionArgs motionArgs;
7946
7947 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007948 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007949 processPosition(mapper, 100, 200);
7950 processSync(mapper);
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007952 ASSERT_EQ(frames, motionArgs.videoFrames);
7953}
7954
7955TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
7956 prepareAxes(POSITION);
7957 addConfigurationProperty("touch.deviceType", "touchScreen");
7958 // Since InputReader works in the un-rotated coordinate space, only devices that are not
7959 // orientation-aware are affected by display rotation.
7960 addConfigurationProperty("touch.orientationAware", "0");
7961 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7962 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7963 // so mix these.
7964 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7965 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7966 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7967 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7968 NotifyMotionArgs motionArgs;
7969
7970 prepareDisplay(DISPLAY_ORIENTATION_90);
7971 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
7972 processPosition(mapper, 100, 200);
7973 processSync(mapper);
7974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7975 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
7976 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
7977 // compared to the display. This is so that when the window transform (which contains the
7978 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
7979 // window's coordinate space.
7980 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
7981 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007982 ASSERT_EQ(frames, motionArgs.videoFrames);
7983}
7984
Arthur Hung9da14732019-09-02 16:16:58 +08007985/**
7986 * If we had defined port associations, but the viewport is not ready, the touch device would be
7987 * expected to be disabled, and it should be enabled after the viewport has found.
7988 */
7989TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007990 constexpr uint8_t hdmi2 = 1;
7991 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007992 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007993
7994 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7995
7996 addConfigurationProperty("touch.deviceType", "touchScreen");
7997 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007998 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007999
8000 ASSERT_EQ(mDevice->isEnabled(), false);
8001
8002 // Add display on hdmi2, the device should be enabled and can receive touch event.
8003 prepareSecondaryDisplay(type, hdmi2);
8004 ASSERT_EQ(mDevice->isEnabled(), true);
8005
8006 // Send a touch event.
8007 processPosition(mapper, 100, 100);
8008 processSync(mapper);
8009
8010 NotifyMotionArgs args;
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8012 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8013}
8014
Arthur Hung421eb1c2020-01-16 00:09:42 +08008015TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008016 addConfigurationProperty("touch.deviceType", "touchScreen");
8017 prepareDisplay(DISPLAY_ORIENTATION_0);
8018 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008019 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008020
8021 NotifyMotionArgs motionArgs;
8022
8023 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8024 // finger down
8025 processId(mapper, 1);
8026 processPosition(mapper, x1, y1);
8027 processSync(mapper);
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8031
8032 // finger move
8033 processId(mapper, 1);
8034 processPosition(mapper, x2, y2);
8035 processSync(mapper);
8036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8038 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8039
8040 // finger up.
8041 processId(mapper, -1);
8042 processSync(mapper);
8043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8044 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8046
8047 // new finger down
8048 processId(mapper, 1);
8049 processPosition(mapper, x3, y3);
8050 processSync(mapper);
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8052 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8054}
8055
8056/**
arthurhungcc7f9802020-04-30 17:55:40 +08008057 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8058 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008059 */
arthurhungcc7f9802020-04-30 17:55:40 +08008060TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008061 addConfigurationProperty("touch.deviceType", "touchScreen");
8062 prepareDisplay(DISPLAY_ORIENTATION_0);
8063 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008064 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008065
8066 NotifyMotionArgs motionArgs;
8067
8068 // default tool type is finger
8069 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008070 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008071 processPosition(mapper, x1, y1);
8072 processSync(mapper);
8073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8074 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8076
8077 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8078 processToolType(mapper, MT_TOOL_PALM);
8079 processSync(mapper);
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8081 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8082
8083 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008084 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008085 processPosition(mapper, x2, y2);
8086 processSync(mapper);
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8088
8089 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008090 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008091 processSync(mapper);
8092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8093
8094 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008095 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008096 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008097 processPosition(mapper, x3, y3);
8098 processSync(mapper);
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8100 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8102}
8103
arthurhungbf89a482020-04-17 17:37:55 +08008104/**
arthurhungcc7f9802020-04-30 17:55:40 +08008105 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8106 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008107 */
arthurhungcc7f9802020-04-30 17:55:40 +08008108TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008109 addConfigurationProperty("touch.deviceType", "touchScreen");
8110 prepareDisplay(DISPLAY_ORIENTATION_0);
8111 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8112 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8113
8114 NotifyMotionArgs motionArgs;
8115
8116 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008117 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8118 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008119 processPosition(mapper, x1, y1);
8120 processSync(mapper);
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8122 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8124
8125 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008126 processSlot(mapper, SECOND_SLOT);
8127 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008128 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008129 processSync(mapper);
8130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8131 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8132 motionArgs.action);
8133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8134
8135 // If the tool type of the first finger changes to MT_TOOL_PALM,
8136 // we expect to receive ACTION_POINTER_UP with cancel flag.
8137 processSlot(mapper, FIRST_SLOT);
8138 processId(mapper, FIRST_TRACKING_ID);
8139 processToolType(mapper, MT_TOOL_PALM);
8140 processSync(mapper);
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8142 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8143 motionArgs.action);
8144 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8145
8146 // The following MOVE events of second finger should be processed.
8147 processSlot(mapper, SECOND_SLOT);
8148 processId(mapper, SECOND_TRACKING_ID);
8149 processPosition(mapper, x2 + 1, y2 + 1);
8150 processSync(mapper);
8151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8153 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8154
8155 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8156 // it. Second finger receive move.
8157 processSlot(mapper, FIRST_SLOT);
8158 processId(mapper, INVALID_TRACKING_ID);
8159 processSync(mapper);
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8162 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8163
8164 // Second finger keeps moving.
8165 processSlot(mapper, SECOND_SLOT);
8166 processId(mapper, SECOND_TRACKING_ID);
8167 processPosition(mapper, x2 + 2, y2 + 2);
8168 processSync(mapper);
8169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8171 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8172
8173 // Second finger up.
8174 processId(mapper, INVALID_TRACKING_ID);
8175 processSync(mapper);
8176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8177 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8178 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8179}
8180
8181/**
8182 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8183 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8184 */
8185TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8186 addConfigurationProperty("touch.deviceType", "touchScreen");
8187 prepareDisplay(DISPLAY_ORIENTATION_0);
8188 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8189 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8190
8191 NotifyMotionArgs motionArgs;
8192
8193 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8194 // First finger down.
8195 processId(mapper, FIRST_TRACKING_ID);
8196 processPosition(mapper, x1, y1);
8197 processSync(mapper);
8198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8199 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8201
8202 // Second finger down.
8203 processSlot(mapper, SECOND_SLOT);
8204 processId(mapper, SECOND_TRACKING_ID);
8205 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008206 processSync(mapper);
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8208 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8209 motionArgs.action);
8210 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8211
arthurhungcc7f9802020-04-30 17:55:40 +08008212 // If the tool type of the first finger changes to MT_TOOL_PALM,
8213 // we expect to receive ACTION_POINTER_UP with cancel flag.
8214 processSlot(mapper, FIRST_SLOT);
8215 processId(mapper, FIRST_TRACKING_ID);
8216 processToolType(mapper, MT_TOOL_PALM);
8217 processSync(mapper);
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8219 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8220 motionArgs.action);
8221 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8222
8223 // Second finger keeps moving.
8224 processSlot(mapper, SECOND_SLOT);
8225 processId(mapper, SECOND_TRACKING_ID);
8226 processPosition(mapper, x2 + 1, y2 + 1);
8227 processSync(mapper);
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8230
8231 // second finger becomes palm, receive cancel due to only 1 finger is active.
8232 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008233 processToolType(mapper, MT_TOOL_PALM);
8234 processSync(mapper);
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8236 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8237
arthurhungcc7f9802020-04-30 17:55:40 +08008238 // third finger down.
8239 processSlot(mapper, THIRD_SLOT);
8240 processId(mapper, THIRD_TRACKING_ID);
8241 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008242 processPosition(mapper, x3, y3);
8243 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8245 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8246 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008247 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8248
8249 // third finger move
8250 processId(mapper, THIRD_TRACKING_ID);
8251 processPosition(mapper, x3 + 1, y3 + 1);
8252 processSync(mapper);
8253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8255
8256 // first finger up, third finger receive move.
8257 processSlot(mapper, FIRST_SLOT);
8258 processId(mapper, INVALID_TRACKING_ID);
8259 processSync(mapper);
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8262 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8263
8264 // second finger up, third finger receive move.
8265 processSlot(mapper, SECOND_SLOT);
8266 processId(mapper, INVALID_TRACKING_ID);
8267 processSync(mapper);
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8270 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8271
8272 // third finger up.
8273 processSlot(mapper, THIRD_SLOT);
8274 processId(mapper, INVALID_TRACKING_ID);
8275 processSync(mapper);
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8277 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8278 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8279}
8280
8281/**
8282 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8283 * and the active finger could still be allowed to receive the events
8284 */
8285TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8286 addConfigurationProperty("touch.deviceType", "touchScreen");
8287 prepareDisplay(DISPLAY_ORIENTATION_0);
8288 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8289 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8290
8291 NotifyMotionArgs motionArgs;
8292
8293 // default tool type is finger
8294 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8295 processId(mapper, FIRST_TRACKING_ID);
8296 processPosition(mapper, x1, y1);
8297 processSync(mapper);
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8301
8302 // Second finger down.
8303 processSlot(mapper, SECOND_SLOT);
8304 processId(mapper, SECOND_TRACKING_ID);
8305 processPosition(mapper, x2, y2);
8306 processSync(mapper);
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8308 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8309 motionArgs.action);
8310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8311
8312 // If the tool type of the second finger changes to MT_TOOL_PALM,
8313 // we expect to receive ACTION_POINTER_UP with cancel flag.
8314 processId(mapper, SECOND_TRACKING_ID);
8315 processToolType(mapper, MT_TOOL_PALM);
8316 processSync(mapper);
8317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8318 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8319 motionArgs.action);
8320 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8321
8322 // The following MOVE event should be processed.
8323 processSlot(mapper, FIRST_SLOT);
8324 processId(mapper, FIRST_TRACKING_ID);
8325 processPosition(mapper, x1 + 1, y1 + 1);
8326 processSync(mapper);
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8329 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8330
8331 // second finger up.
8332 processSlot(mapper, SECOND_SLOT);
8333 processId(mapper, INVALID_TRACKING_ID);
8334 processSync(mapper);
8335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8337
8338 // first finger keep moving
8339 processSlot(mapper, FIRST_SLOT);
8340 processId(mapper, FIRST_TRACKING_ID);
8341 processPosition(mapper, x1 + 2, y1 + 2);
8342 processSync(mapper);
8343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8345
8346 // first finger up.
8347 processId(mapper, INVALID_TRACKING_ID);
8348 processSync(mapper);
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8350 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8351 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008352}
8353
Arthur Hung9ad18942021-06-19 02:04:46 +00008354/**
8355 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8356 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8357 * cause slot be valid again.
8358 */
8359TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8360 addConfigurationProperty("touch.deviceType", "touchScreen");
8361 prepareDisplay(DISPLAY_ORIENTATION_0);
8362 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8363 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8364
8365 NotifyMotionArgs motionArgs;
8366
8367 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8368 // First finger down.
8369 processId(mapper, FIRST_TRACKING_ID);
8370 processPosition(mapper, x1, y1);
8371 processPressure(mapper, RAW_PRESSURE_MAX);
8372 processSync(mapper);
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8374 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8375 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8376
8377 // First finger move.
8378 processId(mapper, FIRST_TRACKING_ID);
8379 processPosition(mapper, x1 + 1, y1 + 1);
8380 processPressure(mapper, RAW_PRESSURE_MAX);
8381 processSync(mapper);
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8384 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8385
8386 // Second finger down.
8387 processSlot(mapper, SECOND_SLOT);
8388 processId(mapper, SECOND_TRACKING_ID);
8389 processPosition(mapper, x2, y2);
8390 processPressure(mapper, RAW_PRESSURE_MAX);
8391 processSync(mapper);
8392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8393 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8394 motionArgs.action);
8395 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8396
8397 // second finger up with some unexpected data.
8398 processSlot(mapper, SECOND_SLOT);
8399 processId(mapper, INVALID_TRACKING_ID);
8400 processPosition(mapper, x2, y2);
8401 processSync(mapper);
8402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8403 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8404 motionArgs.action);
8405 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8406
8407 // first finger up with some unexpected data.
8408 processSlot(mapper, FIRST_SLOT);
8409 processId(mapper, INVALID_TRACKING_ID);
8410 processPosition(mapper, x2, y2);
8411 processPressure(mapper, RAW_PRESSURE_MAX);
8412 processSync(mapper);
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8414 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8415 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8416}
8417
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008418// --- MultiTouchInputMapperTest_ExternalDevice ---
8419
8420class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8421protected:
Chris Yea52ade12020-08-27 16:49:20 -07008422 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008423};
8424
8425/**
8426 * Expect fallback to internal viewport if device is external and external viewport is not present.
8427 */
8428TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8429 prepareAxes(POSITION);
8430 addConfigurationProperty("touch.deviceType", "touchScreen");
8431 prepareDisplay(DISPLAY_ORIENTATION_0);
8432 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8433
8434 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8435
8436 NotifyMotionArgs motionArgs;
8437
8438 // Expect the event to be sent to the internal viewport,
8439 // because an external viewport is not present.
8440 processPosition(mapper, 100, 100);
8441 processSync(mapper);
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8443 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8444
8445 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008446 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008447 processPosition(mapper, 100, 100);
8448 processSync(mapper);
8449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8450 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8451}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008452
8453/**
8454 * Test touch should not work if outside of surface.
8455 */
8456class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8457protected:
8458 void halfDisplayToCenterHorizontal(int32_t orientation) {
8459 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008460 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008461
8462 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8463 internalViewport->orientation = orientation;
8464 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8465 internalViewport->logicalLeft = 0;
8466 internalViewport->logicalTop = 0;
8467 internalViewport->logicalRight = DISPLAY_HEIGHT;
8468 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8469
8470 internalViewport->physicalLeft = 0;
8471 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8472 internalViewport->physicalRight = DISPLAY_HEIGHT;
8473 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8474
8475 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8476 internalViewport->deviceHeight = DISPLAY_WIDTH;
8477 } else {
8478 internalViewport->logicalLeft = 0;
8479 internalViewport->logicalTop = 0;
8480 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8481 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8482
8483 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8484 internalViewport->physicalTop = 0;
8485 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8486 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8487
8488 internalViewport->deviceWidth = DISPLAY_WIDTH;
8489 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8490 }
8491
8492 mFakePolicy->updateViewport(internalViewport.value());
8493 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8494 }
8495
arthurhung5d547942020-12-14 17:04:45 +08008496 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8497 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008498 int32_t yExpected) {
8499 // touch on outside area should not work.
8500 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8501 processSync(mapper);
8502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8503
8504 // touch on inside area should receive the event.
8505 NotifyMotionArgs args;
8506 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8507 processSync(mapper);
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8509 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8510 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8511
8512 // Reset.
8513 mapper.reset(ARBITRARY_TIME);
8514 }
8515};
8516
arthurhung5d547942020-12-14 17:04:45 +08008517TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008518 addConfigurationProperty("touch.deviceType", "touchScreen");
8519 prepareDisplay(DISPLAY_ORIENTATION_0);
8520 prepareAxes(POSITION);
8521 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8522
8523 // Touch on center of normal display should work.
8524 const int32_t x = DISPLAY_WIDTH / 4;
8525 const int32_t y = DISPLAY_HEIGHT / 2;
8526 processPosition(mapper, toRawX(x), toRawY(y));
8527 processSync(mapper);
8528 NotifyMotionArgs args;
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8531 0.0f, 0.0f, 0.0f, 0.0f));
8532 // Reset.
8533 mapper.reset(ARBITRARY_TIME);
8534
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008535 // Let physical display be different to device, and make surface and physical could be 1:1 in
8536 // all four orientations.
8537 for (int orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
8538 DISPLAY_ORIENTATION_270}) {
8539 halfDisplayToCenterHorizontal(orientation);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008540
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008541 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8542 const int32_t yExpected = y;
8543 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8544 }
Arthur Hung4197f6b2020-03-16 15:39:59 +08008545}
8546
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008547TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90_NotOrientationAware) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008548 addConfigurationProperty("touch.deviceType", "touchScreen");
8549 prepareDisplay(DISPLAY_ORIENTATION_0);
8550 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008551 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8552 // orientation-aware are affected by display rotation.
8553 addConfigurationProperty("touch.orientationAware", "0");
Arthur Hung4197f6b2020-03-16 15:39:59 +08008554 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8555
8556 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8557 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8558
8559 const int32_t x = DISPLAY_WIDTH / 4;
8560 const int32_t y = DISPLAY_HEIGHT / 2;
8561
Arthur Hung4197f6b2020-03-16 15:39:59 +08008562 // expect x/y = swap x/y then reverse x.
8563 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8564 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8565 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8566}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008567
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008568TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270_NotOrientationAware) {
arthurhunga36b28e2020-12-29 20:28:15 +08008569 addConfigurationProperty("touch.deviceType", "touchScreen");
8570 prepareDisplay(DISPLAY_ORIENTATION_0);
8571 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008572 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8573 // orientation-aware are affected by display rotation.
8574 addConfigurationProperty("touch.orientationAware", "0");
8575 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8576
8577 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8578 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8579
8580 const int32_t x = DISPLAY_WIDTH / 4;
8581 const int32_t y = DISPLAY_HEIGHT / 2;
8582
8583 // expect x/y = swap x/y then reverse y.
8584 const int32_t xExpected = y;
8585 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8586 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8587}
8588
8589TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner_NotOrientationAware) {
8590 addConfigurationProperty("touch.deviceType", "touchScreen");
8591 prepareDisplay(DISPLAY_ORIENTATION_0);
8592 prepareAxes(POSITION);
8593 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8594 // orientation-aware are affected by display rotation.
8595 addConfigurationProperty("touch.orientationAware", "0");
arthurhunga36b28e2020-12-29 20:28:15 +08008596 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8597
8598 const int32_t x = 0;
8599 const int32_t y = 0;
8600
8601 const int32_t xExpected = x;
8602 const int32_t yExpected = y;
8603 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8604
8605 clearViewports();
8606 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008607 // expect x/y = swap x/y then reverse x.
8608 const int32_t xExpected90 = DISPLAY_HEIGHT - 1;
8609 const int32_t yExpected90 = x;
arthurhunga36b28e2020-12-29 20:28:15 +08008610 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8611
8612 clearViewports();
8613 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008614 // expect x/y = swap x/y then reverse y.
8615 const int32_t xExpected270 = y;
8616 const int32_t yExpected270 = DISPLAY_WIDTH - 1;
arthurhunga36b28e2020-12-29 20:28:15 +08008617 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8618}
8619
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008620TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8621 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8622 std::shared_ptr<FakePointerController> fakePointerController =
8623 std::make_shared<FakePointerController>();
8624 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8625 fakePointerController->setPosition(0, 0);
8626 fakePointerController->setButtonState(0);
8627
8628 // prepare device and capture
8629 prepareDisplay(DISPLAY_ORIENTATION_0);
8630 prepareAxes(POSITION | ID | SLOT);
8631 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8632 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8633 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008634 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008635 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8636
8637 // captured touchpad should be a touchpad source
8638 NotifyDeviceResetArgs resetArgs;
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8640 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8641
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008642 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008643
8644 const InputDeviceInfo::MotionRange* relRangeX =
8645 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8646 ASSERT_NE(relRangeX, nullptr);
8647 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8648 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8649 const InputDeviceInfo::MotionRange* relRangeY =
8650 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8651 ASSERT_NE(relRangeY, nullptr);
8652 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8653 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8654
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008655 // run captured pointer tests - note that this is unscaled, so input listener events should be
8656 // identical to what the hardware sends (accounting for any
8657 // calibration).
8658 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008659 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008660 processId(mapper, 1);
8661 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8662 processKey(mapper, BTN_TOUCH, 1);
8663 processSync(mapper);
8664
8665 // expect coord[0] to contain initial location of touch 0
8666 NotifyMotionArgs args;
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8669 ASSERT_EQ(1U, args.pointerCount);
8670 ASSERT_EQ(0, args.pointerProperties[0].id);
8671 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8672 ASSERT_NO_FATAL_FAILURE(
8673 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8674
8675 // FINGER 1 DOWN
8676 processSlot(mapper, 1);
8677 processId(mapper, 2);
8678 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8679 processSync(mapper);
8680
8681 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008683 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8684 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008685 ASSERT_EQ(2U, args.pointerCount);
8686 ASSERT_EQ(0, args.pointerProperties[0].id);
8687 ASSERT_EQ(1, args.pointerProperties[1].id);
8688 ASSERT_NO_FATAL_FAILURE(
8689 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8690 ASSERT_NO_FATAL_FAILURE(
8691 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8692
8693 // FINGER 1 MOVE
8694 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8695 processSync(mapper);
8696
8697 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8698 // from move
8699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8701 ASSERT_NO_FATAL_FAILURE(
8702 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8703 ASSERT_NO_FATAL_FAILURE(
8704 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8705
8706 // FINGER 0 MOVE
8707 processSlot(mapper, 0);
8708 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8709 processSync(mapper);
8710
8711 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8714 ASSERT_NO_FATAL_FAILURE(
8715 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8716 ASSERT_NO_FATAL_FAILURE(
8717 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8718
8719 // BUTTON DOWN
8720 processKey(mapper, BTN_LEFT, 1);
8721 processSync(mapper);
8722
8723 // touchinputmapper design sends a move before button press
8724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8727 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8728
8729 // BUTTON UP
8730 processKey(mapper, BTN_LEFT, 0);
8731 processSync(mapper);
8732
8733 // touchinputmapper design sends a move after button release
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8735 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8738
8739 // FINGER 0 UP
8740 processId(mapper, -1);
8741 processSync(mapper);
8742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8743 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8744
8745 // FINGER 1 MOVE
8746 processSlot(mapper, 1);
8747 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8748 processSync(mapper);
8749
8750 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8753 ASSERT_EQ(1U, args.pointerCount);
8754 ASSERT_EQ(1, args.pointerProperties[0].id);
8755 ASSERT_NO_FATAL_FAILURE(
8756 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8757
8758 // FINGER 1 UP
8759 processId(mapper, -1);
8760 processKey(mapper, BTN_TOUCH, 0);
8761 processSync(mapper);
8762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8763 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8764
8765 // non captured touchpad should be a mouse source
8766 mFakePolicy->setPointerCapture(false);
8767 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8769 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8770}
8771
8772TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8773 std::shared_ptr<FakePointerController> fakePointerController =
8774 std::make_shared<FakePointerController>();
8775 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8776 fakePointerController->setPosition(0, 0);
8777 fakePointerController->setButtonState(0);
8778
8779 // prepare device and capture
8780 prepareDisplay(DISPLAY_ORIENTATION_0);
8781 prepareAxes(POSITION | ID | SLOT);
8782 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8783 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008784 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008785 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8786 // run uncaptured pointer tests - pushes out generic events
8787 // FINGER 0 DOWN
8788 processId(mapper, 3);
8789 processPosition(mapper, 100, 100);
8790 processKey(mapper, BTN_TOUCH, 1);
8791 processSync(mapper);
8792
8793 // start at (100,100), cursor should be at (0,0) * scale
8794 NotifyMotionArgs args;
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8797 ASSERT_NO_FATAL_FAILURE(
8798 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8799
8800 // FINGER 0 MOVE
8801 processPosition(mapper, 200, 200);
8802 processSync(mapper);
8803
8804 // compute scaling to help with touch position checking
8805 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8806 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8807 float scale =
8808 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8809
8810 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8812 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8814 0, 0, 0, 0, 0, 0, 0));
8815}
8816
8817TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8818 std::shared_ptr<FakePointerController> fakePointerController =
8819 std::make_shared<FakePointerController>();
8820
8821 prepareDisplay(DISPLAY_ORIENTATION_0);
8822 prepareAxes(POSITION | ID | SLOT);
8823 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008824 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008825 mFakePolicy->setPointerCapture(false);
8826 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8827
8828 // uncaptured touchpad should be a pointer device
8829 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8830
8831 // captured touchpad should be a touchpad device
8832 mFakePolicy->setPointerCapture(true);
8833 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8834 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8835}
8836
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008837// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008838
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008839class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008840protected:
8841 static const char* DEVICE_NAME;
8842 static const char* DEVICE_LOCATION;
8843 static const int32_t DEVICE_ID;
8844 static const int32_t DEVICE_GENERATION;
8845 static const int32_t DEVICE_CONTROLLER_NUMBER;
8846 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8847 static const int32_t EVENTHUB_ID;
8848
8849 std::shared_ptr<FakeEventHub> mFakeEventHub;
8850 sp<FakeInputReaderPolicy> mFakePolicy;
8851 sp<TestInputListener> mFakeListener;
8852 std::unique_ptr<InstrumentedInputReader> mReader;
8853 std::shared_ptr<InputDevice> mDevice;
8854
8855 virtual void SetUp(Flags<InputDeviceClass> classes) {
8856 mFakeEventHub = std::make_unique<FakeEventHub>();
8857 mFakePolicy = new FakeInputReaderPolicy();
8858 mFakeListener = new TestInputListener();
8859 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8860 mFakeListener);
8861 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8862 }
8863
8864 void SetUp() override { SetUp(DEVICE_CLASSES); }
8865
8866 void TearDown() override {
8867 mFakeListener.clear();
8868 mFakePolicy.clear();
8869 }
8870
8871 void configureDevice(uint32_t changes) {
8872 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8873 mReader->requestRefreshConfiguration(changes);
8874 mReader->loopOnce();
8875 }
8876 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8877 }
8878
8879 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8880 const std::string& location, int32_t eventHubId,
8881 Flags<InputDeviceClass> classes) {
8882 InputDeviceIdentifier identifier;
8883 identifier.name = name;
8884 identifier.location = location;
8885 std::shared_ptr<InputDevice> device =
8886 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8887 identifier);
8888 mReader->pushNextDevice(device);
8889 mFakeEventHub->addDevice(eventHubId, name, classes);
8890 mReader->loopOnce();
8891 return device;
8892 }
8893
8894 template <class T, typename... Args>
8895 T& addControllerAndConfigure(Args... args) {
8896 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8897
8898 return controller;
8899 }
8900};
8901
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008902const char* PeripheralControllerTest::DEVICE_NAME = "device";
8903const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8904const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8905const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8906const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8907const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008908 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008909const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008910
8911// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008912class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008913protected:
8914 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008915 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008916 }
8917};
8918
8919TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008920 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008921
8922 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8923 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8924}
8925
8926TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008927 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008928
8929 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8930 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8931}
8932
8933// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008934class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008935protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008936 void SetUp() override {
8937 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8938 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008939};
8940
Chris Ye85758332021-05-16 23:05:17 -07008941TEST_F(LightControllerTest, MonoLight) {
8942 RawLightInfo infoMono = {.id = 1,
8943 .name = "Mono",
8944 .maxBrightness = 255,
8945 .flags = InputLightClass::BRIGHTNESS,
8946 .path = ""};
8947 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008948
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008949 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008950 InputDeviceInfo info;
8951 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008952 std::vector<InputDeviceLightInfo> lights = info.getLights();
8953 ASSERT_EQ(1U, lights.size());
8954 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008955
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008956 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
8957 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008958}
8959
8960TEST_F(LightControllerTest, RGBLight) {
8961 RawLightInfo infoRed = {.id = 1,
8962 .name = "red",
8963 .maxBrightness = 255,
8964 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8965 .path = ""};
8966 RawLightInfo infoGreen = {.id = 2,
8967 .name = "green",
8968 .maxBrightness = 255,
8969 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8970 .path = ""};
8971 RawLightInfo infoBlue = {.id = 3,
8972 .name = "blue",
8973 .maxBrightness = 255,
8974 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8975 .path = ""};
8976 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8977 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8978 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8979
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008980 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008981 InputDeviceInfo info;
8982 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008983 std::vector<InputDeviceLightInfo> lights = info.getLights();
8984 ASSERT_EQ(1U, lights.size());
8985 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008986
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008987 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8988 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008989}
8990
8991TEST_F(LightControllerTest, MultiColorRGBLight) {
8992 RawLightInfo infoColor = {.id = 1,
8993 .name = "red",
8994 .maxBrightness = 255,
8995 .flags = InputLightClass::BRIGHTNESS |
8996 InputLightClass::MULTI_INTENSITY |
8997 InputLightClass::MULTI_INDEX,
8998 .path = ""};
8999
9000 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
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::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009008
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009009 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9010 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009011}
9012
9013TEST_F(LightControllerTest, PlayerIdLight) {
9014 RawLightInfo info1 = {.id = 1,
9015 .name = "player1",
9016 .maxBrightness = 255,
9017 .flags = InputLightClass::BRIGHTNESS,
9018 .path = ""};
9019 RawLightInfo info2 = {.id = 2,
9020 .name = "player2",
9021 .maxBrightness = 255,
9022 .flags = InputLightClass::BRIGHTNESS,
9023 .path = ""};
9024 RawLightInfo info3 = {.id = 3,
9025 .name = "player3",
9026 .maxBrightness = 255,
9027 .flags = InputLightClass::BRIGHTNESS,
9028 .path = ""};
9029 RawLightInfo info4 = {.id = 4,
9030 .name = "player4",
9031 .maxBrightness = 255,
9032 .flags = InputLightClass::BRIGHTNESS,
9033 .path = ""};
9034 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9035 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9036 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9037 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9038
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009039 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009040 InputDeviceInfo info;
9041 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009042 std::vector<InputDeviceLightInfo> lights = info.getLights();
9043 ASSERT_EQ(1U, lights.size());
9044 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009045
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009046 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9047 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9048 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009049}
9050
Michael Wrightd02c5b62014-02-10 15:10:22 -08009051} // namespace android