blob: bff1a04c57a11f57cfc15a1acf56746dbfeac11e [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>
19#include <InputMapper.h>
20#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080021#include <InputReaderBase.h>
22#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070023#include <KeyboardInputMapper.h>
24#include <MultiTouchInputMapper.h>
25#include <SingleTouchInputMapper.h>
26#include <SwitchInputMapper.h>
27#include <TestInputListener.h>
28#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080029#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000030#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070031#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080033#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <math.h>
35
Michael Wright17db18e2020-06-26 20:51:44 +010036#include <memory>
Michael Wrightdde67b82020-10-27 16:09:22 +000037#include "input/DisplayViewport.h"
38#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010039
Michael Wrightd02c5b62014-02-10 15:10:22 -080040namespace android {
41
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070042using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070043using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070044
45// Timeout for waiting for an expected event
46static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
47
Michael Wrightd02c5b62014-02-10 15:10:22 -080048// An arbitrary time value.
49static const nsecs_t ARBITRARY_TIME = 1234;
50
51// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080052static constexpr int32_t DISPLAY_ID = 0;
53static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
54static constexpr int32_t DISPLAY_WIDTH = 480;
55static constexpr int32_t DISPLAY_HEIGHT = 800;
56static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
57static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
58static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070059static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070060static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080061
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t FIRST_SLOT = 0;
63static constexpr int32_t SECOND_SLOT = 1;
64static constexpr int32_t THIRD_SLOT = 2;
65static constexpr int32_t INVALID_TRACKING_ID = -1;
66static constexpr int32_t FIRST_TRACKING_ID = 0;
67static constexpr int32_t SECOND_TRACKING_ID = 1;
68static constexpr int32_t THIRD_TRACKING_ID = 2;
69
Michael Wrightd02c5b62014-02-10 15:10:22 -080070// Error tolerance for floating point assertions.
71static const float EPSILON = 0.001f;
72
73template<typename T>
74static inline T min(T a, T b) {
75 return a < b ? a : b;
76}
77
78static inline float avg(float x, float y) {
79 return (x + y) / 2;
80}
81
82
83// --- FakePointerController ---
84
85class FakePointerController : public PointerControllerInterface {
86 bool mHaveBounds;
87 float mMinX, mMinY, mMaxX, mMaxY;
88 float mX, mY;
89 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080090 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080091
Michael Wrightd02c5b62014-02-10 15:10:22 -080092public:
93 FakePointerController() :
94 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080095 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 }
97
Michael Wright17db18e2020-06-26 20:51:44 +010098 virtual ~FakePointerController() {}
99
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100 void setBounds(float minX, float minY, float maxX, float maxY) {
101 mHaveBounds = true;
102 mMinX = minX;
103 mMinY = minY;
104 mMaxX = maxX;
105 mMaxY = maxY;
106 }
107
Chris Yea52ade12020-08-27 16:49:20 -0700108 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800109 mX = x;
110 mY = y;
111 }
112
Chris Yea52ade12020-08-27 16:49:20 -0700113 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
Chris Yea52ade12020-08-27 16:49:20 -0700115 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
Chris Yea52ade12020-08-27 16:49:20 -0700117 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118 *outX = mX;
119 *outY = mY;
120 }
121
Chris Yea52ade12020-08-27 16:49:20 -0700122 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800123
Chris Yea52ade12020-08-27 16:49:20 -0700124 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800125 mDisplayId = viewport.displayId;
126 }
127
Arthur Hung7c645402019-01-25 17:45:42 +0800128 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
129 return mSpotsByDisplay;
130 }
131
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132private:
Chris Yea52ade12020-08-27 16:49:20 -0700133 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134 *outMinX = mMinX;
135 *outMinY = mMinY;
136 *outMaxX = mMaxX;
137 *outMaxY = mMaxY;
138 return mHaveBounds;
139 }
140
Chris Yea52ade12020-08-27 16:49:20 -0700141 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142 mX += deltaX;
143 if (mX < mMinX) mX = mMinX;
144 if (mX > mMaxX) mX = mMaxX;
145 mY += deltaY;
146 if (mY < mMinY) mY = mMinY;
147 if (mY > mMaxY) mY = mMaxY;
148 }
149
Chris Yea52ade12020-08-27 16:49:20 -0700150 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151
Chris Yea52ade12020-08-27 16:49:20 -0700152 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153
Chris Yea52ade12020-08-27 16:49:20 -0700154 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155
Chris Yea52ade12020-08-27 16:49:20 -0700156 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
157 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800158 std::vector<int32_t> newSpots;
159 // Add spots for fingers that are down.
160 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
161 uint32_t id = idBits.clearFirstMarkedBit();
162 newSpots.push_back(id);
163 }
164
165 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166 }
167
Chris Yea52ade12020-08-27 16:49:20 -0700168 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800169
170 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171};
172
173
174// --- FakeInputReaderPolicy ---
175
176class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700177 std::mutex mLock;
178 std::condition_variable mDevicesChangedCondition;
179
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100181 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700182 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
183 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100184 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700185 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
187protected:
Chris Yea52ade12020-08-27 16:49:20 -0700188 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189
190public:
191 FakeInputReaderPolicy() {
192 }
193
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700194 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800195 waitForInputDevices([](bool devicesChanged) {
196 if (!devicesChanged) {
197 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
198 }
199 });
200 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700201
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800202 void assertInputDevicesNotChanged() {
203 waitForInputDevices([](bool devicesChanged) {
204 if (devicesChanged) {
205 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
206 }
207 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 }
209
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700210 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100211 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100212 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700213 }
214
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700215 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
216 return mConfig.getDisplayViewportByUniqueId(uniqueId);
217 }
218 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
219 return mConfig.getDisplayViewportByType(type);
220 }
221
222 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
223 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700224 }
225
226 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000227 bool isActive, const std::string& uniqueId,
228 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
229 const DisplayViewport viewport =
230 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
231 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700232 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100233 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 }
235
Arthur Hung6cd19a42019-08-30 19:04:12 +0800236 bool updateViewport(const DisplayViewport& viewport) {
237 size_t count = mViewports.size();
238 for (size_t i = 0; i < count; i++) {
239 const DisplayViewport& currentViewport = mViewports[i];
240 if (currentViewport.displayId == viewport.displayId) {
241 mViewports[i] = viewport;
242 mConfig.setDisplayViewports(mViewports);
243 return true;
244 }
245 }
246 // no viewport found.
247 return false;
248 }
249
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100250 void addExcludedDeviceName(const std::string& deviceName) {
251 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 }
253
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700254 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
255 mConfig.portAssociations.insert({inputPort, displayPort});
256 }
257
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000258 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700259
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000260 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700261
Michael Wright17db18e2020-06-26 20:51:44 +0100262 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
263 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264 }
265
266 const InputReaderConfiguration* getReaderConfiguration() const {
267 return &mConfig;
268 }
269
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800270 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271 return mInputDevices;
272 }
273
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100274 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700275 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700276 return transform;
277 }
278
279 void setTouchAffineTransformation(const TouchAffineTransformation t) {
280 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800281 }
282
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800283 void setPointerCapture(bool enabled) {
284 mConfig.pointerCapture = enabled;
285 }
286
Arthur Hung7c645402019-01-25 17:45:42 +0800287 void setShowTouches(bool enabled) {
288 mConfig.showTouches = enabled;
289 }
290
Garfield Tan888a6a42020-01-09 11:39:16 -0800291 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
292 mConfig.defaultPointerDisplayId = pointerDisplayId;
293 }
294
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800295 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
296
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700298 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000299 int32_t orientation, bool isActive,
300 const std::string& uniqueId,
301 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700302 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
303 || orientation == DISPLAY_ORIENTATION_270);
304 DisplayViewport v;
305 v.displayId = displayId;
306 v.orientation = orientation;
307 v.logicalLeft = 0;
308 v.logicalTop = 0;
309 v.logicalRight = isRotated ? height : width;
310 v.logicalBottom = isRotated ? width : height;
311 v.physicalLeft = 0;
312 v.physicalTop = 0;
313 v.physicalRight = isRotated ? height : width;
314 v.physicalBottom = isRotated ? width : height;
315 v.deviceWidth = isRotated ? height : width;
316 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000317 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700318 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700319 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100320 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700321 return v;
322 }
323
Chris Yea52ade12020-08-27 16:49:20 -0700324 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 *outConfig = mConfig;
326 }
327
Chris Yea52ade12020-08-27 16:49:20 -0700328 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100329 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 }
331
Chris Yea52ade12020-08-27 16:49:20 -0700332 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700333 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700335 mInputDevicesChanged = true;
336 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 }
338
Chris Yea52ade12020-08-27 16:49:20 -0700339 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
340 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700341 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342 }
343
Chris Yea52ade12020-08-27 16:49:20 -0700344 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800345
346 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
347 std::unique_lock<std::mutex> lock(mLock);
348 base::ScopedLockAssertion assumeLocked(mLock);
349
350 const bool devicesChanged =
351 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
352 return mInputDevicesChanged;
353 });
354 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
355 mInputDevicesChanged = false;
356 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357};
358
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359// --- FakeEventHub ---
360
361class FakeEventHub : public EventHubInterface {
362 struct KeyInfo {
363 int32_t keyCode;
364 uint32_t flags;
365 };
366
367 struct Device {
368 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700369 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370 PropertyMap configuration;
371 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
372 KeyedVector<int, bool> relativeAxes;
373 KeyedVector<int32_t, int32_t> keyCodeStates;
374 KeyedVector<int32_t, int32_t> scanCodeStates;
375 KeyedVector<int32_t, int32_t> switchStates;
376 KeyedVector<int32_t, int32_t> absoluteAxisValue;
377 KeyedVector<int32_t, KeyInfo> keysByScanCode;
378 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
379 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800380 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700381 bool enabled;
382
383 status_t enable() {
384 enabled = true;
385 return OK;
386 }
387
388 status_t disable() {
389 enabled = false;
390 return OK;
391 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392
Chris Ye1b0c7342020-07-28 21:57:03 -0700393 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394 };
395
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700396 std::mutex mLock;
397 std::condition_variable mEventsCondition;
398
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100400 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700401 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600402 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000403 std::vector<int32_t> mVibrators = {0, 1};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700405public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800406 virtual ~FakeEventHub() {
407 for (size_t i = 0; i < mDevices.size(); i++) {
408 delete mDevices.valueAt(i);
409 }
410 }
411
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412 FakeEventHub() { }
413
Chris Ye1b0c7342020-07-28 21:57:03 -0700414 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415 Device* device = new Device(classes);
416 device->identifier.name = name;
417 mDevices.add(deviceId, device);
418
419 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
420 }
421
422 void removeDevice(int32_t deviceId) {
423 delete mDevices.valueFor(deviceId);
424 mDevices.removeItem(deviceId);
425
426 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
427 }
428
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700429 bool isDeviceEnabled(int32_t deviceId) {
430 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700431 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700432 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
433 return false;
434 }
435 return device->enabled;
436 }
437
438 status_t enableDevice(int32_t deviceId) {
439 status_t result;
440 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700441 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700442 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
443 return BAD_VALUE;
444 }
445 if (device->enabled) {
446 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
447 return OK;
448 }
449 result = device->enable();
450 return result;
451 }
452
453 status_t disableDevice(int32_t deviceId) {
454 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700455 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700456 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
457 return BAD_VALUE;
458 }
459 if (!device->enabled) {
460 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
461 return OK;
462 }
463 return device->disable();
464 }
465
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 void finishDeviceScan() {
467 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
468 }
469
470 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
471 Device* device = getDevice(deviceId);
472 device->configuration.addProperty(key, value);
473 }
474
475 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
476 Device* device = getDevice(deviceId);
477 device->configuration.addAll(configuration);
478 }
479
480 void addAbsoluteAxis(int32_t deviceId, int axis,
481 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
482 Device* device = getDevice(deviceId);
483
484 RawAbsoluteAxisInfo info;
485 info.valid = true;
486 info.minValue = minValue;
487 info.maxValue = maxValue;
488 info.flat = flat;
489 info.fuzz = fuzz;
490 info.resolution = resolution;
491 device->absoluteAxes.add(axis, info);
492 }
493
494 void addRelativeAxis(int32_t deviceId, int32_t axis) {
495 Device* device = getDevice(deviceId);
496 device->relativeAxes.add(axis, true);
497 }
498
499 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
500 Device* device = getDevice(deviceId);
501 device->keyCodeStates.replaceValueFor(keyCode, state);
502 }
503
504 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
505 Device* device = getDevice(deviceId);
506 device->scanCodeStates.replaceValueFor(scanCode, state);
507 }
508
509 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
510 Device* device = getDevice(deviceId);
511 device->switchStates.replaceValueFor(switchCode, state);
512 }
513
514 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
515 Device* device = getDevice(deviceId);
516 device->absoluteAxisValue.replaceValueFor(axis, value);
517 }
518
519 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
520 int32_t keyCode, uint32_t flags) {
521 Device* device = getDevice(deviceId);
522 KeyInfo info;
523 info.keyCode = keyCode;
524 info.flags = flags;
525 if (scanCode) {
526 device->keysByScanCode.add(scanCode, info);
527 }
528 if (usageCode) {
529 device->keysByUsageCode.add(usageCode, info);
530 }
531 }
532
533 void addLed(int32_t deviceId, int32_t led, bool initialState) {
534 Device* device = getDevice(deviceId);
535 device->leds.add(led, initialState);
536 }
537
538 bool getLedState(int32_t deviceId, int32_t led) {
539 Device* device = getDevice(deviceId);
540 return device->leds.valueFor(led);
541 }
542
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100543 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544 return mExcludedDevices;
545 }
546
547 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
548 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800549 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 }
551
552 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
553 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700554 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555 RawEvent event;
556 event.when = when;
557 event.deviceId = deviceId;
558 event.type = type;
559 event.code = code;
560 event.value = value;
561 mEvents.push_back(event);
562
563 if (type == EV_ABS) {
564 setAbsoluteAxisValue(deviceId, code, value);
565 }
566 }
567
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600568 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
569 std::vector<TouchVideoFrame>> videoFrames) {
570 mVideoFrames = std::move(videoFrames);
571 }
572
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700574 std::unique_lock<std::mutex> lock(mLock);
575 base::ScopedLockAssertion assumeLocked(mLock);
576 const bool queueIsEmpty =
577 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
578 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
579 if (!queueIsEmpty) {
580 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
581 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582 }
583
584private:
585 Device* getDevice(int32_t deviceId) const {
586 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100587 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Chris Yea52ade12020-08-27 16:49:20 -0700590 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700592 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 }
594
Chris Yea52ade12020-08-27 16:49:20 -0700595 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 Device* device = getDevice(deviceId);
597 return device ? device->identifier : InputDeviceIdentifier();
598 }
599
Chris Yea52ade12020-08-27 16:49:20 -0700600 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601
Chris Yea52ade12020-08-27 16:49:20 -0700602 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603 Device* device = getDevice(deviceId);
604 if (device) {
605 *outConfiguration = device->configuration;
606 }
607 }
608
Chris Yea52ade12020-08-27 16:49:20 -0700609 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
610 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800612 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613 ssize_t index = device->absoluteAxes.indexOfKey(axis);
614 if (index >= 0) {
615 *outAxisInfo = device->absoluteAxes.valueAt(index);
616 return OK;
617 }
618 }
619 outAxisInfo->clear();
620 return -1;
621 }
622
Chris Yea52ade12020-08-27 16:49:20 -0700623 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800624 Device* device = getDevice(deviceId);
625 if (device) {
626 return device->relativeAxes.indexOfKey(axis) >= 0;
627 }
628 return false;
629 }
630
Chris Yea52ade12020-08-27 16:49:20 -0700631 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632
Chris Yea52ade12020-08-27 16:49:20 -0700633 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
634 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 Device* device = getDevice(deviceId);
636 if (device) {
637 const KeyInfo* key = getKey(device, scanCode, usageCode);
638 if (key) {
639 if (outKeycode) {
640 *outKeycode = key->keyCode;
641 }
642 if (outFlags) {
643 *outFlags = key->flags;
644 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700645 if (outMetaState) {
646 *outMetaState = metaState;
647 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 return OK;
649 }
650 }
651 return NAME_NOT_FOUND;
652 }
653
654 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
655 if (usageCode) {
656 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
657 if (index >= 0) {
658 return &device->keysByUsageCode.valueAt(index);
659 }
660 }
661 if (scanCode) {
662 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
663 if (index >= 0) {
664 return &device->keysByScanCode.valueAt(index);
665 }
666 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700667 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 }
669
Chris Yea52ade12020-08-27 16:49:20 -0700670 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671
Chris Yea52ade12020-08-27 16:49:20 -0700672 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800673 mExcludedDevices = devices;
674 }
675
Chris Yea52ade12020-08-27 16:49:20 -0700676 size_t getEvents(int, RawEvent* buffer, size_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700677 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678 if (mEvents.empty()) {
679 return 0;
680 }
681
682 *buffer = *mEvents.begin();
683 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700684 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 return 1;
686 }
687
Chris Yea52ade12020-08-27 16:49:20 -0700688 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600689 auto it = mVideoFrames.find(deviceId);
690 if (it != mVideoFrames.end()) {
691 std::vector<TouchVideoFrame> frames = std::move(it->second);
692 mVideoFrames.erase(deviceId);
693 return frames;
694 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800695 return {};
696 }
697
Chris Yea52ade12020-08-27 16:49:20 -0700698 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699 Device* device = getDevice(deviceId);
700 if (device) {
701 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
702 if (index >= 0) {
703 return device->scanCodeStates.valueAt(index);
704 }
705 }
706 return AKEY_STATE_UNKNOWN;
707 }
708
Chris Yea52ade12020-08-27 16:49:20 -0700709 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710 Device* device = getDevice(deviceId);
711 if (device) {
712 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
713 if (index >= 0) {
714 return device->keyCodeStates.valueAt(index);
715 }
716 }
717 return AKEY_STATE_UNKNOWN;
718 }
719
Chris Yea52ade12020-08-27 16:49:20 -0700720 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 Device* device = getDevice(deviceId);
722 if (device) {
723 ssize_t index = device->switchStates.indexOfKey(sw);
724 if (index >= 0) {
725 return device->switchStates.valueAt(index);
726 }
727 }
728 return AKEY_STATE_UNKNOWN;
729 }
730
Chris Yea52ade12020-08-27 16:49:20 -0700731 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
732 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 Device* device = getDevice(deviceId);
734 if (device) {
735 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
736 if (index >= 0) {
737 *outValue = device->absoluteAxisValue.valueAt(index);
738 return OK;
739 }
740 }
741 *outValue = 0;
742 return -1;
743 }
744
Chris Yea52ade12020-08-27 16:49:20 -0700745 // Return true if the device has non-empty key layout.
746 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
747 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748 bool result = false;
749 Device* device = getDevice(deviceId);
750 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700751 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 for (size_t i = 0; i < numCodes; i++) {
753 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
754 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
755 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 }
757 }
758 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
759 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
760 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761 }
762 }
763 }
764 }
765 return result;
766 }
767
Chris Yea52ade12020-08-27 16:49:20 -0700768 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800769 Device* device = getDevice(deviceId);
770 if (device) {
771 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
772 return index >= 0;
773 }
774 return false;
775 }
776
Chris Yea52ade12020-08-27 16:49:20 -0700777 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800778 Device* device = getDevice(deviceId);
779 return device && device->leds.indexOfKey(led) >= 0;
780 }
781
Chris Yea52ade12020-08-27 16:49:20 -0700782 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 Device* device = getDevice(deviceId);
784 if (device) {
785 ssize_t index = device->leds.indexOfKey(led);
786 if (index >= 0) {
787 device->leds.replaceValueAt(led, on);
788 } else {
789 ADD_FAILURE()
790 << "Attempted to set the state of an LED that the EventHub declared "
791 "was not present. led=" << led;
792 }
793 }
794 }
795
Chris Yea52ade12020-08-27 16:49:20 -0700796 void getVirtualKeyDefinitions(
797 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 outVirtualKeys.clear();
799
800 Device* device = getDevice(deviceId);
801 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800802 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803 }
804 }
805
Chris Yea52ade12020-08-27 16:49:20 -0700806 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700807 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808 }
809
Chris Yea52ade12020-08-27 16:49:20 -0700810 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800811 return false;
812 }
813
Chris Yea52ade12020-08-27 16:49:20 -0700814 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800815
Chris Yea52ade12020-08-27 16:49:20 -0700816 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817
Chris Ye87143712020-11-10 05:05:58 +0000818 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
819
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100820 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 return false;
822 }
823
Chris Yea52ade12020-08-27 16:49:20 -0700824 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825
Chris Yea52ade12020-08-27 16:49:20 -0700826 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827
Chris Yea52ade12020-08-27 16:49:20 -0700828 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829
Chris Yea52ade12020-08-27 16:49:20 -0700830 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831};
832
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833// --- FakeInputMapper ---
834
835class FakeInputMapper : public InputMapper {
836 uint32_t mSources;
837 int32_t mKeyboardType;
838 int32_t mMetaState;
839 KeyedVector<int32_t, int32_t> mKeyCodeStates;
840 KeyedVector<int32_t, int32_t> mScanCodeStates;
841 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800842 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700844 std::mutex mLock;
845 std::condition_variable mStateChangedCondition;
846 bool mConfigureWasCalled GUARDED_BY(mLock);
847 bool mResetWasCalled GUARDED_BY(mLock);
848 bool mProcessWasCalled GUARDED_BY(mLock);
849 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850
Arthur Hungc23540e2018-11-29 20:42:11 +0800851 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800853 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
854 : InputMapper(deviceContext),
855 mSources(sources),
856 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800858 mConfigureWasCalled(false),
859 mResetWasCalled(false),
860 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861
Chris Yea52ade12020-08-27 16:49:20 -0700862 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863
864 void setKeyboardType(int32_t keyboardType) {
865 mKeyboardType = keyboardType;
866 }
867
868 void setMetaState(int32_t metaState) {
869 mMetaState = metaState;
870 }
871
872 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700873 std::unique_lock<std::mutex> lock(mLock);
874 base::ScopedLockAssertion assumeLocked(mLock);
875 const bool configureCalled =
876 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
877 return mConfigureWasCalled;
878 });
879 if (!configureCalled) {
880 FAIL() << "Expected configure() to have been called.";
881 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882 mConfigureWasCalled = false;
883 }
884
885 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700886 std::unique_lock<std::mutex> lock(mLock);
887 base::ScopedLockAssertion assumeLocked(mLock);
888 const bool resetCalled =
889 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
890 return mResetWasCalled;
891 });
892 if (!resetCalled) {
893 FAIL() << "Expected reset() to have been called.";
894 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 mResetWasCalled = false;
896 }
897
Yi Kong9b14ac62018-07-17 13:48:38 -0700898 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700899 std::unique_lock<std::mutex> lock(mLock);
900 base::ScopedLockAssertion assumeLocked(mLock);
901 const bool processCalled =
902 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
903 return mProcessWasCalled;
904 });
905 if (!processCalled) {
906 FAIL() << "Expected process() to have been called.";
907 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908 if (outLastEvent) {
909 *outLastEvent = mLastEvent;
910 }
911 mProcessWasCalled = false;
912 }
913
914 void setKeyCodeState(int32_t keyCode, int32_t state) {
915 mKeyCodeStates.replaceValueFor(keyCode, state);
916 }
917
918 void setScanCodeState(int32_t scanCode, int32_t state) {
919 mScanCodeStates.replaceValueFor(scanCode, state);
920 }
921
922 void setSwitchState(int32_t switchCode, int32_t state) {
923 mSwitchStates.replaceValueFor(switchCode, state);
924 }
925
926 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800927 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928 }
929
930private:
Chris Yea52ade12020-08-27 16:49:20 -0700931 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932
Chris Yea52ade12020-08-27 16:49:20 -0700933 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 InputMapper::populateDeviceInfo(deviceInfo);
935
936 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
937 deviceInfo->setKeyboardType(mKeyboardType);
938 }
939 }
940
Chris Yea52ade12020-08-27 16:49:20 -0700941 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700942 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800944
945 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800946 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800947 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
948 mViewport = config->getDisplayViewportByPort(*displayPort);
949 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700950
951 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 }
953
Chris Yea52ade12020-08-27 16:49:20 -0700954 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700955 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800956 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700957 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958 }
959
Chris Yea52ade12020-08-27 16:49:20 -0700960 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700961 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800962 mLastEvent = *rawEvent;
963 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700964 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 }
966
Chris Yea52ade12020-08-27 16:49:20 -0700967 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
969 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
970 }
971
Chris Yea52ade12020-08-27 16:49:20 -0700972 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
974 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
975 }
976
Chris Yea52ade12020-08-27 16:49:20 -0700977 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800978 ssize_t index = mSwitchStates.indexOfKey(switchCode);
979 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
980 }
981
Chris Yea52ade12020-08-27 16:49:20 -0700982 // Return true if the device has non-empty key layout.
983 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
984 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 for (size_t i = 0; i < numCodes; i++) {
986 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
987 if (keyCodes[i] == mSupportedKeyCodes[j]) {
988 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989 }
990 }
991 }
Chris Yea52ade12020-08-27 16:49:20 -0700992 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 return result;
994 }
995
996 virtual int32_t getMetaState() {
997 return mMetaState;
998 }
999
1000 virtual void fadePointer() {
1001 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001002
1003 virtual std::optional<int32_t> getAssociatedDisplay() {
1004 if (mViewport) {
1005 return std::make_optional(mViewport->displayId);
1006 }
1007 return std::nullopt;
1008 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009};
1010
1011
1012// --- InstrumentedInputReader ---
1013
1014class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001015 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016
1017public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001018 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1019 const sp<InputReaderPolicyInterface>& policy,
1020 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001021 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001023 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001024
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001025 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001027 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001028 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001029 InputDeviceIdentifier identifier;
1030 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001031 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001033 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034 }
1035
Prabir Pradhan28efc192019-11-05 01:10:04 +00001036 // Make the protected loopOnce method accessible to tests.
1037 using InputReader::loopOnce;
1038
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001040 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1041 const InputDeviceIdentifier& identifier)
1042 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001043 if (!mNextDevices.empty()) {
1044 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1045 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046 return device;
1047 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001048 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049 }
1050
arthurhungdcef2dc2020-08-11 14:47:50 +08001051 // --- FakeInputReaderContext ---
1052 class FakeInputReaderContext : public ContextImpl {
1053 int32_t mGlobalMetaState;
1054 bool mUpdateGlobalMetaStateWasCalled;
1055 int32_t mGeneration;
1056
1057 public:
1058 FakeInputReaderContext(InputReader* reader)
1059 : ContextImpl(reader),
1060 mGlobalMetaState(0),
1061 mUpdateGlobalMetaStateWasCalled(false),
1062 mGeneration(1) {}
1063
1064 virtual ~FakeInputReaderContext() {}
1065
1066 void assertUpdateGlobalMetaStateWasCalled() {
1067 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1068 << "Expected updateGlobalMetaState() to have been called.";
1069 mUpdateGlobalMetaStateWasCalled = false;
1070 }
1071
1072 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1073
1074 uint32_t getGeneration() { return mGeneration; }
1075
1076 void updateGlobalMetaState() override {
1077 mUpdateGlobalMetaStateWasCalled = true;
1078 ContextImpl::updateGlobalMetaState();
1079 }
1080
1081 int32_t getGlobalMetaState() override {
1082 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1083 }
1084
1085 int32_t bumpGeneration() override {
1086 mGeneration = ContextImpl::bumpGeneration();
1087 return mGeneration;
1088 }
1089 } mFakeContext;
1090
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001092
1093public:
1094 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095};
1096
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001097// --- InputReaderPolicyTest ---
1098class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001099protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001100 sp<FakeInputReaderPolicy> mFakePolicy;
1101
Chris Yea52ade12020-08-27 16:49:20 -07001102 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1103 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001104};
1105
1106/**
1107 * Check that empty set of viewports is an acceptable configuration.
1108 * Also try to get internal viewport two different ways - by type and by uniqueId.
1109 *
1110 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1111 * Such configuration is not currently allowed.
1112 */
1113TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001114 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001115
1116 // We didn't add any viewports yet, so there shouldn't be any.
1117 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001118 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001119 ASSERT_FALSE(internalViewport);
1120
1121 // Add an internal viewport, then clear it
1122 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001123 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001124 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001125
1126 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001127 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001128 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001129 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001130
1131 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001132 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001133 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001134 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001135
1136 mFakePolicy->clearViewports();
1137 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001138 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001139 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001140 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001141 ASSERT_FALSE(internalViewport);
1142}
1143
1144TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1145 const std::string internalUniqueId = "local:0";
1146 const std::string externalUniqueId = "local:1";
1147 const std::string virtualUniqueId1 = "virtual:2";
1148 const std::string virtualUniqueId2 = "virtual:3";
1149 constexpr int32_t virtualDisplayId1 = 2;
1150 constexpr int32_t virtualDisplayId2 = 3;
1151
1152 // Add an internal viewport
1153 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001154 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1155 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001156 // Add an external viewport
1157 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001158 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1159 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001160 // Add an virtual viewport
1161 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001162 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1163 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001164 // Add another virtual viewport
1165 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001166 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1167 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001168
1169 // Check matching by type for internal
1170 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001171 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001172 ASSERT_TRUE(internalViewport);
1173 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1174
1175 // Check matching by type for external
1176 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001177 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001178 ASSERT_TRUE(externalViewport);
1179 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1180
1181 // Check matching by uniqueId for virtual viewport #1
1182 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001183 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001184 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001185 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001186 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1187 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1188
1189 // Check matching by uniqueId for virtual viewport #2
1190 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001191 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001192 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001193 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001194 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1195 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1196}
1197
1198
1199/**
1200 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1201 * that lookup works by checking display id.
1202 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1203 */
1204TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1205 const std::string uniqueId1 = "uniqueId1";
1206 const std::string uniqueId2 = "uniqueId2";
1207 constexpr int32_t displayId1 = 2;
1208 constexpr int32_t displayId2 = 3;
1209
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001210 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1211 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001212 for (const ViewportType& type : types) {
1213 mFakePolicy->clearViewports();
1214 // Add a viewport
1215 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001216 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1217 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001218 // Add another viewport
1219 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001220 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1221 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001222
1223 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001224 std::optional<DisplayViewport> viewport1 =
1225 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001226 ASSERT_TRUE(viewport1);
1227 ASSERT_EQ(displayId1, viewport1->displayId);
1228 ASSERT_EQ(type, viewport1->type);
1229
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001230 std::optional<DisplayViewport> viewport2 =
1231 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001232 ASSERT_TRUE(viewport2);
1233 ASSERT_EQ(displayId2, viewport2->displayId);
1234 ASSERT_EQ(type, viewport2->type);
1235
1236 // When there are multiple viewports of the same kind, and uniqueId is not specified
1237 // in the call to getDisplayViewport, then that situation is not supported.
1238 // The viewports can be stored in any order, so we cannot rely on the order, since that
1239 // is just implementation detail.
1240 // However, we can check that it still returns *a* viewport, we just cannot assert
1241 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001242 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001243 ASSERT_TRUE(someViewport);
1244 }
1245}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001247/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001248 * When we have multiple internal displays make sure we always return the default display when
1249 * querying by type.
1250 */
1251TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1252 const std::string uniqueId1 = "uniqueId1";
1253 const std::string uniqueId2 = "uniqueId2";
1254 constexpr int32_t nonDefaultDisplayId = 2;
1255 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1256 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1257
1258 // Add the default display first and ensure it gets returned.
1259 mFakePolicy->clearViewports();
1260 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001261 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001262 ViewportType::INTERNAL);
1263 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001264 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001265 ViewportType::INTERNAL);
1266
1267 std::optional<DisplayViewport> viewport =
1268 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1269 ASSERT_TRUE(viewport);
1270 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1271 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1272
1273 // Add the default display second to make sure order doesn't matter.
1274 mFakePolicy->clearViewports();
1275 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001276 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001277 ViewportType::INTERNAL);
1278 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001279 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001280 ViewportType::INTERNAL);
1281
1282 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1283 ASSERT_TRUE(viewport);
1284 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1285 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1286}
1287
1288/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001289 * Check getDisplayViewportByPort
1290 */
1291TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001292 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001293 const std::string uniqueId1 = "uniqueId1";
1294 const std::string uniqueId2 = "uniqueId2";
1295 constexpr int32_t displayId1 = 1;
1296 constexpr int32_t displayId2 = 2;
1297 const uint8_t hdmi1 = 0;
1298 const uint8_t hdmi2 = 1;
1299 const uint8_t hdmi3 = 2;
1300
1301 mFakePolicy->clearViewports();
1302 // Add a viewport that's associated with some display port that's not of interest.
1303 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001304 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1305 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001306 // Add another viewport, connected to HDMI1 port
1307 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001308 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1309 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001310
1311 // Check that correct display viewport was returned by comparing the display ports.
1312 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1313 ASSERT_TRUE(hdmi1Viewport);
1314 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1315 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1316
1317 // Check that we can still get the same viewport using the uniqueId
1318 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1319 ASSERT_TRUE(hdmi1Viewport);
1320 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1321 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1322 ASSERT_EQ(type, hdmi1Viewport->type);
1323
1324 // Check that we cannot find a port with "HDMI2", because we never added one
1325 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1326 ASSERT_FALSE(hdmi2Viewport);
1327}
1328
Michael Wrightd02c5b62014-02-10 15:10:22 -08001329// --- InputReaderTest ---
1330
1331class InputReaderTest : public testing::Test {
1332protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001333 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001334 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001335 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001336 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001337
Chris Yea52ade12020-08-27 16:49:20 -07001338 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001339 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001340 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001341 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001342
Prabir Pradhan28efc192019-11-05 01:10:04 +00001343 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1344 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001345 }
1346
Chris Yea52ade12020-08-27 16:49:20 -07001347 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001348 mFakeListener.clear();
1349 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001350 }
1351
Chris Ye1b0c7342020-07-28 21:57:03 -07001352 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001353 const PropertyMap* configuration) {
1354 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001355
1356 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001357 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001358 }
1359 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001360 mReader->loopOnce();
1361 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001362 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1363 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001364 }
1365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001366 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001367 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001368 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001369 }
1370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001371 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001372 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001373 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001374 }
1375
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001376 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001377 const std::string& name,
1378 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001379 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001380 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1381 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001382 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001383 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384 return mapper;
1385 }
1386};
1387
Chris Ye98d3f532020-10-01 21:48:59 -07001388TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001389 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1390 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1391 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001392
Chris Ye98d3f532020-10-01 21:48:59 -07001393 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001394 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001395 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001396 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001397 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1398 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1399 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001400}
1401
1402TEST_F(InputReaderTest, PolicyGetInputDevices) {
1403 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1404 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1405 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001406
1407 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001408 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001409 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001410 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001411 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001412 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1413 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1414 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1415}
1416
Chris Yee7310032020-09-22 15:36:28 -07001417TEST_F(InputReaderTest, GetMergedInputDevices) {
1418 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1419 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1420 // Add two subdevices to device
1421 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1422 // Must add at least one mapper or the device will be ignored!
1423 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1424 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1425
1426 // Push same device instance for next device to be added, so they'll have same identifier.
1427 mReader->pushNextDevice(device);
1428 mReader->pushNextDevice(device);
1429 ASSERT_NO_FATAL_FAILURE(
1430 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1431 ASSERT_NO_FATAL_FAILURE(
1432 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1433
1434 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001435 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001436}
1437
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001438TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001439 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001440 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001441 constexpr int32_t eventHubId = 1;
1442 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001443 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001444 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001445 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001446 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001447
Yi Kong9b14ac62018-07-17 13:48:38 -07001448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001449
1450 NotifyDeviceResetArgs resetArgs;
1451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001452 ASSERT_EQ(deviceId, resetArgs.deviceId);
1453
1454 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001455 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001456 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001457
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001459 ASSERT_EQ(deviceId, resetArgs.deviceId);
1460 ASSERT_EQ(device->isEnabled(), false);
1461
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001462 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001463 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001466 ASSERT_EQ(device->isEnabled(), false);
1467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001468 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001469 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001471 ASSERT_EQ(deviceId, resetArgs.deviceId);
1472 ASSERT_EQ(device->isEnabled(), true);
1473}
1474
Michael Wrightd02c5b62014-02-10 15:10:22 -08001475TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001476 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001477 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001478 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001479 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001480 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001481 AINPUT_SOURCE_KEYBOARD, nullptr);
1482 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001483
1484 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1485 AINPUT_SOURCE_ANY, AKEYCODE_A))
1486 << "Should return unknown when the device id is >= 0 but unknown.";
1487
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001488 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1489 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1490 << "Should return unknown when the device id is valid but the sources are not "
1491 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001492
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001493 ASSERT_EQ(AKEY_STATE_DOWN,
1494 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1495 AKEYCODE_A))
1496 << "Should return value provided by mapper when device id is valid and the device "
1497 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001498
1499 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1500 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1501 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1502
1503 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1504 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1505 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1506}
1507
1508TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001509 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001510 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001511 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001512 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001513 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001514 AINPUT_SOURCE_KEYBOARD, nullptr);
1515 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516
1517 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1518 AINPUT_SOURCE_ANY, KEY_A))
1519 << "Should return unknown when the device id is >= 0 but unknown.";
1520
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001521 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1522 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1523 << "Should return unknown when the device id is valid but the sources are not "
1524 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001525
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001526 ASSERT_EQ(AKEY_STATE_DOWN,
1527 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1528 KEY_A))
1529 << "Should return value provided by mapper when device id is valid and the device "
1530 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001531
1532 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1533 AINPUT_SOURCE_TRACKBALL, KEY_A))
1534 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1535
1536 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1537 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1538 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1539}
1540
1541TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001542 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001543 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001544 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001545 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001546 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001547 AINPUT_SOURCE_KEYBOARD, nullptr);
1548 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001549
1550 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1551 AINPUT_SOURCE_ANY, SW_LID))
1552 << "Should return unknown when the device id is >= 0 but unknown.";
1553
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001554 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1555 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1556 << "Should return unknown when the device id is valid but the sources are not "
1557 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001559 ASSERT_EQ(AKEY_STATE_DOWN,
1560 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1561 SW_LID))
1562 << "Should return value provided by mapper when device id is valid and the device "
1563 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564
1565 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1566 AINPUT_SOURCE_TRACKBALL, SW_LID))
1567 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1568
1569 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1570 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1571 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1572}
1573
1574TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001575 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001576 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001577 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001578 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001579 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001580 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001582 mapper.addSupportedKeyCode(AKEYCODE_A);
1583 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584
1585 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1586 uint8_t flags[4] = { 0, 0, 0, 1 };
1587
1588 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1589 << "Should return false when device id is >= 0 but unknown.";
1590 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1591
1592 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001593 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1594 << "Should return false when device id is valid but the sources are not supported by "
1595 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1597
1598 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001599 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1600 keyCodes, flags))
1601 << "Should return value provided by mapper when device id is valid and the device "
1602 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001603 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1604
1605 flags[3] = 1;
1606 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1607 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1608 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1609
1610 flags[3] = 1;
1611 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1612 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1613 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1614}
1615
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001616TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001617 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001618 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619
1620 NotifyConfigurationChangedArgs args;
1621
1622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1623 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1624}
1625
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001626TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001628 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001629 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001630 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001631 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001632 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001633
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001634 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001635 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001636 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1637
1638 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001639 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642 ASSERT_EQ(EV_KEY, event.type);
1643 ASSERT_EQ(KEY_A, event.code);
1644 ASSERT_EQ(1, event.value);
1645}
1646
Garfield Tan1c7bc862020-01-28 13:24:04 -08001647TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001649 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001650 constexpr int32_t eventHubId = 1;
1651 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001652 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001654 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001655 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001656
1657 NotifyDeviceResetArgs resetArgs;
1658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001659 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001660
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001661 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001662 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001664 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001665 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001666
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001667 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001668 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001670 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001671 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001673 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001674 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001676 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001677 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001678}
1679
Garfield Tan1c7bc862020-01-28 13:24:04 -08001680TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1681 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001682 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001683 constexpr int32_t eventHubId = 1;
1684 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1685 // Must add at least one mapper or the device will be ignored!
1686 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001687 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001688 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1689
1690 NotifyDeviceResetArgs resetArgs;
1691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1692 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1693}
1694
Arthur Hungc23540e2018-11-29 20:42:11 +08001695TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001697 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001698 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001699 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001700 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1701 FakeInputMapper& mapper =
1702 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001703 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001704
1705 const uint8_t hdmi1 = 1;
1706
1707 // Associated touch screen with second display.
1708 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1709
1710 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001711 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001712 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001713 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001714 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001715 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001716 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001717 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001718 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001719 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001720
1721 // Add the device, and make sure all of the callbacks are triggered.
1722 // The device is added after the input port associations are processed since
1723 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001727 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001728
Arthur Hung2c9a3342019-07-23 14:18:59 +08001729 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001730 ASSERT_EQ(deviceId, device->getId());
1731 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1732 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001733
1734 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001735 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001736 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001737 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001738}
1739
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001740TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1741 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1742 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1743 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1744 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1745 // Must add at least one mapper or the device will be ignored!
1746 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1747 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1748 mReader->pushNextDevice(device);
1749 mReader->pushNextDevice(device);
1750 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1751 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1752
1753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1754
1755 NotifyDeviceResetArgs resetArgs;
1756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1757 ASSERT_EQ(deviceId, resetArgs.deviceId);
1758 ASSERT_TRUE(device->isEnabled());
1759 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1760 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1761
1762 disableDevice(deviceId);
1763 mReader->loopOnce();
1764
1765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1766 ASSERT_EQ(deviceId, resetArgs.deviceId);
1767 ASSERT_FALSE(device->isEnabled());
1768 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1769 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1770
1771 enableDevice(deviceId);
1772 mReader->loopOnce();
1773
1774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1775 ASSERT_EQ(deviceId, resetArgs.deviceId);
1776 ASSERT_TRUE(device->isEnabled());
1777 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1778 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1779}
1780
1781TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1782 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1783 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1784 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1785 // Add two subdevices to device
1786 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1787 FakeInputMapper& mapperDevice1 =
1788 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1789 FakeInputMapper& mapperDevice2 =
1790 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1791 mReader->pushNextDevice(device);
1792 mReader->pushNextDevice(device);
1793 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1794 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1795
1796 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1797 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1798
1799 ASSERT_EQ(AKEY_STATE_DOWN,
1800 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1801 ASSERT_EQ(AKEY_STATE_DOWN,
1802 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1803 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1804 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1805}
1806
Prabir Pradhan7e186182020-11-10 13:56:45 -08001807TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1808 NotifyPointerCaptureChangedArgs args;
1809
1810 mFakePolicy->setPointerCapture(true);
1811 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1812 mReader->loopOnce();
1813 mFakeListener->assertNotifyCaptureWasCalled(&args);
1814 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1815
1816 mFakePolicy->setPointerCapture(false);
1817 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1818 mReader->loopOnce();
1819 mFakeListener->assertNotifyCaptureWasCalled(&args);
1820 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1821
1822 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1823 // does not change.
1824 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1825 mReader->loopOnce();
1826 mFakeListener->assertNotifyCaptureWasCalled(&args);
1827 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1828}
1829
Chris Ye87143712020-11-10 05:05:58 +00001830class FakeVibratorInputMapper : public FakeInputMapper {
1831public:
1832 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1833 : FakeInputMapper(deviceContext, sources) {}
1834
1835 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1836};
1837
1838TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1839 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1840 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1841 constexpr int32_t eventHubId = 1;
1842 const char* DEVICE_LOCATION = "BLUETOOTH";
1843 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1844 FakeVibratorInputMapper& mapper =
1845 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1846 mReader->pushNextDevice(device);
1847
1848 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1849 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1850
1851 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1852 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1853}
1854
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001855// --- InputReaderIntegrationTest ---
1856
1857// These tests create and interact with the InputReader only through its interface.
1858// The InputReader is started during SetUp(), which starts its processing in its own
1859// thread. The tests use linux uinput to emulate input devices.
1860// NOTE: Interacting with the physical device while these tests are running may cause
1861// the tests to fail.
1862class InputReaderIntegrationTest : public testing::Test {
1863protected:
1864 sp<TestInputListener> mTestListener;
1865 sp<FakeInputReaderPolicy> mFakePolicy;
1866 sp<InputReaderInterface> mReader;
1867
Chris Yea52ade12020-08-27 16:49:20 -07001868 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001869 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001870 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1871 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001872
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001873 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001874 ASSERT_EQ(mReader->start(), OK);
1875
1876 // Since this test is run on a real device, all the input devices connected
1877 // to the test device will show up in mReader. We wait for those input devices to
1878 // show up before beginning the tests.
1879 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1880 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1881 }
1882
Chris Yea52ade12020-08-27 16:49:20 -07001883 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001884 ASSERT_EQ(mReader->stop(), OK);
1885 mTestListener.clear();
1886 mFakePolicy.clear();
1887 }
1888};
1889
1890TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1891 // An invalid input device that is only used for this test.
1892 class InvalidUinputDevice : public UinputDevice {
1893 public:
1894 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1895
1896 private:
1897 void configureDevice(int fd, uinput_user_dev* device) override {}
1898 };
1899
1900 const size_t numDevices = mFakePolicy->getInputDevices().size();
1901
1902 // UinputDevice does not set any event or key bits, so InputReader should not
1903 // consider it as a valid device.
1904 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1905 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1906 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1907 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1908
1909 invalidDevice.reset();
1910 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1911 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1912 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1913}
1914
1915TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1916 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1917
1918 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1919 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1920 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1921 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1922
1923 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07001924 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
1925 const auto& it =
1926 std::find_if(inputDevices.begin(), inputDevices.end(),
1927 [&keyboard](const InputDeviceInfo& info) {
1928 return info.getIdentifier().name == keyboard->getName();
1929 });
1930
1931 ASSERT_NE(it, inputDevices.end());
1932 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
1933 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
1934 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001935
1936 keyboard.reset();
1937 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1938 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1939 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1940}
1941
1942TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1943 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1944 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1945
1946 NotifyConfigurationChangedArgs configChangedArgs;
1947 ASSERT_NO_FATAL_FAILURE(
1948 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001949 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001950 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1951
1952 NotifyKeyArgs keyArgs;
1953 keyboard->pressAndReleaseHomeKey();
1954 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1955 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001956 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001957 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001958 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1959 prevTimestamp = keyArgs.eventTime;
1960
1961 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1962 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001963 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001964 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1965}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001966
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001967/**
1968 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1969 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1970 * are passed to the listener.
1971 */
1972static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1973TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1974 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1975 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1976 NotifyKeyArgs keyArgs;
1977
1978 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1979 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1980 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1981 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1982
1983 controller->pressAndReleaseKey(BTN_GEAR_UP);
1984 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1985 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1986 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1987}
1988
Arthur Hungaab25622020-01-16 11:22:11 +08001989// --- TouchProcessTest ---
1990class TouchIntegrationTest : public InputReaderIntegrationTest {
1991protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001992 const std::string UNIQUE_ID = "local:0";
1993
Chris Yea52ade12020-08-27 16:49:20 -07001994 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08001995 InputReaderIntegrationTest::SetUp();
1996 // At least add an internal display.
1997 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1998 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001999 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002000
2001 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2002 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2003 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2004 }
2005
2006 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2007 int32_t orientation, const std::string& uniqueId,
2008 std::optional<uint8_t> physicalPort,
2009 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002010 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2011 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002012 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2013 }
2014
2015 std::unique_ptr<UinputTouchScreen> mDevice;
2016};
2017
2018TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2019 NotifyMotionArgs args;
2020 const Point centerPoint = mDevice->getCenterPoint();
2021
2022 // ACTION_DOWN
2023 mDevice->sendDown(centerPoint);
2024 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2025 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2026
2027 // ACTION_MOVE
2028 mDevice->sendMove(centerPoint + Point(1, 1));
2029 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2031
2032 // ACTION_UP
2033 mDevice->sendUp();
2034 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2035 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2036}
2037
2038TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2039 NotifyMotionArgs args;
2040 const Point centerPoint = mDevice->getCenterPoint();
2041
2042 // ACTION_DOWN
2043 mDevice->sendDown(centerPoint);
2044 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2045 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2046
2047 // ACTION_POINTER_DOWN (Second slot)
2048 const Point secondPoint = centerPoint + Point(100, 100);
2049 mDevice->sendSlot(SECOND_SLOT);
2050 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2051 mDevice->sendDown(secondPoint + Point(1, 1));
2052 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2053 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2054 args.action);
2055
2056 // ACTION_MOVE (Second slot)
2057 mDevice->sendMove(secondPoint);
2058 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2060
2061 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002062 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002063 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002064 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002065 args.action);
2066
2067 // ACTION_UP
2068 mDevice->sendSlot(FIRST_SLOT);
2069 mDevice->sendUp();
2070 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2071 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2072}
2073
2074TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2075 NotifyMotionArgs args;
2076 const Point centerPoint = mDevice->getCenterPoint();
2077
2078 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002079 mDevice->sendSlot(FIRST_SLOT);
2080 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002081 mDevice->sendDown(centerPoint);
2082 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2083 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2084
arthurhungcc7f9802020-04-30 17:55:40 +08002085 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002086 const Point secondPoint = centerPoint + Point(100, 100);
2087 mDevice->sendSlot(SECOND_SLOT);
2088 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2089 mDevice->sendDown(secondPoint);
2090 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2091 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2092 args.action);
2093
arthurhungcc7f9802020-04-30 17:55:40 +08002094 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002095 mDevice->sendMove(secondPoint + Point(1, 1));
2096 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2098
arthurhungcc7f9802020-04-30 17:55:40 +08002099 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2100 // a palm event.
2101 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002102 mDevice->sendToolType(MT_TOOL_PALM);
2103 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002104 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2105 args.action);
2106 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002107
arthurhungcc7f9802020-04-30 17:55:40 +08002108 // Send up to second slot, expect first slot send moving.
2109 mDevice->sendPointerUp();
2110 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002112
arthurhungcc7f9802020-04-30 17:55:40 +08002113 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002114 mDevice->sendSlot(FIRST_SLOT);
2115 mDevice->sendUp();
2116
arthurhungcc7f9802020-04-30 17:55:40 +08002117 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2118 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002119}
2120
Michael Wrightd02c5b62014-02-10 15:10:22 -08002121// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002122class InputDeviceTest : public testing::Test {
2123protected:
2124 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002125 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002126 static const int32_t DEVICE_ID;
2127 static const int32_t DEVICE_GENERATION;
2128 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002129 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002130 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002131
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002132 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002133 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002134 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002135 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002136 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002137
Chris Yea52ade12020-08-27 16:49:20 -07002138 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002139 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002140 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002141 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002142 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2143 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002144 InputDeviceIdentifier identifier;
2145 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002146 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002147 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002148 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002149 mReader->pushNextDevice(mDevice);
2150 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2151 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002152 }
2153
Chris Yea52ade12020-08-27 16:49:20 -07002154 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002155 mFakeListener.clear();
2156 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002157 }
2158};
2159
2160const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002161const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002162const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002163const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2164const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002165const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2166 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002167const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002168
2169TEST_F(InputDeviceTest, ImmutableProperties) {
2170 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002171 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002172 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002173}
2174
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002175TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2176 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002177}
2178
Michael Wrightd02c5b62014-02-10 15:10:22 -08002179TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2180 // Configuration.
2181 InputReaderConfiguration config;
2182 mDevice->configure(ARBITRARY_TIME, &config, 0);
2183
2184 // Reset.
2185 mDevice->reset(ARBITRARY_TIME);
2186
2187 NotifyDeviceResetArgs resetArgs;
2188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2189 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2190 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2191
2192 // Metadata.
2193 ASSERT_TRUE(mDevice->isIgnored());
2194 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2195
2196 InputDeviceInfo info;
2197 mDevice->getDeviceInfo(&info);
2198 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002199 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002200 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2201 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2202
2203 // State queries.
2204 ASSERT_EQ(0, mDevice->getMetaState());
2205
2206 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2207 << "Ignored device should return unknown key code state.";
2208 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2209 << "Ignored device should return unknown scan code state.";
2210 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2211 << "Ignored device should return unknown switch state.";
2212
2213 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2214 uint8_t flags[2] = { 0, 1 };
2215 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2216 << "Ignored device should never mark any key codes.";
2217 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2218 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2219}
2220
2221TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2222 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002223 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002224
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002225 FakeInputMapper& mapper1 =
2226 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002227 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2228 mapper1.setMetaState(AMETA_ALT_ON);
2229 mapper1.addSupportedKeyCode(AKEYCODE_A);
2230 mapper1.addSupportedKeyCode(AKEYCODE_B);
2231 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2232 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2233 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2234 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2235 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002236
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002237 FakeInputMapper& mapper2 =
2238 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002239 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002240
2241 InputReaderConfiguration config;
2242 mDevice->configure(ARBITRARY_TIME, &config, 0);
2243
2244 String8 propertyValue;
2245 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2246 << "Device should have read configuration during configuration phase.";
2247 ASSERT_STREQ("value", propertyValue.string());
2248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002249 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2250 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002251
2252 // Reset
2253 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002254 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2255 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256
2257 NotifyDeviceResetArgs resetArgs;
2258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2259 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2260 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2261
2262 // Metadata.
2263 ASSERT_FALSE(mDevice->isIgnored());
2264 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2265
2266 InputDeviceInfo info;
2267 mDevice->getDeviceInfo(&info);
2268 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002269 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002270 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2271 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2272
2273 // State queries.
2274 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2275 << "Should query mappers and combine meta states.";
2276
2277 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2278 << "Should return unknown key code state when source not supported.";
2279 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2280 << "Should return unknown scan code state when source not supported.";
2281 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2282 << "Should return unknown switch state when source not supported.";
2283
2284 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2285 << "Should query mapper when source is supported.";
2286 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2287 << "Should query mapper when source is supported.";
2288 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2289 << "Should query mapper when source is supported.";
2290
2291 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2292 uint8_t flags[4] = { 0, 0, 0, 1 };
2293 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2294 << "Should do nothing when source is unsupported.";
2295 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2296 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2297 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2298 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2299
2300 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2301 << "Should query mapper when source is supported.";
2302 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2303 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2304 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2305 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2306
2307 // Event handling.
2308 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002309 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002310 mDevice->process(&event, 1);
2311
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002312 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2313 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002314}
2315
Arthur Hung2c9a3342019-07-23 14:18:59 +08002316// A single input device is associated with a specific display. Check that:
2317// 1. Device is disabled if the viewport corresponding to the associated display is not found
2318// 2. Device is disabled when setEnabled API is called
2319TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002320 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002321
2322 // First Configuration.
2323 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2324
2325 // Device should be enabled by default.
2326 ASSERT_TRUE(mDevice->isEnabled());
2327
2328 // Prepare associated info.
2329 constexpr uint8_t hdmi = 1;
2330 const std::string UNIQUE_ID = "local:1";
2331
2332 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2333 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2334 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2335 // Device should be disabled because it is associated with a specific display via
2336 // input port <-> display port association, but the corresponding display is not found
2337 ASSERT_FALSE(mDevice->isEnabled());
2338
2339 // Prepare displays.
2340 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002341 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2342 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002343 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2344 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2345 ASSERT_TRUE(mDevice->isEnabled());
2346
2347 // Device should be disabled after set disable.
2348 mFakePolicy->addDisabledDevice(mDevice->getId());
2349 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2350 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2351 ASSERT_FALSE(mDevice->isEnabled());
2352
2353 // Device should still be disabled even found the associated display.
2354 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2355 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2356 ASSERT_FALSE(mDevice->isEnabled());
2357}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002358
2359// --- InputMapperTest ---
2360
2361class InputMapperTest : public testing::Test {
2362protected:
2363 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002364 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365 static const int32_t DEVICE_ID;
2366 static const int32_t DEVICE_GENERATION;
2367 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002368 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002369 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002370
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002371 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002372 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002373 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002374 std::unique_ptr<InstrumentedInputReader> mReader;
2375 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002376
Chris Ye1b0c7342020-07-28 21:57:03 -07002377 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002378 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002380 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002381 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2382 mFakeListener);
2383 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384 }
2385
Chris Yea52ade12020-08-27 16:49:20 -07002386 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002387
Chris Yea52ade12020-08-27 16:49:20 -07002388 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389 mFakeListener.clear();
2390 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002391 }
2392
2393 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002394 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395 }
2396
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002397 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002398 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002399 mReader->requestRefreshConfiguration(changes);
2400 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002401 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002402 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2403 }
2404
arthurhungdcef2dc2020-08-11 14:47:50 +08002405 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2406 const std::string& location, int32_t eventHubId,
2407 Flags<InputDeviceClass> classes) {
2408 InputDeviceIdentifier identifier;
2409 identifier.name = name;
2410 identifier.location = location;
2411 std::shared_ptr<InputDevice> device =
2412 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2413 identifier);
2414 mReader->pushNextDevice(device);
2415 mFakeEventHub->addDevice(eventHubId, name, classes);
2416 mReader->loopOnce();
2417 return device;
2418 }
2419
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002420 template <class T, typename... Args>
2421 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002422 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002423 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002425 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002426 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427 }
2428
2429 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002430 int32_t orientation, const std::string& uniqueId,
2431 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002432 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2433 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002434 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2435 }
2436
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002437 void clearViewports() {
2438 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439 }
2440
arthurhungdcef2dc2020-08-11 14:47:50 +08002441 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002442 RawEvent event;
2443 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002444 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445 event.type = type;
2446 event.code = code;
2447 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002448 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002449 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450 }
2451
2452 static void assertMotionRange(const InputDeviceInfo& info,
2453 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2454 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002455 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2457 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2458 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2459 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2460 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2461 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2462 }
2463
2464 static void assertPointerCoords(const PointerCoords& coords,
2465 float x, float y, float pressure, float size,
2466 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2467 float orientation, float distance) {
2468 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2469 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2470 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2471 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2472 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2473 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2474 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2475 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2476 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2477 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2478 }
2479
Michael Wright17db18e2020-06-26 20:51:44 +01002480 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002481 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002482 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483 ASSERT_NEAR(x, actualX, 1);
2484 ASSERT_NEAR(y, actualY, 1);
2485 }
2486};
2487
2488const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002489const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002490const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2492const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002493const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2494 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002495const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496
2497// --- SwitchInputMapperTest ---
2498
2499class SwitchInputMapperTest : public InputMapperTest {
2500protected:
2501};
2502
2503TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002504 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002505
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002506 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002507}
2508
2509TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002510 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002512 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002513 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002515 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002516 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002517}
2518
2519TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002520 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002522 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2523 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2524 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2525 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002526
2527 NotifySwitchArgs args;
2528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2529 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002530 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2531 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002532 args.switchMask);
2533 ASSERT_EQ(uint32_t(0), args.policyFlags);
2534}
2535
Chris Ye87143712020-11-10 05:05:58 +00002536// --- VibratorInputMapperTest ---
2537class VibratorInputMapperTest : public InputMapperTest {
2538protected:
2539 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2540};
2541
2542TEST_F(VibratorInputMapperTest, GetSources) {
2543 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2544
2545 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2546}
2547
2548TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2549 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2550
2551 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2552}
2553
2554TEST_F(VibratorInputMapperTest, Vibrate) {
2555 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
2556 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2557
2558 VibrationElement pattern(2);
2559 VibrationSequence sequence(2);
2560 pattern.duration = std::chrono::milliseconds(200);
2561 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2562 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2563 sequence.addElement(pattern);
2564 pattern.duration = std::chrono::milliseconds(500);
2565 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2566 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2567 sequence.addElement(pattern);
2568
2569 std::vector<int64_t> timings = {0, 1};
2570 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2571
2572 ASSERT_FALSE(mapper.isVibrating());
2573 mapper.vibrate(sequence, -1 /* repeat */, 0 /* token */);
2574 ASSERT_TRUE(mapper.isVibrating());
2575}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
2577// --- KeyboardInputMapperTest ---
2578
2579class KeyboardInputMapperTest : public InputMapperTest {
2580protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002581 const std::string UNIQUE_ID = "local:0";
2582
2583 void prepareDisplay(int32_t orientation);
2584
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002585 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002586 int32_t originalKeyCode, int32_t rotatedKeyCode,
2587 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002588};
2589
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002590/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2591 * orientation.
2592 */
2593void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002594 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2595 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002596}
2597
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002598void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002599 int32_t originalScanCode, int32_t originalKeyCode,
2600 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002601 NotifyKeyArgs args;
2602
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002603 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2605 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2606 ASSERT_EQ(originalScanCode, args.scanCode);
2607 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002608 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002610 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2612 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2613 ASSERT_EQ(originalScanCode, args.scanCode);
2614 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002615 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616}
2617
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002619 KeyboardInputMapper& mapper =
2620 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2621 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002623 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624}
2625
2626TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2627 const int32_t USAGE_A = 0x070004;
2628 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002629 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2630 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002631 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2632 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2633 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002635 KeyboardInputMapper& mapper =
2636 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2637 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002638 // Initial metastate to AMETA_NONE.
2639 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2640 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641
2642 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002643 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644 NotifyKeyArgs args;
2645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2646 ASSERT_EQ(DEVICE_ID, args.deviceId);
2647 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2648 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2649 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2650 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2651 ASSERT_EQ(KEY_HOME, args.scanCode);
2652 ASSERT_EQ(AMETA_NONE, args.metaState);
2653 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2654 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2655 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2656
2657 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002658 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2660 ASSERT_EQ(DEVICE_ID, args.deviceId);
2661 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2662 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2663 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2664 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2665 ASSERT_EQ(KEY_HOME, args.scanCode);
2666 ASSERT_EQ(AMETA_NONE, args.metaState);
2667 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2668 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2669 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2670
2671 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002672 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2673 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2675 ASSERT_EQ(DEVICE_ID, args.deviceId);
2676 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2677 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2678 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2679 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2680 ASSERT_EQ(0, args.scanCode);
2681 ASSERT_EQ(AMETA_NONE, args.metaState);
2682 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2683 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2684 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2685
2686 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002687 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2688 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2690 ASSERT_EQ(DEVICE_ID, args.deviceId);
2691 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2692 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2693 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2694 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2695 ASSERT_EQ(0, args.scanCode);
2696 ASSERT_EQ(AMETA_NONE, args.metaState);
2697 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2698 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2699 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2700
2701 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002702 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2703 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2705 ASSERT_EQ(DEVICE_ID, args.deviceId);
2706 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2707 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2708 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2709 ASSERT_EQ(0, args.keyCode);
2710 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2711 ASSERT_EQ(AMETA_NONE, args.metaState);
2712 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2713 ASSERT_EQ(0U, args.policyFlags);
2714 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2715
2716 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002717 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2718 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2720 ASSERT_EQ(DEVICE_ID, args.deviceId);
2721 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2722 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2723 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2724 ASSERT_EQ(0, args.keyCode);
2725 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2726 ASSERT_EQ(AMETA_NONE, args.metaState);
2727 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2728 ASSERT_EQ(0U, args.policyFlags);
2729 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2730}
2731
2732TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002733 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2734 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002735 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2736 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2737 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002739 KeyboardInputMapper& mapper =
2740 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2741 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002742
arthurhungc903df12020-08-11 15:08:42 +08002743 // Initial metastate to AMETA_NONE.
2744 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2745 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002746
2747 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002748 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749 NotifyKeyArgs args;
2750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2751 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002752 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002753 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002754
2755 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002756 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2758 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002759 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002760
2761 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002762 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2764 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002765 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766
2767 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002768 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2770 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002771 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002772 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002773}
2774
2775TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002776 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2777 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2778 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2779 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002781 KeyboardInputMapper& mapper =
2782 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2783 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002785 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2787 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2788 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2789 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2790 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2791 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2792 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2793 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2794}
2795
2796TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002797 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2798 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2799 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2800 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002801
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002803 KeyboardInputMapper& mapper =
2804 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2805 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002807 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002808 ASSERT_NO_FATAL_FAILURE(
2809 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2810 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2811 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2812 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2813 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2814 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2815 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002817 clearViewports();
2818 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002819 ASSERT_NO_FATAL_FAILURE(
2820 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2821 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2822 AKEYCODE_DPAD_UP, DISPLAY_ID));
2823 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2824 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2825 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2826 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002828 clearViewports();
2829 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002830 ASSERT_NO_FATAL_FAILURE(
2831 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2832 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2833 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2834 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2835 AKEYCODE_DPAD_UP, DISPLAY_ID));
2836 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2837 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002839 clearViewports();
2840 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002841 ASSERT_NO_FATAL_FAILURE(
2842 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2843 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2844 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2845 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2846 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2847 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2848 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849
2850 // Special case: if orientation changes while key is down, we still emit the same keycode
2851 // in the key up as we did in the key down.
2852 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002853 clearViewports();
2854 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002855 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2857 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2858 ASSERT_EQ(KEY_UP, args.scanCode);
2859 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2860
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002861 clearViewports();
2862 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002863 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2865 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2866 ASSERT_EQ(KEY_UP, args.scanCode);
2867 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2868}
2869
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002870TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2871 // If the keyboard is not orientation aware,
2872 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002873 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002875 KeyboardInputMapper& mapper =
2876 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2877 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002878 NotifyKeyArgs args;
2879
2880 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002881 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002883 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2885 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2886
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002887 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002888 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002890 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2892 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2893}
2894
2895TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2896 // If the keyboard is orientation aware,
2897 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002898 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002899
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002900 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002901 KeyboardInputMapper& mapper =
2902 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2903 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002904 NotifyKeyArgs args;
2905
2906 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2907 // ^--- already checked by the previous test
2908
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002909 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002910 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002911 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002913 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2915 ASSERT_EQ(DISPLAY_ID, args.displayId);
2916
2917 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002918 clearViewports();
2919 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002920 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002921 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002923 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2925 ASSERT_EQ(newDisplayId, args.displayId);
2926}
2927
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002929 KeyboardInputMapper& mapper =
2930 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2931 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002932
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002933 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002934 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002935
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002936 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002937 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938}
2939
2940TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002941 KeyboardInputMapper& mapper =
2942 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2943 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002945 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002946 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002948 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002949 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950}
2951
2952TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002953 KeyboardInputMapper& mapper =
2954 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2955 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002956
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002957 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958
2959 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2960 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002961 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962 ASSERT_TRUE(flags[0]);
2963 ASSERT_FALSE(flags[1]);
2964}
2965
2966TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002967 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2968 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2969 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2970 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2971 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2972 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002973
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002974 KeyboardInputMapper& mapper =
2975 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2976 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07002977 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08002978 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2979 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980
2981 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002982 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2983 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2984 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985
2986 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002987 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2988 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002989 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2990 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2991 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002992 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993
2994 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002995 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2996 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002997 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2998 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2999 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003000 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001
3002 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003003 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3004 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003005 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3006 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3007 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003008 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009
3010 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003011 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3012 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003013 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3014 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3015 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003016 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003017
3018 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003019 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3020 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003021 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3022 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3023 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003024 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025
3026 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003027 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3028 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003029 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3030 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3031 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003032 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003033}
3034
Chris Yea52ade12020-08-27 16:49:20 -07003035TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3036 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3037 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3038 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3039 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3040
3041 KeyboardInputMapper& mapper =
3042 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3043 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3044
3045 // Initial metastate should be AMETA_NONE as no meta keys added.
3046 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3047 // Meta state should be AMETA_NONE after reset
3048 mapper.reset(ARBITRARY_TIME);
3049 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3050 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3051 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3052 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3053
3054 NotifyKeyArgs args;
3055 // Press button "A"
3056 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
3057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3058 ASSERT_EQ(AMETA_NONE, args.metaState);
3059 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3060 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3061 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3062
3063 // Button up.
3064 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
3065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3066 ASSERT_EQ(AMETA_NONE, args.metaState);
3067 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3068 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3069 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3070}
3071
Arthur Hung2c9a3342019-07-23 14:18:59 +08003072TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3073 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003074 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3075 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3076 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3077 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003078
3079 // keyboard 2.
3080 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003081 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003082 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003083 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003084 std::shared_ptr<InputDevice> device2 =
3085 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3086 Flags<InputDeviceClass>(0));
3087
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003088 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3089 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3090 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3091 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003093 KeyboardInputMapper& mapper =
3094 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3095 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003097 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003098 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003100 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3101 device2->reset(ARBITRARY_TIME);
3102
3103 // Prepared displays and associated info.
3104 constexpr uint8_t hdmi1 = 0;
3105 constexpr uint8_t hdmi2 = 1;
3106 const std::string SECONDARY_UNIQUE_ID = "local:1";
3107
3108 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3109 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3110
3111 // No associated display viewport found, should disable the device.
3112 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3113 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3114 ASSERT_FALSE(device2->isEnabled());
3115
3116 // Prepare second display.
3117 constexpr int32_t newDisplayId = 2;
3118 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003119 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003120 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003121 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003122 // Default device will reconfigure above, need additional reconfiguration for another device.
3123 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3124 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3125
3126 // Device should be enabled after the associated display is found.
3127 ASSERT_TRUE(mDevice->isEnabled());
3128 ASSERT_TRUE(device2->isEnabled());
3129
3130 // Test pad key events
3131 ASSERT_NO_FATAL_FAILURE(
3132 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3133 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3134 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3135 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3136 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3137 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3138 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3139
3140 ASSERT_NO_FATAL_FAILURE(
3141 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3142 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3143 AKEYCODE_DPAD_RIGHT, newDisplayId));
3144 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3145 AKEYCODE_DPAD_DOWN, newDisplayId));
3146 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3147 AKEYCODE_DPAD_LEFT, newDisplayId));
3148}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149
arthurhungc903df12020-08-11 15:08:42 +08003150TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3151 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3152 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3153 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3154 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3155 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3156 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3157
3158 KeyboardInputMapper& mapper =
3159 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3160 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3161 // Initial metastate to AMETA_NONE.
3162 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3163 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3164
3165 // Initialization should have turned all of the lights off.
3166 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3167 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3168 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3169
3170 // Toggle caps lock on.
3171 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3172 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3173 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3174 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3175
3176 // Toggle num lock on.
3177 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3178 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3179 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3180 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3181
3182 // Toggle scroll lock on.
3183 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3184 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3185 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3186 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3187
3188 mFakeEventHub->removeDevice(EVENTHUB_ID);
3189 mReader->loopOnce();
3190
3191 // keyboard 2 should default toggle keys.
3192 const std::string USB2 = "USB2";
3193 const std::string DEVICE_NAME2 = "KEYBOARD2";
3194 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3195 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3196 std::shared_ptr<InputDevice> device2 =
3197 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3198 Flags<InputDeviceClass>(0));
3199 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3200 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3201 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3202 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3203 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3204 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3205
arthurhung6fe95782020-10-05 22:41:16 +08003206 KeyboardInputMapper& mapper2 =
3207 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3208 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003209 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3210 device2->reset(ARBITRARY_TIME);
3211
3212 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3213 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3214 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003215 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3216 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003217}
3218
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003219// --- KeyboardInputMapperTest_ExternalDevice ---
3220
3221class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3222protected:
Chris Yea52ade12020-08-27 16:49:20 -07003223 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003224};
3225
3226TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003227 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3228 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003229
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003230 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3231 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3232 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3233 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003234
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003235 KeyboardInputMapper& mapper =
3236 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3237 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003238
3239 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3240 NotifyKeyArgs args;
3241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3242 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3243
3244 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3246 ASSERT_EQ(uint32_t(0), args.policyFlags);
3247
3248 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3250 ASSERT_EQ(uint32_t(0), args.policyFlags);
3251
3252 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3254 ASSERT_EQ(uint32_t(0), args.policyFlags);
3255
3256 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3258 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3259
3260 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3262 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3263}
3264
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003265TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003266 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003267
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003268 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3269 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3270 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003271
Powei Fengd041c5d2019-05-03 17:11:33 -07003272 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 KeyboardInputMapper& mapper =
3274 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3275 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003276
3277 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3278 NotifyKeyArgs args;
3279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3280 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3281
3282 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3284 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3285
3286 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3288 ASSERT_EQ(uint32_t(0), args.policyFlags);
3289
3290 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3292 ASSERT_EQ(uint32_t(0), args.policyFlags);
3293
3294 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3296 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3297
3298 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3300 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3301}
3302
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303// --- CursorInputMapperTest ---
3304
3305class CursorInputMapperTest : public InputMapperTest {
3306protected:
3307 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3308
Michael Wright17db18e2020-06-26 20:51:44 +01003309 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310
Chris Yea52ade12020-08-27 16:49:20 -07003311 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 InputMapperTest::SetUp();
3313
Michael Wright17db18e2020-06-26 20:51:44 +01003314 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003315 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316 }
3317
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003318 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3319 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003320
3321 void prepareDisplay(int32_t orientation) {
3322 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003323 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003324 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3325 orientation, uniqueId, NO_PORT, viewportType);
3326 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327};
3328
3329const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3330
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003331void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3332 int32_t originalY, int32_t rotatedX,
3333 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334 NotifyMotionArgs args;
3335
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003336 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3337 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3338 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3342 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3343 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3344 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3345}
3346
3347TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003349 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003351 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352}
3353
3354TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003356 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003358 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359}
3360
3361TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003363 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364
3365 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003366 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367
3368 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003369 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3370 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3372 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3373
3374 // When the bounds are set, then there should be a valid motion range.
3375 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3376
3377 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003378 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379
3380 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3381 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3382 1, 800 - 1, 0.0f, 0.0f));
3383 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3384 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3385 2, 480 - 1, 0.0f, 0.0f));
3386 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3387 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3388 0.0f, 1.0f, 0.0f, 0.0f));
3389}
3390
3391TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003393 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394
3395 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003396 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397
3398 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3399 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3400 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3401 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3402 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3403 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3404 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3405 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3406 0.0f, 1.0f, 0.0f, 0.0f));
3407}
3408
3409TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003411 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412
arthurhungdcef2dc2020-08-11 14:47:50 +08003413 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414
3415 NotifyMotionArgs args;
3416
3417 // Button press.
3418 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003419 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3420 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3422 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3423 ASSERT_EQ(DEVICE_ID, args.deviceId);
3424 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3425 ASSERT_EQ(uint32_t(0), args.policyFlags);
3426 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3427 ASSERT_EQ(0, args.flags);
3428 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3429 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3430 ASSERT_EQ(0, args.edgeFlags);
3431 ASSERT_EQ(uint32_t(1), args.pointerCount);
3432 ASSERT_EQ(0, args.pointerProperties[0].id);
3433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3435 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3436 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3437 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3438 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3439
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3441 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3442 ASSERT_EQ(DEVICE_ID, args.deviceId);
3443 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3444 ASSERT_EQ(uint32_t(0), args.policyFlags);
3445 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3446 ASSERT_EQ(0, args.flags);
3447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3448 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3449 ASSERT_EQ(0, args.edgeFlags);
3450 ASSERT_EQ(uint32_t(1), args.pointerCount);
3451 ASSERT_EQ(0, args.pointerProperties[0].id);
3452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3454 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3455 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3456 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3457 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3458
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003460 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3461 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3463 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3464 ASSERT_EQ(DEVICE_ID, args.deviceId);
3465 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3466 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003467 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3468 ASSERT_EQ(0, args.flags);
3469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3470 ASSERT_EQ(0, args.buttonState);
3471 ASSERT_EQ(0, args.edgeFlags);
3472 ASSERT_EQ(uint32_t(1), args.pointerCount);
3473 ASSERT_EQ(0, args.pointerProperties[0].id);
3474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3476 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3477 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3478 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3479 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3480
3481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3482 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3483 ASSERT_EQ(DEVICE_ID, args.deviceId);
3484 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3485 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3487 ASSERT_EQ(0, args.flags);
3488 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3489 ASSERT_EQ(0, args.buttonState);
3490 ASSERT_EQ(0, args.edgeFlags);
3491 ASSERT_EQ(uint32_t(1), args.pointerCount);
3492 ASSERT_EQ(0, args.pointerProperties[0].id);
3493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3495 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3496 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3497 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3498 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3499}
3500
3501TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003502 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003503 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504
3505 NotifyMotionArgs args;
3506
3507 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003508 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3509 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3513 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3514
3515 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003516 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3517 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3521 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3522}
3523
3524TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527
3528 NotifyMotionArgs args;
3529
3530 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003531 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3532 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3534 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3536 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3537
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3539 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3541 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3542
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003544 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3545 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003547 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3549 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3550
3551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3554 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3555}
3556
3557TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003559 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003560
3561 NotifyMotionArgs args;
3562
3563 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003564 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3565 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3566 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3567 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3569 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3571 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3572 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3573
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3575 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3577 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3578 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3579
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003581 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3582 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3583 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3587 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3588 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3589
3590 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003591 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3592 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003594 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3596 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3597
3598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003599 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3601 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3602}
3603
3604TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003606 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003608 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3610 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3611 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3612 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3613 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3614 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3615 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3616 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3617}
3618
3619TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 addConfigurationProperty("cursor.mode", "navigation");
3621 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003622 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003624 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3626 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3627 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3628 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3629 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3630 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3631 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3632 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3633
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003634 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3636 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3637 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3638 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3639 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3640 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3641 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3642 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3643
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003644 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3646 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3647 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3648 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3649 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3650 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3651 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3652 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3653
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003654 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003655 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3656 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3657 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3658 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3659 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3660 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3661 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3662 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3663}
3664
3665TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003667 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668
3669 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3670 mFakePointerController->setPosition(100, 200);
3671 mFakePointerController->setButtonState(0);
3672
3673 NotifyMotionArgs motionArgs;
3674 NotifyKeyArgs keyArgs;
3675
3676 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003677 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3678 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3680 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3681 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3682 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3684 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3685
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3688 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3689 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3691 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3692
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003693 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3694 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003696 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697 ASSERT_EQ(0, motionArgs.buttonState);
3698 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3700 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3701
3702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003703 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704 ASSERT_EQ(0, motionArgs.buttonState);
3705 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3707 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3708
3709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003711 ASSERT_EQ(0, motionArgs.buttonState);
3712 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3714 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3715
3716 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003717 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3718 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3719 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3721 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3722 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3723 motionArgs.buttonState);
3724 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3725 mFakePointerController->getButtonState());
3726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3727 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3728
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3731 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3732 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3733 mFakePointerController->getButtonState());
3734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3735 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3736
3737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3739 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3740 motionArgs.buttonState);
3741 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3742 mFakePointerController->getButtonState());
3743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3744 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3745
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003746 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3747 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003749 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3751 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3753 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3754
3755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003757 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3758 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3760 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3761
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003762 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3763 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003765 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3766 ASSERT_EQ(0, motionArgs.buttonState);
3767 ASSERT_EQ(0, mFakePointerController->getButtonState());
3768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3769 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003770 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3771 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003772
3773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003774 ASSERT_EQ(0, motionArgs.buttonState);
3775 ASSERT_EQ(0, mFakePointerController->getButtonState());
3776 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3778 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003779
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3781 ASSERT_EQ(0, motionArgs.buttonState);
3782 ASSERT_EQ(0, mFakePointerController->getButtonState());
3783 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3785 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3786
3787 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003788 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3789 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3791 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3792 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003793
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003795 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3797 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3799 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3800
3801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3802 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3803 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3804 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3806 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3807
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003808 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3809 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812 ASSERT_EQ(0, motionArgs.buttonState);
3813 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3815 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3816
3817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003819 ASSERT_EQ(0, motionArgs.buttonState);
3820 ASSERT_EQ(0, mFakePointerController->getButtonState());
3821
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3823 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3825 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3826 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3827
3828 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003829 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3830 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3832 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3833 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003834
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3838 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3840 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3841
3842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3844 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3845 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3847 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3848
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003849 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3850 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853 ASSERT_EQ(0, motionArgs.buttonState);
3854 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3856 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003857
3858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3859 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3860 ASSERT_EQ(0, motionArgs.buttonState);
3861 ASSERT_EQ(0, mFakePointerController->getButtonState());
3862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3863 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3864
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3866 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3867 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3868
3869 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003870 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3871 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3873 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3874 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003875
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003877 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3879 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3881 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3882
3883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3884 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3885 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3886 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3888 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3889
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003890 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3891 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003894 ASSERT_EQ(0, motionArgs.buttonState);
3895 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3897 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003898
3899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3900 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3901 ASSERT_EQ(0, motionArgs.buttonState);
3902 ASSERT_EQ(0, mFakePointerController->getButtonState());
3903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3904 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3905
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3907 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3908 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3909
3910 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003911 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3912 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3914 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3915 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003916
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003918 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3920 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3922 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3923
3924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3925 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3926 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3927 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3929 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3930
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003931 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3932 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935 ASSERT_EQ(0, motionArgs.buttonState);
3936 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3938 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003939
3940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3941 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3942 ASSERT_EQ(0, motionArgs.buttonState);
3943 ASSERT_EQ(0, mFakePointerController->getButtonState());
3944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3945 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3946
Michael Wrightd02c5b62014-02-10 15:10:22 -08003947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3948 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3949 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3950}
3951
3952TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003954 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955
3956 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3957 mFakePointerController->setPosition(100, 200);
3958 mFakePointerController->setButtonState(0);
3959
3960 NotifyMotionArgs args;
3961
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003962 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3963 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3964 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003966 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3967 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3969 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 +01003970 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003971}
3972
3973TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003974 addConfigurationProperty("cursor.mode", "pointer");
3975 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003976 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003977
3978 NotifyDeviceResetArgs resetArgs;
3979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3980 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3981 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3982
3983 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3984 mFakePointerController->setPosition(100, 200);
3985 mFakePointerController->setButtonState(0);
3986
3987 NotifyMotionArgs args;
3988
3989 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003990 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3991 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3992 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3994 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3997 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 +01003998 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003999
4000 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004001 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
4002 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4004 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4005 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4007 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4009 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4010 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4012 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4013
4014 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004015 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
4016 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4018 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4019 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4021 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4023 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4024 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4026 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4027
4028 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004029 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
4030 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
4031 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4033 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4036 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 +01004037 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004038
4039 // Disable pointer capture and check that the device generation got bumped
4040 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004041 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004042 mFakePolicy->setPointerCapture(false);
4043 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004044 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004045
4046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4047 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4048 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4049
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004050 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4051 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4052 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4054 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004055 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4057 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 +01004058 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059}
4060
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004061TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004062 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004063
Garfield Tan888a6a42020-01-09 11:39:16 -08004064 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004065 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004066 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4067 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004068 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4069 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004070 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4071 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4072
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004073 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4074 mFakePointerController->setPosition(100, 200);
4075 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004076
4077 NotifyMotionArgs args;
4078 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4079 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4080 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
4081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4082 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4083 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4085 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 +01004086 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004087 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4088}
4089
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090// --- TouchInputMapperTest ---
4091
4092class TouchInputMapperTest : public InputMapperTest {
4093protected:
4094 static const int32_t RAW_X_MIN;
4095 static const int32_t RAW_X_MAX;
4096 static const int32_t RAW_Y_MIN;
4097 static const int32_t RAW_Y_MAX;
4098 static const int32_t RAW_TOUCH_MIN;
4099 static const int32_t RAW_TOUCH_MAX;
4100 static const int32_t RAW_TOOL_MIN;
4101 static const int32_t RAW_TOOL_MAX;
4102 static const int32_t RAW_PRESSURE_MIN;
4103 static const int32_t RAW_PRESSURE_MAX;
4104 static const int32_t RAW_ORIENTATION_MIN;
4105 static const int32_t RAW_ORIENTATION_MAX;
4106 static const int32_t RAW_DISTANCE_MIN;
4107 static const int32_t RAW_DISTANCE_MAX;
4108 static const int32_t RAW_TILT_MIN;
4109 static const int32_t RAW_TILT_MAX;
4110 static const int32_t RAW_ID_MIN;
4111 static const int32_t RAW_ID_MAX;
4112 static const int32_t RAW_SLOT_MIN;
4113 static const int32_t RAW_SLOT_MAX;
4114 static const float X_PRECISION;
4115 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004116 static const float X_PRECISION_VIRTUAL;
4117 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118
4119 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004120 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121
4122 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4123
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004124 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004125 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004126
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127 enum Axes {
4128 POSITION = 1 << 0,
4129 TOUCH = 1 << 1,
4130 TOOL = 1 << 2,
4131 PRESSURE = 1 << 3,
4132 ORIENTATION = 1 << 4,
4133 MINOR = 1 << 5,
4134 ID = 1 << 6,
4135 DISTANCE = 1 << 7,
4136 TILT = 1 << 8,
4137 SLOT = 1 << 9,
4138 TOOL_TYPE = 1 << 10,
4139 };
4140
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004141 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4142 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004143 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004145 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 int32_t toRawX(float displayX);
4147 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004148 float toCookedX(float rawX, float rawY);
4149 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004151 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004153 float toDisplayY(int32_t rawY, int32_t displayHeight);
4154
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155};
4156
4157const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4158const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4159const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4160const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4161const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4162const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4163const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4164const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004165const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4166const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4168const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4169const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4170const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4171const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4172const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4173const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4174const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4175const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4176const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4177const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4178const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004179const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4180 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4181const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4182 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004183const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4184 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185
4186const float TouchInputMapperTest::GEOMETRIC_SCALE =
4187 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4188 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4189
4190const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4191 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4192 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4193};
4194
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004195void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004196 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4197 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004198}
4199
4200void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4201 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4202 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203}
4204
Santos Cordonfa5cf462017-04-05 10:37:00 -07004205void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004206 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4207 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4208 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004209}
4210
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004212 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4213 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4214 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4215 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216}
4217
Jason Gerecke489fda82012-09-07 17:19:40 -07004218void TouchInputMapperTest::prepareLocationCalibration() {
4219 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4220}
4221
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222int32_t TouchInputMapperTest::toRawX(float displayX) {
4223 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4224}
4225
4226int32_t TouchInputMapperTest::toRawY(float displayY) {
4227 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4228}
4229
Jason Gerecke489fda82012-09-07 17:19:40 -07004230float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4231 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4232 return rawX;
4233}
4234
4235float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4236 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4237 return rawY;
4238}
4239
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004241 return toDisplayX(rawX, DISPLAY_WIDTH);
4242}
4243
4244float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4245 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246}
4247
4248float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004249 return toDisplayY(rawY, DISPLAY_HEIGHT);
4250}
4251
4252float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4253 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254}
4255
4256
4257// --- SingleTouchInputMapperTest ---
4258
4259class SingleTouchInputMapperTest : public TouchInputMapperTest {
4260protected:
4261 void prepareButtons();
4262 void prepareAxes(int axes);
4263
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004264 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4265 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4266 void processUp(SingleTouchInputMapper& mappery);
4267 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4268 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4269 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4270 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4271 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4272 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273};
4274
4275void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004276 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277}
4278
4279void SingleTouchInputMapperTest::prepareAxes(int axes) {
4280 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004281 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4282 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 }
4284 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004285 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4286 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287 }
4288 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004289 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4290 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 }
4292 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004293 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4294 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 }
4296 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004297 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4298 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 }
4300}
4301
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004302void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004303 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4304 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4305 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306}
4307
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004308void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004309 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4310 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311}
4312
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004313void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004314 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315}
4316
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004317void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004318 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319}
4320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004321void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4322 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004323 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324}
4325
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004326void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004327 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328}
4329
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004330void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4331 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004332 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4333 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334}
4335
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004336void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4337 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004338 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339}
4340
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004341void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004342 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343}
4344
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 prepareButtons();
4347 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004348 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004350 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351}
4352
4353TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004354 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4355 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 prepareButtons();
4357 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004358 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004360 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361}
4362
4363TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 prepareButtons();
4365 prepareAxes(POSITION);
4366 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004367 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004369 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370}
4371
4372TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 prepareButtons();
4374 prepareAxes(POSITION);
4375 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004376 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004378 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379}
4380
4381TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 addConfigurationProperty("touch.deviceType", "touchScreen");
4383 prepareDisplay(DISPLAY_ORIENTATION_0);
4384 prepareButtons();
4385 prepareAxes(POSITION);
4386 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004387 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388
4389 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004390 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391
4392 // Virtual key is down.
4393 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4394 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4395 processDown(mapper, x, y);
4396 processSync(mapper);
4397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4398
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004399 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400
4401 // Virtual key is up.
4402 processUp(mapper);
4403 processSync(mapper);
4404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4405
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004406 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407}
4408
4409TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410 addConfigurationProperty("touch.deviceType", "touchScreen");
4411 prepareDisplay(DISPLAY_ORIENTATION_0);
4412 prepareButtons();
4413 prepareAxes(POSITION);
4414 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004415 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416
4417 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004418 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419
4420 // Virtual key is down.
4421 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4422 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4423 processDown(mapper, x, y);
4424 processSync(mapper);
4425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4426
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004427 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428
4429 // Virtual key is up.
4430 processUp(mapper);
4431 processSync(mapper);
4432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4433
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004434 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435}
4436
4437TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438 addConfigurationProperty("touch.deviceType", "touchScreen");
4439 prepareDisplay(DISPLAY_ORIENTATION_0);
4440 prepareButtons();
4441 prepareAxes(POSITION);
4442 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004443 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444
4445 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4446 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004447 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_TRUE(flags[0]);
4449 ASSERT_FALSE(flags[1]);
4450}
4451
4452TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453 addConfigurationProperty("touch.deviceType", "touchScreen");
4454 prepareDisplay(DISPLAY_ORIENTATION_0);
4455 prepareButtons();
4456 prepareAxes(POSITION);
4457 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004458 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459
arthurhungdcef2dc2020-08-11 14:47:50 +08004460 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461
4462 NotifyKeyArgs args;
4463
4464 // Press virtual key.
4465 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4466 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4467 processDown(mapper, x, y);
4468 processSync(mapper);
4469
4470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4471 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4472 ASSERT_EQ(DEVICE_ID, args.deviceId);
4473 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4474 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4475 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4476 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4477 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4478 ASSERT_EQ(KEY_HOME, args.scanCode);
4479 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4480 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4481
4482 // Release virtual key.
4483 processUp(mapper);
4484 processSync(mapper);
4485
4486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4487 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4488 ASSERT_EQ(DEVICE_ID, args.deviceId);
4489 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4490 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4491 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4492 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4493 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4494 ASSERT_EQ(KEY_HOME, args.scanCode);
4495 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4496 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4497
4498 // Should not have sent any motions.
4499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4500}
4501
4502TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503 addConfigurationProperty("touch.deviceType", "touchScreen");
4504 prepareDisplay(DISPLAY_ORIENTATION_0);
4505 prepareButtons();
4506 prepareAxes(POSITION);
4507 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004508 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509
arthurhungdcef2dc2020-08-11 14:47:50 +08004510 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511
4512 NotifyKeyArgs keyArgs;
4513
4514 // Press virtual key.
4515 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4516 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4517 processDown(mapper, x, y);
4518 processSync(mapper);
4519
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4521 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4522 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4523 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4524 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4525 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4526 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4527 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4528 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4530 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4531
4532 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4533 // into the display area.
4534 y -= 100;
4535 processMove(mapper, x, y);
4536 processSync(mapper);
4537
4538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4539 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4540 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4541 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4542 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4543 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4544 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4545 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4546 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4547 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4548 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4549 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4550
4551 NotifyMotionArgs motionArgs;
4552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4553 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4554 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4555 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4556 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4557 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4558 ASSERT_EQ(0, motionArgs.flags);
4559 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4560 ASSERT_EQ(0, motionArgs.buttonState);
4561 ASSERT_EQ(0, motionArgs.edgeFlags);
4562 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4563 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4566 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4567 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4568 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4570
4571 // Keep moving out of bounds. Should generate a pointer move.
4572 y -= 50;
4573 processMove(mapper, x, y);
4574 processSync(mapper);
4575
4576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4577 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4578 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4579 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4580 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4582 ASSERT_EQ(0, motionArgs.flags);
4583 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4584 ASSERT_EQ(0, motionArgs.buttonState);
4585 ASSERT_EQ(0, motionArgs.edgeFlags);
4586 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4587 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4590 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4591 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4592 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4593 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4594
4595 // Release out of bounds. Should generate a pointer up.
4596 processUp(mapper);
4597 processSync(mapper);
4598
4599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4600 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4601 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4602 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4603 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4605 ASSERT_EQ(0, motionArgs.flags);
4606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4607 ASSERT_EQ(0, motionArgs.buttonState);
4608 ASSERT_EQ(0, motionArgs.edgeFlags);
4609 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4610 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4613 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4614 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4615 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4616 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4617
4618 // Should not have sent any more keys or motions.
4619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4621}
4622
4623TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 addConfigurationProperty("touch.deviceType", "touchScreen");
4625 prepareDisplay(DISPLAY_ORIENTATION_0);
4626 prepareButtons();
4627 prepareAxes(POSITION);
4628 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004629 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630
arthurhungdcef2dc2020-08-11 14:47:50 +08004631 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632
4633 NotifyMotionArgs motionArgs;
4634
4635 // Initially go down out of bounds.
4636 int32_t x = -10;
4637 int32_t y = -10;
4638 processDown(mapper, x, y);
4639 processSync(mapper);
4640
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4642
4643 // Move into the display area. Should generate a pointer down.
4644 x = 50;
4645 y = 75;
4646 processMove(mapper, x, y);
4647 processSync(mapper);
4648
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4650 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4651 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4652 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4653 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4654 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4655 ASSERT_EQ(0, motionArgs.flags);
4656 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4657 ASSERT_EQ(0, motionArgs.buttonState);
4658 ASSERT_EQ(0, motionArgs.edgeFlags);
4659 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4660 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4663 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4664 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4665 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4666 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4667
4668 // Release. Should generate a pointer up.
4669 processUp(mapper);
4670 processSync(mapper);
4671
4672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4673 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4674 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4675 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4676 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4677 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4678 ASSERT_EQ(0, motionArgs.flags);
4679 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4680 ASSERT_EQ(0, motionArgs.buttonState);
4681 ASSERT_EQ(0, motionArgs.edgeFlags);
4682 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4683 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4686 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4687 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4688 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4689 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4690
4691 // Should not have sent any more keys or motions.
4692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4694}
4695
Santos Cordonfa5cf462017-04-05 10:37:00 -07004696TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004697 addConfigurationProperty("touch.deviceType", "touchScreen");
4698 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4699
4700 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4701 prepareButtons();
4702 prepareAxes(POSITION);
4703 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004704 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004705
arthurhungdcef2dc2020-08-11 14:47:50 +08004706 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004707
4708 NotifyMotionArgs motionArgs;
4709
4710 // Down.
4711 int32_t x = 100;
4712 int32_t y = 125;
4713 processDown(mapper, x, y);
4714 processSync(mapper);
4715
4716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4717 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4718 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4719 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4720 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4721 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4722 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4723 ASSERT_EQ(0, motionArgs.flags);
4724 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4725 ASSERT_EQ(0, motionArgs.buttonState);
4726 ASSERT_EQ(0, motionArgs.edgeFlags);
4727 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4728 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4731 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4732 1, 0, 0, 0, 0, 0, 0, 0));
4733 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4734 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4735 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4736
4737 // Move.
4738 x += 50;
4739 y += 75;
4740 processMove(mapper, x, y);
4741 processSync(mapper);
4742
4743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4745 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4746 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4747 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4748 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4750 ASSERT_EQ(0, motionArgs.flags);
4751 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4752 ASSERT_EQ(0, motionArgs.buttonState);
4753 ASSERT_EQ(0, motionArgs.edgeFlags);
4754 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4755 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4758 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4759 1, 0, 0, 0, 0, 0, 0, 0));
4760 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4761 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4762 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4763
4764 // Up.
4765 processUp(mapper);
4766 processSync(mapper);
4767
4768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4769 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4770 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4771 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4772 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4773 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4774 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4775 ASSERT_EQ(0, motionArgs.flags);
4776 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4777 ASSERT_EQ(0, motionArgs.buttonState);
4778 ASSERT_EQ(0, motionArgs.edgeFlags);
4779 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4780 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4783 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4784 1, 0, 0, 0, 0, 0, 0, 0));
4785 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4786 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4787 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4788
4789 // Should not have sent any more keys or motions.
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4792}
4793
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 addConfigurationProperty("touch.deviceType", "touchScreen");
4796 prepareDisplay(DISPLAY_ORIENTATION_0);
4797 prepareButtons();
4798 prepareAxes(POSITION);
4799 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004800 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801
arthurhungdcef2dc2020-08-11 14:47:50 +08004802 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803
4804 NotifyMotionArgs motionArgs;
4805
4806 // Down.
4807 int32_t x = 100;
4808 int32_t y = 125;
4809 processDown(mapper, x, y);
4810 processSync(mapper);
4811
4812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4813 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4814 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4815 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4816 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4817 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4818 ASSERT_EQ(0, motionArgs.flags);
4819 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4820 ASSERT_EQ(0, motionArgs.buttonState);
4821 ASSERT_EQ(0, motionArgs.edgeFlags);
4822 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4823 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4826 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4827 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4828 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4830
4831 // Move.
4832 x += 50;
4833 y += 75;
4834 processMove(mapper, x, y);
4835 processSync(mapper);
4836
4837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4838 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4839 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4840 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4841 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4843 ASSERT_EQ(0, motionArgs.flags);
4844 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4845 ASSERT_EQ(0, motionArgs.buttonState);
4846 ASSERT_EQ(0, motionArgs.edgeFlags);
4847 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4848 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4851 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4852 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4853 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4854 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4855
4856 // Up.
4857 processUp(mapper);
4858 processSync(mapper);
4859
4860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4861 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4862 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4863 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4864 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4865 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4866 ASSERT_EQ(0, motionArgs.flags);
4867 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4868 ASSERT_EQ(0, motionArgs.buttonState);
4869 ASSERT_EQ(0, motionArgs.edgeFlags);
4870 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4871 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4874 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4875 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4876 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4877 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4878
4879 // Should not have sent any more keys or motions.
4880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4882}
4883
4884TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885 addConfigurationProperty("touch.deviceType", "touchScreen");
4886 prepareButtons();
4887 prepareAxes(POSITION);
4888 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004889 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890
4891 NotifyMotionArgs args;
4892
4893 // Rotation 90.
4894 prepareDisplay(DISPLAY_ORIENTATION_90);
4895 processDown(mapper, toRawX(50), toRawY(75));
4896 processSync(mapper);
4897
4898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4899 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4900 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4901
4902 processUp(mapper);
4903 processSync(mapper);
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4905}
4906
4907TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 addConfigurationProperty("touch.deviceType", "touchScreen");
4909 prepareButtons();
4910 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004911 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912
4913 NotifyMotionArgs args;
4914
4915 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004916 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917 prepareDisplay(DISPLAY_ORIENTATION_0);
4918 processDown(mapper, toRawX(50), toRawY(75));
4919 processSync(mapper);
4920
4921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4922 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4923 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4924
4925 processUp(mapper);
4926 processSync(mapper);
4927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4928
4929 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004930 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931 prepareDisplay(DISPLAY_ORIENTATION_90);
4932 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4933 processSync(mapper);
4934
4935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4936 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4937 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4938
4939 processUp(mapper);
4940 processSync(mapper);
4941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4942
4943 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004944 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945 prepareDisplay(DISPLAY_ORIENTATION_180);
4946 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4947 processSync(mapper);
4948
4949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4950 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4951 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4952
4953 processUp(mapper);
4954 processSync(mapper);
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4956
4957 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004958 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959 prepareDisplay(DISPLAY_ORIENTATION_270);
4960 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4961 processSync(mapper);
4962
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4964 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4965 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4966
4967 processUp(mapper);
4968 processSync(mapper);
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4970}
4971
4972TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973 addConfigurationProperty("touch.deviceType", "touchScreen");
4974 prepareDisplay(DISPLAY_ORIENTATION_0);
4975 prepareButtons();
4976 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004977 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978
4979 // These calculations are based on the input device calibration documentation.
4980 int32_t rawX = 100;
4981 int32_t rawY = 200;
4982 int32_t rawPressure = 10;
4983 int32_t rawToolMajor = 12;
4984 int32_t rawDistance = 2;
4985 int32_t rawTiltX = 30;
4986 int32_t rawTiltY = 110;
4987
4988 float x = toDisplayX(rawX);
4989 float y = toDisplayY(rawY);
4990 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4991 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4992 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4993 float distance = float(rawDistance);
4994
4995 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4996 float tiltScale = M_PI / 180;
4997 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4998 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4999 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5000 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5001
5002 processDown(mapper, rawX, rawY);
5003 processPressure(mapper, rawPressure);
5004 processToolMajor(mapper, rawToolMajor);
5005 processDistance(mapper, rawDistance);
5006 processTilt(mapper, rawTiltX, rawTiltY);
5007 processSync(mapper);
5008
5009 NotifyMotionArgs args;
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5012 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5013 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5014}
5015
Jason Gerecke489fda82012-09-07 17:19:40 -07005016TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005017 addConfigurationProperty("touch.deviceType", "touchScreen");
5018 prepareDisplay(DISPLAY_ORIENTATION_0);
5019 prepareLocationCalibration();
5020 prepareButtons();
5021 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005022 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005023
5024 int32_t rawX = 100;
5025 int32_t rawY = 200;
5026
5027 float x = toDisplayX(toCookedX(rawX, rawY));
5028 float y = toDisplayY(toCookedY(rawX, rawY));
5029
5030 processDown(mapper, rawX, rawY);
5031 processSync(mapper);
5032
5033 NotifyMotionArgs args;
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5036 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5037}
5038
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040 addConfigurationProperty("touch.deviceType", "touchScreen");
5041 prepareDisplay(DISPLAY_ORIENTATION_0);
5042 prepareButtons();
5043 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005044 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045
5046 NotifyMotionArgs motionArgs;
5047 NotifyKeyArgs keyArgs;
5048
5049 processDown(mapper, 100, 200);
5050 processSync(mapper);
5051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5052 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5053 ASSERT_EQ(0, motionArgs.buttonState);
5054
5055 // press BTN_LEFT, release BTN_LEFT
5056 processKey(mapper, BTN_LEFT, 1);
5057 processSync(mapper);
5058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5060 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5061
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5064 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5065
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066 processKey(mapper, BTN_LEFT, 0);
5067 processSync(mapper);
5068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005071
5072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005074 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075
5076 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5077 processKey(mapper, BTN_RIGHT, 1);
5078 processKey(mapper, BTN_MIDDLE, 1);
5079 processSync(mapper);
5080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5082 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5083 motionArgs.buttonState);
5084
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5087 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5088
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5091 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5092 motionArgs.buttonState);
5093
Michael Wrightd02c5b62014-02-10 15:10:22 -08005094 processKey(mapper, BTN_RIGHT, 0);
5095 processSync(mapper);
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005097 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005099
5100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005102 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103
5104 processKey(mapper, BTN_MIDDLE, 0);
5105 processSync(mapper);
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005109
5110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005112 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113
5114 // press BTN_BACK, release BTN_BACK
5115 processKey(mapper, BTN_BACK, 1);
5116 processSync(mapper);
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5118 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5119 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005120
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005123 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5124
5125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5127 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128
5129 processKey(mapper, BTN_BACK, 0);
5130 processSync(mapper);
5131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005133 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005134
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005137 ASSERT_EQ(0, motionArgs.buttonState);
5138
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5140 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5141 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5142
5143 // press BTN_SIDE, release BTN_SIDE
5144 processKey(mapper, BTN_SIDE, 1);
5145 processSync(mapper);
5146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5147 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5148 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005149
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005152 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5153
5154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5156 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157
5158 processKey(mapper, BTN_SIDE, 0);
5159 processSync(mapper);
5160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005162 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005163
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005166 ASSERT_EQ(0, motionArgs.buttonState);
5167
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5169 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5170 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5171
5172 // press BTN_FORWARD, release BTN_FORWARD
5173 processKey(mapper, BTN_FORWARD, 1);
5174 processSync(mapper);
5175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5176 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5177 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005178
Michael Wrightd02c5b62014-02-10 15:10:22 -08005179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005181 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5182
5183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5185 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005186
5187 processKey(mapper, BTN_FORWARD, 0);
5188 processSync(mapper);
5189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005191 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005192
5193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005195 ASSERT_EQ(0, motionArgs.buttonState);
5196
Michael Wrightd02c5b62014-02-10 15:10:22 -08005197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5198 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5199 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5200
5201 // press BTN_EXTRA, release BTN_EXTRA
5202 processKey(mapper, BTN_EXTRA, 1);
5203 processSync(mapper);
5204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5205 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5206 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005207
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005210 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5211
5212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5214 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215
5216 processKey(mapper, BTN_EXTRA, 0);
5217 processSync(mapper);
5218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005221
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005224 ASSERT_EQ(0, motionArgs.buttonState);
5225
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5227 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5228 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5229
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5231
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232 // press BTN_STYLUS, release BTN_STYLUS
5233 processKey(mapper, BTN_STYLUS, 1);
5234 processSync(mapper);
5235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005237 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5238
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5241 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242
5243 processKey(mapper, BTN_STYLUS, 0);
5244 processSync(mapper);
5245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005248
5249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005251 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252
5253 // press BTN_STYLUS2, release BTN_STYLUS2
5254 processKey(mapper, BTN_STYLUS2, 1);
5255 processSync(mapper);
5256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005258 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5259
5260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5262 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005263
5264 processKey(mapper, BTN_STYLUS2, 0);
5265 processSync(mapper);
5266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005269
5270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005272 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273
5274 // release touch
5275 processUp(mapper);
5276 processSync(mapper);
5277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5278 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5279 ASSERT_EQ(0, motionArgs.buttonState);
5280}
5281
5282TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005283 addConfigurationProperty("touch.deviceType", "touchScreen");
5284 prepareDisplay(DISPLAY_ORIENTATION_0);
5285 prepareButtons();
5286 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005287 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288
5289 NotifyMotionArgs motionArgs;
5290
5291 // default tool type is finger
5292 processDown(mapper, 100, 200);
5293 processSync(mapper);
5294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5297
5298 // eraser
5299 processKey(mapper, BTN_TOOL_RUBBER, 1);
5300 processSync(mapper);
5301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5304
5305 // stylus
5306 processKey(mapper, BTN_TOOL_RUBBER, 0);
5307 processKey(mapper, BTN_TOOL_PEN, 1);
5308 processSync(mapper);
5309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5312
5313 // brush
5314 processKey(mapper, BTN_TOOL_PEN, 0);
5315 processKey(mapper, BTN_TOOL_BRUSH, 1);
5316 processSync(mapper);
5317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5320
5321 // pencil
5322 processKey(mapper, BTN_TOOL_BRUSH, 0);
5323 processKey(mapper, BTN_TOOL_PENCIL, 1);
5324 processSync(mapper);
5325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5328
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005329 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330 processKey(mapper, BTN_TOOL_PENCIL, 0);
5331 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5332 processSync(mapper);
5333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5336
5337 // mouse
5338 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5339 processKey(mapper, BTN_TOOL_MOUSE, 1);
5340 processSync(mapper);
5341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5344
5345 // lens
5346 processKey(mapper, BTN_TOOL_MOUSE, 0);
5347 processKey(mapper, BTN_TOOL_LENS, 1);
5348 processSync(mapper);
5349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5352
5353 // double-tap
5354 processKey(mapper, BTN_TOOL_LENS, 0);
5355 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5356 processSync(mapper);
5357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5360
5361 // triple-tap
5362 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5363 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5364 processSync(mapper);
5365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5368
5369 // quad-tap
5370 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5371 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5372 processSync(mapper);
5373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5376
5377 // finger
5378 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5379 processKey(mapper, BTN_TOOL_FINGER, 1);
5380 processSync(mapper);
5381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5384
5385 // stylus trumps finger
5386 processKey(mapper, BTN_TOOL_PEN, 1);
5387 processSync(mapper);
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5391
5392 // eraser trumps stylus
5393 processKey(mapper, BTN_TOOL_RUBBER, 1);
5394 processSync(mapper);
5395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5398
5399 // mouse trumps eraser
5400 processKey(mapper, BTN_TOOL_MOUSE, 1);
5401 processSync(mapper);
5402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5405
5406 // back to default tool type
5407 processKey(mapper, BTN_TOOL_MOUSE, 0);
5408 processKey(mapper, BTN_TOOL_RUBBER, 0);
5409 processKey(mapper, BTN_TOOL_PEN, 0);
5410 processKey(mapper, BTN_TOOL_FINGER, 0);
5411 processSync(mapper);
5412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5415}
5416
5417TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418 addConfigurationProperty("touch.deviceType", "touchScreen");
5419 prepareDisplay(DISPLAY_ORIENTATION_0);
5420 prepareButtons();
5421 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005422 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005423 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424
5425 NotifyMotionArgs motionArgs;
5426
5427 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5428 processKey(mapper, BTN_TOOL_FINGER, 1);
5429 processMove(mapper, 100, 200);
5430 processSync(mapper);
5431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5434 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5435
5436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5437 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5439 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5440
5441 // move a little
5442 processMove(mapper, 150, 250);
5443 processSync(mapper);
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5445 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5447 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5448
5449 // down when BTN_TOUCH is pressed, pressure defaults to 1
5450 processKey(mapper, BTN_TOUCH, 1);
5451 processSync(mapper);
5452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5453 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5455 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5456
5457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5458 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5460 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5461
5462 // up when BTN_TOUCH is released, hover restored
5463 processKey(mapper, BTN_TOUCH, 0);
5464 processSync(mapper);
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5466 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5468 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5469
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5471 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5473 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5474
5475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5476 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5478 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5479
5480 // exit hover when pointer goes away
5481 processKey(mapper, BTN_TOOL_FINGER, 0);
5482 processSync(mapper);
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5484 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5486 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5487}
5488
5489TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490 addConfigurationProperty("touch.deviceType", "touchScreen");
5491 prepareDisplay(DISPLAY_ORIENTATION_0);
5492 prepareButtons();
5493 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005494 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495
5496 NotifyMotionArgs motionArgs;
5497
5498 // initially hovering because pressure is 0
5499 processDown(mapper, 100, 200);
5500 processPressure(mapper, 0);
5501 processSync(mapper);
5502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5503 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5505 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5506
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5508 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5510 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5511
5512 // move a little
5513 processMove(mapper, 150, 250);
5514 processSync(mapper);
5515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5516 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5518 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5519
5520 // down when pressure is non-zero
5521 processPressure(mapper, RAW_PRESSURE_MAX);
5522 processSync(mapper);
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5524 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5526 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5527
5528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5529 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5531 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5532
5533 // up when pressure becomes 0, hover restored
5534 processPressure(mapper, 0);
5535 processSync(mapper);
5536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5537 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5539 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5540
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5542 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5544 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5545
5546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5547 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5549 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5550
5551 // exit hover when pointer goes away
5552 processUp(mapper);
5553 processSync(mapper);
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5555 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5557 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5558}
5559
Michael Wrightd02c5b62014-02-10 15:10:22 -08005560// --- MultiTouchInputMapperTest ---
5561
5562class MultiTouchInputMapperTest : public TouchInputMapperTest {
5563protected:
5564 void prepareAxes(int axes);
5565
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005566 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5567 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5568 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5569 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5570 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5571 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5572 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5573 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5574 void processId(MultiTouchInputMapper& mapper, int32_t id);
5575 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5576 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5577 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5578 void processMTSync(MultiTouchInputMapper& mapper);
5579 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005580};
5581
5582void MultiTouchInputMapperTest::prepareAxes(int axes) {
5583 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005584 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5585 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586 }
5587 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005588 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5589 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005591 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5592 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593 }
5594 }
5595 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005596 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5597 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005598 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005599 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5600 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601 }
5602 }
5603 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005604 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5605 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606 }
5607 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005608 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5609 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610 }
5611 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005612 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5613 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005614 }
5615 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005616 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5617 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005618 }
5619 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005620 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5621 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005622 }
5623 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005624 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005625 }
5626}
5627
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005628void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5629 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005630 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5631 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005632}
5633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005634void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5635 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005636 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005637}
5638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005639void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5640 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005641 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005642}
5643
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005644void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005645 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005646}
5647
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005648void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005649 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005650}
5651
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005652void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5653 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005654 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655}
5656
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005657void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005658 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005659}
5660
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005661void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005662 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663}
5664
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005665void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005666 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667}
5668
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005669void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005670 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671}
5672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005673void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005674 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675}
5676
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005677void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5678 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005679 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680}
5681
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005682void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005683 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684}
5685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005686void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005687 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688}
5689
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691 addConfigurationProperty("touch.deviceType", "touchScreen");
5692 prepareDisplay(DISPLAY_ORIENTATION_0);
5693 prepareAxes(POSITION);
5694 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005695 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696
arthurhungdcef2dc2020-08-11 14:47:50 +08005697 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698
5699 NotifyMotionArgs motionArgs;
5700
5701 // Two fingers down at once.
5702 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5703 processPosition(mapper, x1, y1);
5704 processMTSync(mapper);
5705 processPosition(mapper, x2, y2);
5706 processMTSync(mapper);
5707 processSync(mapper);
5708
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5710 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5711 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5712 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5713 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5714 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5715 ASSERT_EQ(0, motionArgs.flags);
5716 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5717 ASSERT_EQ(0, motionArgs.buttonState);
5718 ASSERT_EQ(0, motionArgs.edgeFlags);
5719 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5720 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5723 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5724 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5725 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5726 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5727
5728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5729 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5730 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5731 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5732 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5733 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5734 motionArgs.action);
5735 ASSERT_EQ(0, motionArgs.flags);
5736 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5737 ASSERT_EQ(0, motionArgs.buttonState);
5738 ASSERT_EQ(0, motionArgs.edgeFlags);
5739 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5740 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5742 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5745 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5747 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5748 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5749 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5750 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5751
5752 // Move.
5753 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5754 processPosition(mapper, x1, y1);
5755 processMTSync(mapper);
5756 processPosition(mapper, x2, y2);
5757 processMTSync(mapper);
5758 processSync(mapper);
5759
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5761 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5762 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5763 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5764 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5766 ASSERT_EQ(0, motionArgs.flags);
5767 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5768 ASSERT_EQ(0, motionArgs.buttonState);
5769 ASSERT_EQ(0, motionArgs.edgeFlags);
5770 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5771 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5773 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5776 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5778 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5779 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5780 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5781 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5782
5783 // First finger up.
5784 x2 += 15; y2 -= 20;
5785 processPosition(mapper, x2, y2);
5786 processMTSync(mapper);
5787 processSync(mapper);
5788
5789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5790 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5791 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5792 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5793 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5794 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5795 motionArgs.action);
5796 ASSERT_EQ(0, motionArgs.flags);
5797 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5798 ASSERT_EQ(0, motionArgs.buttonState);
5799 ASSERT_EQ(0, motionArgs.edgeFlags);
5800 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5801 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5803 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5804 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5806 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5808 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5809 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5810 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5811 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5812
5813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5814 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5815 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5816 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5817 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5819 ASSERT_EQ(0, motionArgs.flags);
5820 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5821 ASSERT_EQ(0, motionArgs.buttonState);
5822 ASSERT_EQ(0, motionArgs.edgeFlags);
5823 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5824 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5827 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5828 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5829 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5830 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5831
5832 // Move.
5833 x2 += 20; y2 -= 25;
5834 processPosition(mapper, x2, y2);
5835 processMTSync(mapper);
5836 processSync(mapper);
5837
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5839 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5840 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5841 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5842 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5844 ASSERT_EQ(0, motionArgs.flags);
5845 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5846 ASSERT_EQ(0, motionArgs.buttonState);
5847 ASSERT_EQ(0, motionArgs.edgeFlags);
5848 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5849 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5852 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5853 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5854 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5855 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5856
5857 // New finger down.
5858 int32_t x3 = 700, y3 = 300;
5859 processPosition(mapper, x2, y2);
5860 processMTSync(mapper);
5861 processPosition(mapper, x3, y3);
5862 processMTSync(mapper);
5863 processSync(mapper);
5864
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5867 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5868 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5869 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5870 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5871 motionArgs.action);
5872 ASSERT_EQ(0, motionArgs.flags);
5873 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5874 ASSERT_EQ(0, motionArgs.buttonState);
5875 ASSERT_EQ(0, motionArgs.edgeFlags);
5876 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5877 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5879 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5882 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5884 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5885 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5886 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5887 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5888
5889 // Second finger up.
5890 x3 += 30; y3 -= 20;
5891 processPosition(mapper, x3, y3);
5892 processMTSync(mapper);
5893 processSync(mapper);
5894
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5896 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5897 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5898 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5899 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5900 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5901 motionArgs.action);
5902 ASSERT_EQ(0, motionArgs.flags);
5903 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5904 ASSERT_EQ(0, motionArgs.buttonState);
5905 ASSERT_EQ(0, motionArgs.edgeFlags);
5906 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5907 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5909 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5912 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5914 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5915 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5916 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5917 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5918
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5920 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5921 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5922 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5923 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5925 ASSERT_EQ(0, motionArgs.flags);
5926 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5927 ASSERT_EQ(0, motionArgs.buttonState);
5928 ASSERT_EQ(0, motionArgs.edgeFlags);
5929 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5930 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5933 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5934 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5935 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5936 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5937
5938 // Last finger up.
5939 processMTSync(mapper);
5940 processSync(mapper);
5941
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5944 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5945 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5946 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5947 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5948 ASSERT_EQ(0, motionArgs.flags);
5949 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5950 ASSERT_EQ(0, motionArgs.buttonState);
5951 ASSERT_EQ(0, motionArgs.edgeFlags);
5952 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5953 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5954 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5956 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5957 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5958 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5959 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5960
5961 // Should not have sent any more keys or motions.
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5964}
5965
5966TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967 addConfigurationProperty("touch.deviceType", "touchScreen");
5968 prepareDisplay(DISPLAY_ORIENTATION_0);
5969 prepareAxes(POSITION | ID);
5970 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005971 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972
arthurhungdcef2dc2020-08-11 14:47:50 +08005973 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005974
5975 NotifyMotionArgs motionArgs;
5976
5977 // Two fingers down at once.
5978 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5979 processPosition(mapper, x1, y1);
5980 processId(mapper, 1);
5981 processMTSync(mapper);
5982 processPosition(mapper, x2, y2);
5983 processId(mapper, 2);
5984 processMTSync(mapper);
5985 processSync(mapper);
5986
5987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5989 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5990 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5993 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5994
5995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5996 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5997 motionArgs.action);
5998 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5999 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6001 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6002 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6004 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6006 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6007
6008 // Move.
6009 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6010 processPosition(mapper, x1, y1);
6011 processId(mapper, 1);
6012 processMTSync(mapper);
6013 processPosition(mapper, x2, y2);
6014 processId(mapper, 2);
6015 processMTSync(mapper);
6016 processSync(mapper);
6017
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6020 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6021 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6023 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6024 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6026 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6028 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6029
6030 // First finger up.
6031 x2 += 15; y2 -= 20;
6032 processPosition(mapper, x2, y2);
6033 processId(mapper, 2);
6034 processMTSync(mapper);
6035 processSync(mapper);
6036
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6038 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6039 motionArgs.action);
6040 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6041 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6043 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6044 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6046 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6048 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6049
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6052 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6053 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6056 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6057
6058 // Move.
6059 x2 += 20; y2 -= 25;
6060 processPosition(mapper, x2, y2);
6061 processId(mapper, 2);
6062 processMTSync(mapper);
6063 processSync(mapper);
6064
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6067 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6068 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6071 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6072
6073 // New finger down.
6074 int32_t x3 = 700, y3 = 300;
6075 processPosition(mapper, x2, y2);
6076 processId(mapper, 2);
6077 processMTSync(mapper);
6078 processPosition(mapper, x3, y3);
6079 processId(mapper, 3);
6080 processMTSync(mapper);
6081 processSync(mapper);
6082
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6085 motionArgs.action);
6086 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6087 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6088 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6089 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6092 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6094 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6095
6096 // Second finger up.
6097 x3 += 30; y3 -= 20;
6098 processPosition(mapper, x3, y3);
6099 processId(mapper, 3);
6100 processMTSync(mapper);
6101 processSync(mapper);
6102
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6104 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6105 motionArgs.action);
6106 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6107 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6109 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6112 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6114 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6115
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6118 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6119 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6122 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6123
6124 // Last finger up.
6125 processMTSync(mapper);
6126 processSync(mapper);
6127
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6129 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6130 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6131 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6134 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6135
6136 // Should not have sent any more keys or motions.
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6139}
6140
6141TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006142 addConfigurationProperty("touch.deviceType", "touchScreen");
6143 prepareDisplay(DISPLAY_ORIENTATION_0);
6144 prepareAxes(POSITION | ID | SLOT);
6145 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006146 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147
arthurhungdcef2dc2020-08-11 14:47:50 +08006148 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149
6150 NotifyMotionArgs motionArgs;
6151
6152 // Two fingers down at once.
6153 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6154 processPosition(mapper, x1, y1);
6155 processId(mapper, 1);
6156 processSlot(mapper, 1);
6157 processPosition(mapper, x2, y2);
6158 processId(mapper, 2);
6159 processSync(mapper);
6160
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6162 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6163 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6164 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6167 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6168
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6170 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6171 motionArgs.action);
6172 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6173 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6175 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6178 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6180 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6181
6182 // Move.
6183 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6184 processSlot(mapper, 0);
6185 processPosition(mapper, x1, y1);
6186 processSlot(mapper, 1);
6187 processPosition(mapper, x2, y2);
6188 processSync(mapper);
6189
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6192 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6193 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6195 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6198 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6200 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6201
6202 // First finger up.
6203 x2 += 15; y2 -= 20;
6204 processSlot(mapper, 0);
6205 processId(mapper, -1);
6206 processSlot(mapper, 1);
6207 processPosition(mapper, x2, y2);
6208 processSync(mapper);
6209
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6211 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6212 motionArgs.action);
6213 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6214 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6216 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6219 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6221 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6222
6223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6225 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6226 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6229 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6230
6231 // Move.
6232 x2 += 20; y2 -= 25;
6233 processPosition(mapper, x2, y2);
6234 processSync(mapper);
6235
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6238 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6239 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6242 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6243
6244 // New finger down.
6245 int32_t x3 = 700, y3 = 300;
6246 processPosition(mapper, x2, y2);
6247 processSlot(mapper, 0);
6248 processId(mapper, 3);
6249 processPosition(mapper, x3, y3);
6250 processSync(mapper);
6251
6252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6253 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6254 motionArgs.action);
6255 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6256 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6258 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6261 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6263 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6264
6265 // Second finger up.
6266 x3 += 30; y3 -= 20;
6267 processSlot(mapper, 1);
6268 processId(mapper, -1);
6269 processSlot(mapper, 0);
6270 processPosition(mapper, x3, y3);
6271 processSync(mapper);
6272
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6274 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6275 motionArgs.action);
6276 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6277 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6279 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6282 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6284 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6285
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6288 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6289 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6292 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6293
6294 // Last finger up.
6295 processId(mapper, -1);
6296 processSync(mapper);
6297
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6299 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6300 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6301 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6304 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6305
6306 // Should not have sent any more keys or motions.
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6309}
6310
6311TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312 addConfigurationProperty("touch.deviceType", "touchScreen");
6313 prepareDisplay(DISPLAY_ORIENTATION_0);
6314 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006315 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316
6317 // These calculations are based on the input device calibration documentation.
6318 int32_t rawX = 100;
6319 int32_t rawY = 200;
6320 int32_t rawTouchMajor = 7;
6321 int32_t rawTouchMinor = 6;
6322 int32_t rawToolMajor = 9;
6323 int32_t rawToolMinor = 8;
6324 int32_t rawPressure = 11;
6325 int32_t rawDistance = 0;
6326 int32_t rawOrientation = 3;
6327 int32_t id = 5;
6328
6329 float x = toDisplayX(rawX);
6330 float y = toDisplayY(rawY);
6331 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6332 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6333 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6334 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6335 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6336 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6337 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6338 float distance = float(rawDistance);
6339
6340 processPosition(mapper, rawX, rawY);
6341 processTouchMajor(mapper, rawTouchMajor);
6342 processTouchMinor(mapper, rawTouchMinor);
6343 processToolMajor(mapper, rawToolMajor);
6344 processToolMinor(mapper, rawToolMinor);
6345 processPressure(mapper, rawPressure);
6346 processOrientation(mapper, rawOrientation);
6347 processDistance(mapper, rawDistance);
6348 processId(mapper, id);
6349 processMTSync(mapper);
6350 processSync(mapper);
6351
6352 NotifyMotionArgs args;
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6354 ASSERT_EQ(0, args.pointerProperties[0].id);
6355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6356 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6357 orientation, distance));
6358}
6359
6360TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 addConfigurationProperty("touch.deviceType", "touchScreen");
6362 prepareDisplay(DISPLAY_ORIENTATION_0);
6363 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6364 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006365 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366
6367 // These calculations are based on the input device calibration documentation.
6368 int32_t rawX = 100;
6369 int32_t rawY = 200;
6370 int32_t rawTouchMajor = 140;
6371 int32_t rawTouchMinor = 120;
6372 int32_t rawToolMajor = 180;
6373 int32_t rawToolMinor = 160;
6374
6375 float x = toDisplayX(rawX);
6376 float y = toDisplayY(rawY);
6377 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6378 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6379 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6380 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6381 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6382
6383 processPosition(mapper, rawX, rawY);
6384 processTouchMajor(mapper, rawTouchMajor);
6385 processTouchMinor(mapper, rawTouchMinor);
6386 processToolMajor(mapper, rawToolMajor);
6387 processToolMinor(mapper, rawToolMinor);
6388 processMTSync(mapper);
6389 processSync(mapper);
6390
6391 NotifyMotionArgs args;
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6394 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6395}
6396
6397TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006398 addConfigurationProperty("touch.deviceType", "touchScreen");
6399 prepareDisplay(DISPLAY_ORIENTATION_0);
6400 prepareAxes(POSITION | TOUCH | TOOL);
6401 addConfigurationProperty("touch.size.calibration", "diameter");
6402 addConfigurationProperty("touch.size.scale", "10");
6403 addConfigurationProperty("touch.size.bias", "160");
6404 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006405 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406
6407 // These calculations are based on the input device calibration documentation.
6408 // Note: We only provide a single common touch/tool value because the device is assumed
6409 // not to emit separate values for each pointer (isSummed = 1).
6410 int32_t rawX = 100;
6411 int32_t rawY = 200;
6412 int32_t rawX2 = 150;
6413 int32_t rawY2 = 250;
6414 int32_t rawTouchMajor = 5;
6415 int32_t rawToolMajor = 8;
6416
6417 float x = toDisplayX(rawX);
6418 float y = toDisplayY(rawY);
6419 float x2 = toDisplayX(rawX2);
6420 float y2 = toDisplayY(rawY2);
6421 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6422 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6423 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6424
6425 processPosition(mapper, rawX, rawY);
6426 processTouchMajor(mapper, rawTouchMajor);
6427 processToolMajor(mapper, rawToolMajor);
6428 processMTSync(mapper);
6429 processPosition(mapper, rawX2, rawY2);
6430 processTouchMajor(mapper, rawTouchMajor);
6431 processToolMajor(mapper, rawToolMajor);
6432 processMTSync(mapper);
6433 processSync(mapper);
6434
6435 NotifyMotionArgs args;
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6437 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6438
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6441 args.action);
6442 ASSERT_EQ(size_t(2), args.pointerCount);
6443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6444 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6446 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6447}
6448
6449TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450 addConfigurationProperty("touch.deviceType", "touchScreen");
6451 prepareDisplay(DISPLAY_ORIENTATION_0);
6452 prepareAxes(POSITION | TOUCH | TOOL);
6453 addConfigurationProperty("touch.size.calibration", "area");
6454 addConfigurationProperty("touch.size.scale", "43");
6455 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006456 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457
6458 // These calculations are based on the input device calibration documentation.
6459 int32_t rawX = 100;
6460 int32_t rawY = 200;
6461 int32_t rawTouchMajor = 5;
6462 int32_t rawToolMajor = 8;
6463
6464 float x = toDisplayX(rawX);
6465 float y = toDisplayY(rawY);
6466 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6467 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6468 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6469
6470 processPosition(mapper, rawX, rawY);
6471 processTouchMajor(mapper, rawTouchMajor);
6472 processToolMajor(mapper, rawToolMajor);
6473 processMTSync(mapper);
6474 processSync(mapper);
6475
6476 NotifyMotionArgs args;
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6479 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6480}
6481
6482TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483 addConfigurationProperty("touch.deviceType", "touchScreen");
6484 prepareDisplay(DISPLAY_ORIENTATION_0);
6485 prepareAxes(POSITION | PRESSURE);
6486 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6487 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006488 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006489
Michael Wrightaa449c92017-12-13 21:21:43 +00006490 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006491 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006492 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6493 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6494 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6495
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496 // These calculations are based on the input device calibration documentation.
6497 int32_t rawX = 100;
6498 int32_t rawY = 200;
6499 int32_t rawPressure = 60;
6500
6501 float x = toDisplayX(rawX);
6502 float y = toDisplayY(rawY);
6503 float pressure = float(rawPressure) * 0.01f;
6504
6505 processPosition(mapper, rawX, rawY);
6506 processPressure(mapper, rawPressure);
6507 processMTSync(mapper);
6508 processSync(mapper);
6509
6510 NotifyMotionArgs args;
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6513 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6514}
6515
6516TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006517 addConfigurationProperty("touch.deviceType", "touchScreen");
6518 prepareDisplay(DISPLAY_ORIENTATION_0);
6519 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006520 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006521
6522 NotifyMotionArgs motionArgs;
6523 NotifyKeyArgs keyArgs;
6524
6525 processId(mapper, 1);
6526 processPosition(mapper, 100, 200);
6527 processSync(mapper);
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6529 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6530 ASSERT_EQ(0, motionArgs.buttonState);
6531
6532 // press BTN_LEFT, release BTN_LEFT
6533 processKey(mapper, BTN_LEFT, 1);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6537 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6538
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6540 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6541 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6542
Michael Wrightd02c5b62014-02-10 15:10:22 -08006543 processKey(mapper, BTN_LEFT, 0);
6544 processSync(mapper);
6545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006546 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006548
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006551 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552
6553 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6554 processKey(mapper, BTN_RIGHT, 1);
6555 processKey(mapper, BTN_MIDDLE, 1);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6559 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6560 motionArgs.buttonState);
6561
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6564 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6565
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6568 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6569 motionArgs.buttonState);
6570
Michael Wrightd02c5b62014-02-10 15:10:22 -08006571 processKey(mapper, BTN_RIGHT, 0);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006574 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006575 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006576
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006579 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006580
6581 processKey(mapper, BTN_MIDDLE, 0);
6582 processSync(mapper);
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006585 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006586
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006589 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590
6591 // press BTN_BACK, release BTN_BACK
6592 processKey(mapper, BTN_BACK, 1);
6593 processSync(mapper);
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6595 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6596 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006597
Michael Wrightd02c5b62014-02-10 15:10:22 -08006598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006600 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6601
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6604 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006605
6606 processKey(mapper, BTN_BACK, 0);
6607 processSync(mapper);
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006609 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006610 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006611
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006614 ASSERT_EQ(0, motionArgs.buttonState);
6615
Michael Wrightd02c5b62014-02-10 15:10:22 -08006616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6617 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6618 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6619
6620 // press BTN_SIDE, release BTN_SIDE
6621 processKey(mapper, BTN_SIDE, 1);
6622 processSync(mapper);
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6624 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6625 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006626
Michael Wrightd02c5b62014-02-10 15:10:22 -08006627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006629 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6630
6631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6632 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6633 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006634
6635 processKey(mapper, BTN_SIDE, 0);
6636 processSync(mapper);
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006638 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006639 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006640
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006643 ASSERT_EQ(0, motionArgs.buttonState);
6644
Michael Wrightd02c5b62014-02-10 15:10:22 -08006645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6646 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6647 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6648
6649 // press BTN_FORWARD, release BTN_FORWARD
6650 processKey(mapper, BTN_FORWARD, 1);
6651 processSync(mapper);
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6653 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6654 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006655
Michael Wrightd02c5b62014-02-10 15:10:22 -08006656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006658 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6659
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6661 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6662 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006663
6664 processKey(mapper, BTN_FORWARD, 0);
6665 processSync(mapper);
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006668 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006669
6670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006672 ASSERT_EQ(0, motionArgs.buttonState);
6673
Michael Wrightd02c5b62014-02-10 15:10:22 -08006674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6675 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6676 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6677
6678 // press BTN_EXTRA, release BTN_EXTRA
6679 processKey(mapper, BTN_EXTRA, 1);
6680 processSync(mapper);
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6682 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6683 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006684
Michael Wrightd02c5b62014-02-10 15:10:22 -08006685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006687 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6688
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6691 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006692
6693 processKey(mapper, BTN_EXTRA, 0);
6694 processSync(mapper);
6695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006696 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006697 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006698
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006701 ASSERT_EQ(0, motionArgs.buttonState);
6702
Michael Wrightd02c5b62014-02-10 15:10:22 -08006703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6704 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6705 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6706
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6708
Michael Wrightd02c5b62014-02-10 15:10:22 -08006709 // press BTN_STYLUS, release BTN_STYLUS
6710 processKey(mapper, BTN_STYLUS, 1);
6711 processSync(mapper);
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006714 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6715
6716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6718 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006719
6720 processKey(mapper, BTN_STYLUS, 0);
6721 processSync(mapper);
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006723 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006724 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006725
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006728 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006729
6730 // press BTN_STYLUS2, release BTN_STYLUS2
6731 processKey(mapper, BTN_STYLUS2, 1);
6732 processSync(mapper);
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006735 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6736
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6739 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006740
6741 processKey(mapper, BTN_STYLUS2, 0);
6742 processSync(mapper);
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006745 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006746
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006749 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006750
6751 // release touch
6752 processId(mapper, -1);
6753 processSync(mapper);
6754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6755 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6756 ASSERT_EQ(0, motionArgs.buttonState);
6757}
6758
6759TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006760 addConfigurationProperty("touch.deviceType", "touchScreen");
6761 prepareDisplay(DISPLAY_ORIENTATION_0);
6762 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006763 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006764
6765 NotifyMotionArgs motionArgs;
6766
6767 // default tool type is finger
6768 processId(mapper, 1);
6769 processPosition(mapper, 100, 200);
6770 processSync(mapper);
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6772 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6774
6775 // eraser
6776 processKey(mapper, BTN_TOOL_RUBBER, 1);
6777 processSync(mapper);
6778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6781
6782 // stylus
6783 processKey(mapper, BTN_TOOL_RUBBER, 0);
6784 processKey(mapper, BTN_TOOL_PEN, 1);
6785 processSync(mapper);
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6789
6790 // brush
6791 processKey(mapper, BTN_TOOL_PEN, 0);
6792 processKey(mapper, BTN_TOOL_BRUSH, 1);
6793 processSync(mapper);
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6796 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6797
6798 // pencil
6799 processKey(mapper, BTN_TOOL_BRUSH, 0);
6800 processKey(mapper, BTN_TOOL_PENCIL, 1);
6801 processSync(mapper);
6802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6804 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6805
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006806 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006807 processKey(mapper, BTN_TOOL_PENCIL, 0);
6808 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6809 processSync(mapper);
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6813
6814 // mouse
6815 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6816 processKey(mapper, BTN_TOOL_MOUSE, 1);
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6821
6822 // lens
6823 processKey(mapper, BTN_TOOL_MOUSE, 0);
6824 processKey(mapper, BTN_TOOL_LENS, 1);
6825 processSync(mapper);
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6828 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6829
6830 // double-tap
6831 processKey(mapper, BTN_TOOL_LENS, 0);
6832 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6833 processSync(mapper);
6834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6836 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6837
6838 // triple-tap
6839 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6840 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6841 processSync(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6845
6846 // quad-tap
6847 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6848 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6849 processSync(mapper);
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6853
6854 // finger
6855 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6856 processKey(mapper, BTN_TOOL_FINGER, 1);
6857 processSync(mapper);
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6860 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6861
6862 // stylus trumps finger
6863 processKey(mapper, BTN_TOOL_PEN, 1);
6864 processSync(mapper);
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6868
6869 // eraser trumps stylus
6870 processKey(mapper, BTN_TOOL_RUBBER, 1);
6871 processSync(mapper);
6872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6875
6876 // mouse trumps eraser
6877 processKey(mapper, BTN_TOOL_MOUSE, 1);
6878 processSync(mapper);
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6881 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6882
6883 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6884 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6885 processSync(mapper);
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6889
6890 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6891 processToolType(mapper, MT_TOOL_PEN);
6892 processSync(mapper);
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6896
6897 // back to default tool type
6898 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6899 processKey(mapper, BTN_TOOL_MOUSE, 0);
6900 processKey(mapper, BTN_TOOL_RUBBER, 0);
6901 processKey(mapper, BTN_TOOL_PEN, 0);
6902 processKey(mapper, BTN_TOOL_FINGER, 0);
6903 processSync(mapper);
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6907}
6908
6909TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006910 addConfigurationProperty("touch.deviceType", "touchScreen");
6911 prepareDisplay(DISPLAY_ORIENTATION_0);
6912 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006913 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006914 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006915
6916 NotifyMotionArgs motionArgs;
6917
6918 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6919 processId(mapper, 1);
6920 processPosition(mapper, 100, 200);
6921 processSync(mapper);
6922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6923 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6925 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6926
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6928 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6930 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6931
6932 // move a little
6933 processPosition(mapper, 150, 250);
6934 processSync(mapper);
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6938 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6939
6940 // down when BTN_TOUCH is pressed, pressure defaults to 1
6941 processKey(mapper, BTN_TOUCH, 1);
6942 processSync(mapper);
6943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6944 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6946 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6947
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6949 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6951 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6952
6953 // up when BTN_TOUCH is released, hover restored
6954 processKey(mapper, BTN_TOUCH, 0);
6955 processSync(mapper);
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6957 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6959 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6960
6961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6962 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6963 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6964 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6965
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6967 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6969 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6970
6971 // exit hover when pointer goes away
6972 processId(mapper, -1);
6973 processSync(mapper);
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6975 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6977 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6978}
6979
6980TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006981 addConfigurationProperty("touch.deviceType", "touchScreen");
6982 prepareDisplay(DISPLAY_ORIENTATION_0);
6983 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006984 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006985
6986 NotifyMotionArgs motionArgs;
6987
6988 // initially hovering because pressure is 0
6989 processId(mapper, 1);
6990 processPosition(mapper, 100, 200);
6991 processPressure(mapper, 0);
6992 processSync(mapper);
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6994 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6996 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6997
6998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6999 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7001 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7002
7003 // move a little
7004 processPosition(mapper, 150, 250);
7005 processSync(mapper);
7006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7007 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7009 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7010
7011 // down when pressure becomes non-zero
7012 processPressure(mapper, RAW_PRESSURE_MAX);
7013 processSync(mapper);
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7015 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7017 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7018
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7020 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7022 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7023
7024 // up when pressure becomes 0, hover restored
7025 processPressure(mapper, 0);
7026 processSync(mapper);
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7028 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7030 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7031
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7033 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7035 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7036
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7038 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7040 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7041
7042 // exit hover when pointer goes away
7043 processId(mapper, -1);
7044 processSync(mapper);
7045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7046 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7048 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7049}
7050
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007051/**
7052 * Set the input device port <--> display port associations, and check that the
7053 * events are routed to the display that matches the display port.
7054 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7055 */
7056TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007057 const std::string usb2 = "USB2";
7058 const uint8_t hdmi1 = 0;
7059 const uint8_t hdmi2 = 1;
7060 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007061 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007062
7063 addConfigurationProperty("touch.deviceType", "touchScreen");
7064 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007065 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007066
7067 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7068 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7069
7070 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7071 // for this input device is specified, and the matching viewport is not present,
7072 // the input device should be disabled (at the mapper level).
7073
7074 // Add viewport for display 2 on hdmi2
7075 prepareSecondaryDisplay(type, hdmi2);
7076 // Send a touch event
7077 processPosition(mapper, 100, 100);
7078 processSync(mapper);
7079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7080
7081 // Add viewport for display 1 on hdmi1
7082 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7083 // Send a touch event again
7084 processPosition(mapper, 100, 100);
7085 processSync(mapper);
7086
7087 NotifyMotionArgs args;
7088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7089 ASSERT_EQ(DISPLAY_ID, args.displayId);
7090}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007092TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007093 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007094 std::shared_ptr<FakePointerController> fakePointerController =
7095 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007096 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007097 fakePointerController->setPosition(100, 200);
7098 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007099 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7100
Garfield Tan888a6a42020-01-09 11:39:16 -08007101 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007102 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007103
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007104 prepareDisplay(DISPLAY_ORIENTATION_0);
7105 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007106 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007107
7108 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007109 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007110
7111 NotifyMotionArgs motionArgs;
7112 processPosition(mapper, 100, 100);
7113 processSync(mapper);
7114
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7116 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7117 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7118}
7119
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007120/**
7121 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7122 * events should not be delivered to the listener.
7123 */
7124TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7125 addConfigurationProperty("touch.deviceType", "touchScreen");
7126 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7127 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7128 ViewportType::INTERNAL);
7129 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7130 prepareAxes(POSITION);
7131 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7132
7133 NotifyMotionArgs motionArgs;
7134 processPosition(mapper, 100, 100);
7135 processSync(mapper);
7136
7137 mFakeListener->assertNotifyMotionWasNotCalled();
7138}
7139
Arthur Hung7c645402019-01-25 17:45:42 +08007140TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7141 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007142 prepareAxes(POSITION | ID | SLOT);
7143 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007144 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007145
7146 // Create the second touch screen device, and enable multi fingers.
7147 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007148 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007149 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007150 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007151 std::shared_ptr<InputDevice> device2 =
7152 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7153 Flags<InputDeviceClass>(0));
7154
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007155 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7156 0 /*flat*/, 0 /*fuzz*/);
7157 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7158 0 /*flat*/, 0 /*fuzz*/);
7159 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7160 0 /*flat*/, 0 /*fuzz*/);
7161 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7162 0 /*flat*/, 0 /*fuzz*/);
7163 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7164 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7165 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007166
7167 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007168 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007169 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7170 device2->reset(ARBITRARY_TIME);
7171
7172 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007173 std::shared_ptr<FakePointerController> fakePointerController =
7174 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007175 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7176 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7177
7178 // Setup policy for associated displays and show touches.
7179 const uint8_t hdmi1 = 0;
7180 const uint8_t hdmi2 = 1;
7181 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7182 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7183 mFakePolicy->setShowTouches(true);
7184
7185 // Create displays.
7186 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007187 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007188
7189 // Default device will reconfigure above, need additional reconfiguration for another device.
7190 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007191 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007192
7193 // Two fingers down at default display.
7194 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7195 processPosition(mapper, x1, y1);
7196 processId(mapper, 1);
7197 processSlot(mapper, 1);
7198 processPosition(mapper, x2, y2);
7199 processId(mapper, 2);
7200 processSync(mapper);
7201
7202 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7203 fakePointerController->getSpots().find(DISPLAY_ID);
7204 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7205 ASSERT_EQ(size_t(2), iter->second.size());
7206
7207 // Two fingers down at second display.
7208 processPosition(mapper2, x1, y1);
7209 processId(mapper2, 1);
7210 processSlot(mapper2, 1);
7211 processPosition(mapper2, x2, y2);
7212 processId(mapper2, 2);
7213 processSync(mapper2);
7214
7215 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7216 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7217 ASSERT_EQ(size_t(2), iter->second.size());
7218}
7219
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007220TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007221 prepareAxes(POSITION);
7222 addConfigurationProperty("touch.deviceType", "touchScreen");
7223 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007224 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007225
7226 NotifyMotionArgs motionArgs;
7227 // Unrotated video frame
7228 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7229 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007230 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007231 processPosition(mapper, 100, 200);
7232 processSync(mapper);
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7234 ASSERT_EQ(frames, motionArgs.videoFrames);
7235
7236 // Subsequent touch events should not have any videoframes
7237 // This is implemented separately in FakeEventHub,
7238 // but that should match the behaviour of TouchVideoDevice.
7239 processPosition(mapper, 200, 200);
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7242 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7243}
7244
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007245TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007246 prepareAxes(POSITION);
7247 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007248 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007249 // Unrotated video frame
7250 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7251 NotifyMotionArgs motionArgs;
7252
7253 // Test all 4 orientations
7254 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7255 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7256 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7257 clearViewports();
7258 prepareDisplay(orientation);
7259 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007260 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007261 processPosition(mapper, 100, 200);
7262 processSync(mapper);
7263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7264 frames[0].rotate(orientation);
7265 ASSERT_EQ(frames, motionArgs.videoFrames);
7266 }
7267}
7268
7269TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007270 prepareAxes(POSITION);
7271 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007272 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007273 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7274 // so mix these.
7275 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7276 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7277 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7278 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7279 NotifyMotionArgs motionArgs;
7280
7281 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007282 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007283 processPosition(mapper, 100, 200);
7284 processSync(mapper);
7285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7286 std::for_each(frames.begin(), frames.end(),
7287 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7288 ASSERT_EQ(frames, motionArgs.videoFrames);
7289}
7290
Arthur Hung9da14732019-09-02 16:16:58 +08007291/**
7292 * If we had defined port associations, but the viewport is not ready, the touch device would be
7293 * expected to be disabled, and it should be enabled after the viewport has found.
7294 */
7295TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007296 constexpr uint8_t hdmi2 = 1;
7297 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007298 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007299
7300 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7301
7302 addConfigurationProperty("touch.deviceType", "touchScreen");
7303 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007304 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007305
7306 ASSERT_EQ(mDevice->isEnabled(), false);
7307
7308 // Add display on hdmi2, the device should be enabled and can receive touch event.
7309 prepareSecondaryDisplay(type, hdmi2);
7310 ASSERT_EQ(mDevice->isEnabled(), true);
7311
7312 // Send a touch event.
7313 processPosition(mapper, 100, 100);
7314 processSync(mapper);
7315
7316 NotifyMotionArgs args;
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7318 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7319}
7320
Arthur Hung6cd19a42019-08-30 19:04:12 +08007321
Arthur Hung6cd19a42019-08-30 19:04:12 +08007322
Arthur Hung421eb1c2020-01-16 00:09:42 +08007323TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007324 addConfigurationProperty("touch.deviceType", "touchScreen");
7325 prepareDisplay(DISPLAY_ORIENTATION_0);
7326 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007327 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007328
7329 NotifyMotionArgs motionArgs;
7330
7331 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7332 // finger down
7333 processId(mapper, 1);
7334 processPosition(mapper, x1, y1);
7335 processSync(mapper);
7336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7337 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7339
7340 // finger move
7341 processId(mapper, 1);
7342 processPosition(mapper, x2, y2);
7343 processSync(mapper);
7344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7347
7348 // finger up.
7349 processId(mapper, -1);
7350 processSync(mapper);
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7352 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7354
7355 // new finger down
7356 processId(mapper, 1);
7357 processPosition(mapper, x3, y3);
7358 processSync(mapper);
7359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7360 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7362}
7363
7364/**
arthurhungcc7f9802020-04-30 17:55:40 +08007365 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7366 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007367 */
arthurhungcc7f9802020-04-30 17:55:40 +08007368TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007369 addConfigurationProperty("touch.deviceType", "touchScreen");
7370 prepareDisplay(DISPLAY_ORIENTATION_0);
7371 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007372 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007373
7374 NotifyMotionArgs motionArgs;
7375
7376 // default tool type is finger
7377 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007378 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007379 processPosition(mapper, x1, y1);
7380 processSync(mapper);
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7384
7385 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7386 processToolType(mapper, MT_TOOL_PALM);
7387 processSync(mapper);
7388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7389 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7390
7391 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007392 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007393 processPosition(mapper, x2, y2);
7394 processSync(mapper);
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7396
7397 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007398 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007399 processSync(mapper);
7400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7401
7402 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007403 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007404 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007405 processPosition(mapper, x3, y3);
7406 processSync(mapper);
7407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7408 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7410}
7411
arthurhungbf89a482020-04-17 17:37:55 +08007412/**
arthurhungcc7f9802020-04-30 17:55:40 +08007413 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7414 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007415 */
arthurhungcc7f9802020-04-30 17:55:40 +08007416TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007417 addConfigurationProperty("touch.deviceType", "touchScreen");
7418 prepareDisplay(DISPLAY_ORIENTATION_0);
7419 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7420 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7421
7422 NotifyMotionArgs motionArgs;
7423
7424 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007425 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7426 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007427 processPosition(mapper, x1, y1);
7428 processSync(mapper);
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7432
7433 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007434 processSlot(mapper, SECOND_SLOT);
7435 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007436 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007437 processSync(mapper);
7438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7439 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7440 motionArgs.action);
7441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7442
7443 // If the tool type of the first finger changes to MT_TOOL_PALM,
7444 // we expect to receive ACTION_POINTER_UP with cancel flag.
7445 processSlot(mapper, FIRST_SLOT);
7446 processId(mapper, FIRST_TRACKING_ID);
7447 processToolType(mapper, MT_TOOL_PALM);
7448 processSync(mapper);
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7450 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7451 motionArgs.action);
7452 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7453
7454 // The following MOVE events of second finger should be processed.
7455 processSlot(mapper, SECOND_SLOT);
7456 processId(mapper, SECOND_TRACKING_ID);
7457 processPosition(mapper, x2 + 1, y2 + 1);
7458 processSync(mapper);
7459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7461 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7462
7463 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7464 // it. Second finger receive move.
7465 processSlot(mapper, FIRST_SLOT);
7466 processId(mapper, INVALID_TRACKING_ID);
7467 processSync(mapper);
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7470 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7471
7472 // Second finger keeps moving.
7473 processSlot(mapper, SECOND_SLOT);
7474 processId(mapper, SECOND_TRACKING_ID);
7475 processPosition(mapper, x2 + 2, y2 + 2);
7476 processSync(mapper);
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7479 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7480
7481 // Second finger up.
7482 processId(mapper, INVALID_TRACKING_ID);
7483 processSync(mapper);
7484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7486 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7487}
7488
7489/**
7490 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7491 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7492 */
7493TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7494 addConfigurationProperty("touch.deviceType", "touchScreen");
7495 prepareDisplay(DISPLAY_ORIENTATION_0);
7496 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7497 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7498
7499 NotifyMotionArgs motionArgs;
7500
7501 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7502 // First finger down.
7503 processId(mapper, FIRST_TRACKING_ID);
7504 processPosition(mapper, x1, y1);
7505 processSync(mapper);
7506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7507 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7509
7510 // Second finger down.
7511 processSlot(mapper, SECOND_SLOT);
7512 processId(mapper, SECOND_TRACKING_ID);
7513 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007514 processSync(mapper);
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7516 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7517 motionArgs.action);
7518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7519
arthurhungcc7f9802020-04-30 17:55:40 +08007520 // If the tool type of the first finger changes to MT_TOOL_PALM,
7521 // we expect to receive ACTION_POINTER_UP with cancel flag.
7522 processSlot(mapper, FIRST_SLOT);
7523 processId(mapper, FIRST_TRACKING_ID);
7524 processToolType(mapper, MT_TOOL_PALM);
7525 processSync(mapper);
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7527 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7528 motionArgs.action);
7529 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7530
7531 // Second finger keeps moving.
7532 processSlot(mapper, SECOND_SLOT);
7533 processId(mapper, SECOND_TRACKING_ID);
7534 processPosition(mapper, x2 + 1, y2 + 1);
7535 processSync(mapper);
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7538
7539 // second finger becomes palm, receive cancel due to only 1 finger is active.
7540 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007541 processToolType(mapper, MT_TOOL_PALM);
7542 processSync(mapper);
7543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7544 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7545
arthurhungcc7f9802020-04-30 17:55:40 +08007546 // third finger down.
7547 processSlot(mapper, THIRD_SLOT);
7548 processId(mapper, THIRD_TRACKING_ID);
7549 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007550 processPosition(mapper, x3, y3);
7551 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7553 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007555 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7556
7557 // third finger move
7558 processId(mapper, THIRD_TRACKING_ID);
7559 processPosition(mapper, x3 + 1, y3 + 1);
7560 processSync(mapper);
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7563
7564 // first finger up, third finger receive move.
7565 processSlot(mapper, FIRST_SLOT);
7566 processId(mapper, INVALID_TRACKING_ID);
7567 processSync(mapper);
7568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7570 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7571
7572 // second finger up, third finger receive move.
7573 processSlot(mapper, SECOND_SLOT);
7574 processId(mapper, INVALID_TRACKING_ID);
7575 processSync(mapper);
7576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7578 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7579
7580 // third finger up.
7581 processSlot(mapper, THIRD_SLOT);
7582 processId(mapper, INVALID_TRACKING_ID);
7583 processSync(mapper);
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7585 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7586 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7587}
7588
7589/**
7590 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7591 * and the active finger could still be allowed to receive the events
7592 */
7593TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7594 addConfigurationProperty("touch.deviceType", "touchScreen");
7595 prepareDisplay(DISPLAY_ORIENTATION_0);
7596 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7597 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7598
7599 NotifyMotionArgs motionArgs;
7600
7601 // default tool type is finger
7602 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7603 processId(mapper, FIRST_TRACKING_ID);
7604 processPosition(mapper, x1, y1);
7605 processSync(mapper);
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7607 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7609
7610 // Second finger down.
7611 processSlot(mapper, SECOND_SLOT);
7612 processId(mapper, SECOND_TRACKING_ID);
7613 processPosition(mapper, x2, y2);
7614 processSync(mapper);
7615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7616 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7617 motionArgs.action);
7618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7619
7620 // If the tool type of the second finger changes to MT_TOOL_PALM,
7621 // we expect to receive ACTION_POINTER_UP with cancel flag.
7622 processId(mapper, SECOND_TRACKING_ID);
7623 processToolType(mapper, MT_TOOL_PALM);
7624 processSync(mapper);
7625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7626 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7627 motionArgs.action);
7628 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7629
7630 // The following MOVE event should be processed.
7631 processSlot(mapper, FIRST_SLOT);
7632 processId(mapper, FIRST_TRACKING_ID);
7633 processPosition(mapper, x1 + 1, y1 + 1);
7634 processSync(mapper);
7635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7637 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7638
7639 // second finger up.
7640 processSlot(mapper, SECOND_SLOT);
7641 processId(mapper, INVALID_TRACKING_ID);
7642 processSync(mapper);
7643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7645
7646 // first finger keep moving
7647 processSlot(mapper, FIRST_SLOT);
7648 processId(mapper, FIRST_TRACKING_ID);
7649 processPosition(mapper, x1 + 2, y1 + 2);
7650 processSync(mapper);
7651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7653
7654 // first finger up.
7655 processId(mapper, INVALID_TRACKING_ID);
7656 processSync(mapper);
7657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7658 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7659 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007660}
7661
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007662// --- MultiTouchInputMapperTest_ExternalDevice ---
7663
7664class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7665protected:
Chris Yea52ade12020-08-27 16:49:20 -07007666 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007667};
7668
7669/**
7670 * Expect fallback to internal viewport if device is external and external viewport is not present.
7671 */
7672TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7673 prepareAxes(POSITION);
7674 addConfigurationProperty("touch.deviceType", "touchScreen");
7675 prepareDisplay(DISPLAY_ORIENTATION_0);
7676 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7677
7678 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7679
7680 NotifyMotionArgs motionArgs;
7681
7682 // Expect the event to be sent to the internal viewport,
7683 // because an external viewport is not present.
7684 processPosition(mapper, 100, 100);
7685 processSync(mapper);
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7687 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7688
7689 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007690 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007691 processPosition(mapper, 100, 100);
7692 processSync(mapper);
7693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7694 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7695}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007696
7697/**
7698 * Test touch should not work if outside of surface.
7699 */
7700class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7701protected:
7702 void halfDisplayToCenterHorizontal(int32_t orientation) {
7703 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007704 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007705
7706 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7707 internalViewport->orientation = orientation;
7708 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7709 internalViewport->logicalLeft = 0;
7710 internalViewport->logicalTop = 0;
7711 internalViewport->logicalRight = DISPLAY_HEIGHT;
7712 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7713
7714 internalViewport->physicalLeft = 0;
7715 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7716 internalViewport->physicalRight = DISPLAY_HEIGHT;
7717 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7718
7719 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7720 internalViewport->deviceHeight = DISPLAY_WIDTH;
7721 } else {
7722 internalViewport->logicalLeft = 0;
7723 internalViewport->logicalTop = 0;
7724 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7725 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7726
7727 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7728 internalViewport->physicalTop = 0;
7729 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7730 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7731
7732 internalViewport->deviceWidth = DISPLAY_WIDTH;
7733 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7734 }
7735
7736 mFakePolicy->updateViewport(internalViewport.value());
7737 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7738 }
7739
7740 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7741 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7742 int32_t yExpected) {
7743 // touch on outside area should not work.
7744 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7745 processSync(mapper);
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7747
7748 // touch on inside area should receive the event.
7749 NotifyMotionArgs args;
7750 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7751 processSync(mapper);
7752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7753 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7754 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7755
7756 // Reset.
7757 mapper.reset(ARBITRARY_TIME);
7758 }
7759};
7760
Siarhei Vishniakou96501612020-12-10 16:08:47 -10007761// TODO(b/175351838): Fix and enable this test
7762TEST_F(MultiTouchInputMapperTest_SurfaceRange, DISABLED_Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08007763 addConfigurationProperty("touch.deviceType", "touchScreen");
7764 prepareDisplay(DISPLAY_ORIENTATION_0);
7765 prepareAxes(POSITION);
7766 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7767
7768 // Touch on center of normal display should work.
7769 const int32_t x = DISPLAY_WIDTH / 4;
7770 const int32_t y = DISPLAY_HEIGHT / 2;
7771 processPosition(mapper, toRawX(x), toRawY(y));
7772 processSync(mapper);
7773 NotifyMotionArgs args;
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7776 0.0f, 0.0f, 0.0f, 0.0f));
7777 // Reset.
7778 mapper.reset(ARBITRARY_TIME);
7779
7780 // Let physical display be different to device, and make surface and physical could be 1:1.
7781 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7782
7783 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7784 const int32_t yExpected = y;
7785 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7786}
7787
Siarhei Vishniakou96501612020-12-10 16:08:47 -10007788// TODO(b/175351838): Fix and enable this test
7789TEST_F(MultiTouchInputMapperTest_SurfaceRange, DISABLED_Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08007790 addConfigurationProperty("touch.deviceType", "touchScreen");
7791 prepareDisplay(DISPLAY_ORIENTATION_0);
7792 prepareAxes(POSITION);
7793 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7794
7795 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7796 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7797
7798 const int32_t x = DISPLAY_WIDTH / 4;
7799 const int32_t y = DISPLAY_HEIGHT / 2;
7800
7801 // expect x/y = swap x/y then reverse y.
7802 const int32_t xExpected = y;
7803 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7804 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7805}
7806
Siarhei Vishniakou96501612020-12-10 16:08:47 -10007807// TODO(b/175351838): Fix and enable this test
7808TEST_F(MultiTouchInputMapperTest_SurfaceRange, DISABLED_Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08007809 addConfigurationProperty("touch.deviceType", "touchScreen");
7810 prepareDisplay(DISPLAY_ORIENTATION_0);
7811 prepareAxes(POSITION);
7812 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7813
7814 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7815 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7816
7817 const int32_t x = DISPLAY_WIDTH / 4;
7818 const int32_t y = DISPLAY_HEIGHT / 2;
7819
7820 // expect x/y = swap x/y then reverse x.
7821 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7822 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7823 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7824}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007825
7826TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7827 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7828 std::shared_ptr<FakePointerController> fakePointerController =
7829 std::make_shared<FakePointerController>();
7830 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7831 fakePointerController->setPosition(0, 0);
7832 fakePointerController->setButtonState(0);
7833
7834 // prepare device and capture
7835 prepareDisplay(DISPLAY_ORIENTATION_0);
7836 prepareAxes(POSITION | ID | SLOT);
7837 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7838 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7839 mFakePolicy->setPointerCapture(true);
7840 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7841 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7842
7843 // captured touchpad should be a touchpad source
7844 NotifyDeviceResetArgs resetArgs;
7845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7846 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7847
Chris Yef74dc422020-09-02 22:41:50 -07007848 InputDeviceInfo deviceInfo;
7849 mDevice->getDeviceInfo(&deviceInfo);
7850
7851 const InputDeviceInfo::MotionRange* relRangeX =
7852 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
7853 ASSERT_NE(relRangeX, nullptr);
7854 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
7855 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
7856 const InputDeviceInfo::MotionRange* relRangeY =
7857 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
7858 ASSERT_NE(relRangeY, nullptr);
7859 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
7860 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
7861
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007862 // run captured pointer tests - note that this is unscaled, so input listener events should be
7863 // identical to what the hardware sends (accounting for any
7864 // calibration).
7865 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007866 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007867 processId(mapper, 1);
7868 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7869 processKey(mapper, BTN_TOUCH, 1);
7870 processSync(mapper);
7871
7872 // expect coord[0] to contain initial location of touch 0
7873 NotifyMotionArgs args;
7874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7875 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7876 ASSERT_EQ(1U, args.pointerCount);
7877 ASSERT_EQ(0, args.pointerProperties[0].id);
7878 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7879 ASSERT_NO_FATAL_FAILURE(
7880 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7881
7882 // FINGER 1 DOWN
7883 processSlot(mapper, 1);
7884 processId(mapper, 2);
7885 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7886 processSync(mapper);
7887
7888 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007890 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7891 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007892 ASSERT_EQ(2U, args.pointerCount);
7893 ASSERT_EQ(0, args.pointerProperties[0].id);
7894 ASSERT_EQ(1, args.pointerProperties[1].id);
7895 ASSERT_NO_FATAL_FAILURE(
7896 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7897 ASSERT_NO_FATAL_FAILURE(
7898 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7899
7900 // FINGER 1 MOVE
7901 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7902 processSync(mapper);
7903
7904 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7905 // from move
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7908 ASSERT_NO_FATAL_FAILURE(
7909 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7910 ASSERT_NO_FATAL_FAILURE(
7911 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7912
7913 // FINGER 0 MOVE
7914 processSlot(mapper, 0);
7915 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7916 processSync(mapper);
7917
7918 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7921 ASSERT_NO_FATAL_FAILURE(
7922 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7923 ASSERT_NO_FATAL_FAILURE(
7924 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7925
7926 // BUTTON DOWN
7927 processKey(mapper, BTN_LEFT, 1);
7928 processSync(mapper);
7929
7930 // touchinputmapper design sends a move before button press
7931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7935
7936 // BUTTON UP
7937 processKey(mapper, BTN_LEFT, 0);
7938 processSync(mapper);
7939
7940 // touchinputmapper design sends a move after button release
7941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7942 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7945
7946 // FINGER 0 UP
7947 processId(mapper, -1);
7948 processSync(mapper);
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7950 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7951
7952 // FINGER 1 MOVE
7953 processSlot(mapper, 1);
7954 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7955 processSync(mapper);
7956
7957 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7960 ASSERT_EQ(1U, args.pointerCount);
7961 ASSERT_EQ(1, args.pointerProperties[0].id);
7962 ASSERT_NO_FATAL_FAILURE(
7963 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7964
7965 // FINGER 1 UP
7966 processId(mapper, -1);
7967 processKey(mapper, BTN_TOUCH, 0);
7968 processSync(mapper);
7969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7970 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7971
7972 // non captured touchpad should be a mouse source
7973 mFakePolicy->setPointerCapture(false);
7974 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7976 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7977}
7978
7979TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7980 std::shared_ptr<FakePointerController> fakePointerController =
7981 std::make_shared<FakePointerController>();
7982 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7983 fakePointerController->setPosition(0, 0);
7984 fakePointerController->setButtonState(0);
7985
7986 // prepare device and capture
7987 prepareDisplay(DISPLAY_ORIENTATION_0);
7988 prepareAxes(POSITION | ID | SLOT);
7989 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7990 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7991 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7992 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7993 // run uncaptured pointer tests - pushes out generic events
7994 // FINGER 0 DOWN
7995 processId(mapper, 3);
7996 processPosition(mapper, 100, 100);
7997 processKey(mapper, BTN_TOUCH, 1);
7998 processSync(mapper);
7999
8000 // start at (100,100), cursor should be at (0,0) * scale
8001 NotifyMotionArgs args;
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8003 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8004 ASSERT_NO_FATAL_FAILURE(
8005 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8006
8007 // FINGER 0 MOVE
8008 processPosition(mapper, 200, 200);
8009 processSync(mapper);
8010
8011 // compute scaling to help with touch position checking
8012 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8013 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8014 float scale =
8015 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8016
8017 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8019 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8021 0, 0, 0, 0, 0, 0, 0));
8022}
8023
8024TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8025 std::shared_ptr<FakePointerController> fakePointerController =
8026 std::make_shared<FakePointerController>();
8027
8028 prepareDisplay(DISPLAY_ORIENTATION_0);
8029 prepareAxes(POSITION | ID | SLOT);
8030 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8031 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8032 mFakePolicy->setPointerCapture(false);
8033 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8034
8035 // uncaptured touchpad should be a pointer device
8036 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8037
8038 // captured touchpad should be a touchpad device
8039 mFakePolicy->setPointerCapture(true);
8040 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8041 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8042}
8043
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044} // namespace android