blob: cd61750971bb023831a87bb815083879a430a20e [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 Vishniakou8158e7e2018-10-15 14:28:20 -0700227 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
228 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700229 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700230 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700231 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100232 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800233 }
234
Arthur Hung6cd19a42019-08-30 19:04:12 +0800235 bool updateViewport(const DisplayViewport& viewport) {
236 size_t count = mViewports.size();
237 for (size_t i = 0; i < count; i++) {
238 const DisplayViewport& currentViewport = mViewports[i];
239 if (currentViewport.displayId == viewport.displayId) {
240 mViewports[i] = viewport;
241 mConfig.setDisplayViewports(mViewports);
242 return true;
243 }
244 }
245 // no viewport found.
246 return false;
247 }
248
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100249 void addExcludedDeviceName(const std::string& deviceName) {
250 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251 }
252
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700253 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
254 mConfig.portAssociations.insert({inputPort, displayPort});
255 }
256
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000257 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700258
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000259 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700260
Michael Wright17db18e2020-06-26 20:51:44 +0100261 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
262 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263 }
264
265 const InputReaderConfiguration* getReaderConfiguration() const {
266 return &mConfig;
267 }
268
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800269 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 return mInputDevices;
271 }
272
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100273 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700274 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700275 return transform;
276 }
277
278 void setTouchAffineTransformation(const TouchAffineTransformation t) {
279 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800280 }
281
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800282 void setPointerCapture(bool enabled) {
283 mConfig.pointerCapture = enabled;
284 }
285
Arthur Hung7c645402019-01-25 17:45:42 +0800286 void setShowTouches(bool enabled) {
287 mConfig.showTouches = enabled;
288 }
289
Garfield Tan888a6a42020-01-09 11:39:16 -0800290 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
291 mConfig.defaultPointerDisplayId = pointerDisplayId;
292 }
293
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800294 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
295
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700297 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700298 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
299 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700300 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
301 || orientation == DISPLAY_ORIENTATION_270);
302 DisplayViewport v;
303 v.displayId = displayId;
304 v.orientation = orientation;
305 v.logicalLeft = 0;
306 v.logicalTop = 0;
307 v.logicalRight = isRotated ? height : width;
308 v.logicalBottom = isRotated ? width : height;
309 v.physicalLeft = 0;
310 v.physicalTop = 0;
311 v.physicalRight = isRotated ? height : width;
312 v.physicalBottom = isRotated ? width : height;
313 v.deviceWidth = isRotated ? height : width;
314 v.deviceHeight = isRotated ? width : height;
315 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700316 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100317 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700318 return v;
319 }
320
Chris Yea52ade12020-08-27 16:49:20 -0700321 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 *outConfig = mConfig;
323 }
324
Chris Yea52ade12020-08-27 16:49:20 -0700325 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100326 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328
Chris Yea52ade12020-08-27 16:49:20 -0700329 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700330 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700332 mInputDevicesChanged = true;
333 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Chris Yea52ade12020-08-27 16:49:20 -0700336 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
337 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700338 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339 }
340
Chris Yea52ade12020-08-27 16:49:20 -0700341 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800342
343 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
344 std::unique_lock<std::mutex> lock(mLock);
345 base::ScopedLockAssertion assumeLocked(mLock);
346
347 const bool devicesChanged =
348 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
349 return mInputDevicesChanged;
350 });
351 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
352 mInputDevicesChanged = false;
353 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354};
355
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356// --- FakeEventHub ---
357
358class FakeEventHub : public EventHubInterface {
359 struct KeyInfo {
360 int32_t keyCode;
361 uint32_t flags;
362 };
363
364 struct Device {
365 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700366 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367 PropertyMap configuration;
368 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
369 KeyedVector<int, bool> relativeAxes;
370 KeyedVector<int32_t, int32_t> keyCodeStates;
371 KeyedVector<int32_t, int32_t> scanCodeStates;
372 KeyedVector<int32_t, int32_t> switchStates;
373 KeyedVector<int32_t, int32_t> absoluteAxisValue;
374 KeyedVector<int32_t, KeyInfo> keysByScanCode;
375 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
376 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800377 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700378 bool enabled;
379
380 status_t enable() {
381 enabled = true;
382 return OK;
383 }
384
385 status_t disable() {
386 enabled = false;
387 return OK;
388 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389
Chris Ye1b0c7342020-07-28 21:57:03 -0700390 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 };
392
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700393 std::mutex mLock;
394 std::condition_variable mEventsCondition;
395
Michael Wrightd02c5b62014-02-10 15:10:22 -0800396 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100397 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700398 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600399 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000400 std::vector<int32_t> mVibrators = {0, 1};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700402public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403 virtual ~FakeEventHub() {
404 for (size_t i = 0; i < mDevices.size(); i++) {
405 delete mDevices.valueAt(i);
406 }
407 }
408
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 FakeEventHub() { }
410
Chris Ye1b0c7342020-07-28 21:57:03 -0700411 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412 Device* device = new Device(classes);
413 device->identifier.name = name;
414 mDevices.add(deviceId, device);
415
416 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
417 }
418
419 void removeDevice(int32_t deviceId) {
420 delete mDevices.valueFor(deviceId);
421 mDevices.removeItem(deviceId);
422
423 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
424 }
425
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700426 bool isDeviceEnabled(int32_t deviceId) {
427 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700428 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700429 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
430 return false;
431 }
432 return device->enabled;
433 }
434
435 status_t enableDevice(int32_t deviceId) {
436 status_t result;
437 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700438 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700439 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
440 return BAD_VALUE;
441 }
442 if (device->enabled) {
443 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
444 return OK;
445 }
446 result = device->enable();
447 return result;
448 }
449
450 status_t disableDevice(int32_t deviceId) {
451 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700452 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700453 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
454 return BAD_VALUE;
455 }
456 if (!device->enabled) {
457 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
458 return OK;
459 }
460 return device->disable();
461 }
462
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463 void finishDeviceScan() {
464 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
465 }
466
467 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
468 Device* device = getDevice(deviceId);
469 device->configuration.addProperty(key, value);
470 }
471
472 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
473 Device* device = getDevice(deviceId);
474 device->configuration.addAll(configuration);
475 }
476
477 void addAbsoluteAxis(int32_t deviceId, int axis,
478 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
479 Device* device = getDevice(deviceId);
480
481 RawAbsoluteAxisInfo info;
482 info.valid = true;
483 info.minValue = minValue;
484 info.maxValue = maxValue;
485 info.flat = flat;
486 info.fuzz = fuzz;
487 info.resolution = resolution;
488 device->absoluteAxes.add(axis, info);
489 }
490
491 void addRelativeAxis(int32_t deviceId, int32_t axis) {
492 Device* device = getDevice(deviceId);
493 device->relativeAxes.add(axis, true);
494 }
495
496 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
497 Device* device = getDevice(deviceId);
498 device->keyCodeStates.replaceValueFor(keyCode, state);
499 }
500
501 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
502 Device* device = getDevice(deviceId);
503 device->scanCodeStates.replaceValueFor(scanCode, state);
504 }
505
506 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
507 Device* device = getDevice(deviceId);
508 device->switchStates.replaceValueFor(switchCode, state);
509 }
510
511 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
512 Device* device = getDevice(deviceId);
513 device->absoluteAxisValue.replaceValueFor(axis, value);
514 }
515
516 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
517 int32_t keyCode, uint32_t flags) {
518 Device* device = getDevice(deviceId);
519 KeyInfo info;
520 info.keyCode = keyCode;
521 info.flags = flags;
522 if (scanCode) {
523 device->keysByScanCode.add(scanCode, info);
524 }
525 if (usageCode) {
526 device->keysByUsageCode.add(usageCode, info);
527 }
528 }
529
530 void addLed(int32_t deviceId, int32_t led, bool initialState) {
531 Device* device = getDevice(deviceId);
532 device->leds.add(led, initialState);
533 }
534
535 bool getLedState(int32_t deviceId, int32_t led) {
536 Device* device = getDevice(deviceId);
537 return device->leds.valueFor(led);
538 }
539
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100540 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800541 return mExcludedDevices;
542 }
543
544 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
545 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800546 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 }
548
549 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
550 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700551 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800552 RawEvent event;
553 event.when = when;
554 event.deviceId = deviceId;
555 event.type = type;
556 event.code = code;
557 event.value = value;
558 mEvents.push_back(event);
559
560 if (type == EV_ABS) {
561 setAbsoluteAxisValue(deviceId, code, value);
562 }
563 }
564
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600565 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
566 std::vector<TouchVideoFrame>> videoFrames) {
567 mVideoFrames = std::move(videoFrames);
568 }
569
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700571 std::unique_lock<std::mutex> lock(mLock);
572 base::ScopedLockAssertion assumeLocked(mLock);
573 const bool queueIsEmpty =
574 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
575 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
576 if (!queueIsEmpty) {
577 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
578 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 }
580
581private:
582 Device* getDevice(int32_t deviceId) const {
583 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100584 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585 }
586
Chris Yea52ade12020-08-27 16:49:20 -0700587 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700589 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 }
591
Chris Yea52ade12020-08-27 16:49:20 -0700592 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 Device* device = getDevice(deviceId);
594 return device ? device->identifier : InputDeviceIdentifier();
595 }
596
Chris Yea52ade12020-08-27 16:49:20 -0700597 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598
Chris Yea52ade12020-08-27 16:49:20 -0700599 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 Device* device = getDevice(deviceId);
601 if (device) {
602 *outConfiguration = device->configuration;
603 }
604 }
605
Chris Yea52ade12020-08-27 16:49:20 -0700606 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
607 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800609 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 ssize_t index = device->absoluteAxes.indexOfKey(axis);
611 if (index >= 0) {
612 *outAxisInfo = device->absoluteAxes.valueAt(index);
613 return OK;
614 }
615 }
616 outAxisInfo->clear();
617 return -1;
618 }
619
Chris Yea52ade12020-08-27 16:49:20 -0700620 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621 Device* device = getDevice(deviceId);
622 if (device) {
623 return device->relativeAxes.indexOfKey(axis) >= 0;
624 }
625 return false;
626 }
627
Chris Yea52ade12020-08-27 16:49:20 -0700628 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629
Chris Yea52ade12020-08-27 16:49:20 -0700630 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
631 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 Device* device = getDevice(deviceId);
633 if (device) {
634 const KeyInfo* key = getKey(device, scanCode, usageCode);
635 if (key) {
636 if (outKeycode) {
637 *outKeycode = key->keyCode;
638 }
639 if (outFlags) {
640 *outFlags = key->flags;
641 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700642 if (outMetaState) {
643 *outMetaState = metaState;
644 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 return OK;
646 }
647 }
648 return NAME_NOT_FOUND;
649 }
650
651 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
652 if (usageCode) {
653 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
654 if (index >= 0) {
655 return &device->keysByUsageCode.valueAt(index);
656 }
657 }
658 if (scanCode) {
659 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
660 if (index >= 0) {
661 return &device->keysByScanCode.valueAt(index);
662 }
663 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700664 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800665 }
666
Chris Yea52ade12020-08-27 16:49:20 -0700667 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668
Chris Yea52ade12020-08-27 16:49:20 -0700669 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 mExcludedDevices = devices;
671 }
672
Chris Yea52ade12020-08-27 16:49:20 -0700673 size_t getEvents(int, RawEvent* buffer, size_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700674 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 if (mEvents.empty()) {
676 return 0;
677 }
678
679 *buffer = *mEvents.begin();
680 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700681 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 return 1;
683 }
684
Chris Yea52ade12020-08-27 16:49:20 -0700685 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600686 auto it = mVideoFrames.find(deviceId);
687 if (it != mVideoFrames.end()) {
688 std::vector<TouchVideoFrame> frames = std::move(it->second);
689 mVideoFrames.erase(deviceId);
690 return frames;
691 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800692 return {};
693 }
694
Chris Yea52ade12020-08-27 16:49:20 -0700695 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 Device* device = getDevice(deviceId);
697 if (device) {
698 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
699 if (index >= 0) {
700 return device->scanCodeStates.valueAt(index);
701 }
702 }
703 return AKEY_STATE_UNKNOWN;
704 }
705
Chris Yea52ade12020-08-27 16:49:20 -0700706 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 Device* device = getDevice(deviceId);
708 if (device) {
709 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
710 if (index >= 0) {
711 return device->keyCodeStates.valueAt(index);
712 }
713 }
714 return AKEY_STATE_UNKNOWN;
715 }
716
Chris Yea52ade12020-08-27 16:49:20 -0700717 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 Device* device = getDevice(deviceId);
719 if (device) {
720 ssize_t index = device->switchStates.indexOfKey(sw);
721 if (index >= 0) {
722 return device->switchStates.valueAt(index);
723 }
724 }
725 return AKEY_STATE_UNKNOWN;
726 }
727
Chris Yea52ade12020-08-27 16:49:20 -0700728 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
729 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 Device* device = getDevice(deviceId);
731 if (device) {
732 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
733 if (index >= 0) {
734 *outValue = device->absoluteAxisValue.valueAt(index);
735 return OK;
736 }
737 }
738 *outValue = 0;
739 return -1;
740 }
741
Chris Yea52ade12020-08-27 16:49:20 -0700742 // Return true if the device has non-empty key layout.
743 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
744 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 bool result = false;
746 Device* device = getDevice(deviceId);
747 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700748 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749 for (size_t i = 0; i < numCodes; i++) {
750 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
751 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
752 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753 }
754 }
755 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
756 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
757 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 }
759 }
760 }
761 }
762 return result;
763 }
764
Chris Yea52ade12020-08-27 16:49:20 -0700765 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766 Device* device = getDevice(deviceId);
767 if (device) {
768 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
769 return index >= 0;
770 }
771 return false;
772 }
773
Chris Yea52ade12020-08-27 16:49:20 -0700774 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800775 Device* device = getDevice(deviceId);
776 return device && device->leds.indexOfKey(led) >= 0;
777 }
778
Chris Yea52ade12020-08-27 16:49:20 -0700779 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800780 Device* device = getDevice(deviceId);
781 if (device) {
782 ssize_t index = device->leds.indexOfKey(led);
783 if (index >= 0) {
784 device->leds.replaceValueAt(led, on);
785 } else {
786 ADD_FAILURE()
787 << "Attempted to set the state of an LED that the EventHub declared "
788 "was not present. led=" << led;
789 }
790 }
791 }
792
Chris Yea52ade12020-08-27 16:49:20 -0700793 void getVirtualKeyDefinitions(
794 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795 outVirtualKeys.clear();
796
797 Device* device = getDevice(deviceId);
798 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800799 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 }
801 }
802
Chris Yea52ade12020-08-27 16:49:20 -0700803 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700804 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 }
806
Chris Yea52ade12020-08-27 16:49:20 -0700807 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808 return false;
809 }
810
Chris Yea52ade12020-08-27 16:49:20 -0700811 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812
Chris Yea52ade12020-08-27 16:49:20 -0700813 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814
Chris Ye87143712020-11-10 05:05:58 +0000815 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
816
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100817 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 return false;
819 }
820
Chris Yea52ade12020-08-27 16:49:20 -0700821 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822
Chris Yea52ade12020-08-27 16:49:20 -0700823 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800824
Chris Yea52ade12020-08-27 16:49:20 -0700825 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826
Chris Yea52ade12020-08-27 16:49:20 -0700827 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828};
829
Michael Wrightd02c5b62014-02-10 15:10:22 -0800830// --- FakeInputMapper ---
831
832class FakeInputMapper : public InputMapper {
833 uint32_t mSources;
834 int32_t mKeyboardType;
835 int32_t mMetaState;
836 KeyedVector<int32_t, int32_t> mKeyCodeStates;
837 KeyedVector<int32_t, int32_t> mScanCodeStates;
838 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800839 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800840
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700841 std::mutex mLock;
842 std::condition_variable mStateChangedCondition;
843 bool mConfigureWasCalled GUARDED_BY(mLock);
844 bool mResetWasCalled GUARDED_BY(mLock);
845 bool mProcessWasCalled GUARDED_BY(mLock);
846 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847
Arthur Hungc23540e2018-11-29 20:42:11 +0800848 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800850 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
851 : InputMapper(deviceContext),
852 mSources(sources),
853 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800855 mConfigureWasCalled(false),
856 mResetWasCalled(false),
857 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800858
Chris Yea52ade12020-08-27 16:49:20 -0700859 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
861 void setKeyboardType(int32_t keyboardType) {
862 mKeyboardType = keyboardType;
863 }
864
865 void setMetaState(int32_t metaState) {
866 mMetaState = metaState;
867 }
868
869 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700870 std::unique_lock<std::mutex> lock(mLock);
871 base::ScopedLockAssertion assumeLocked(mLock);
872 const bool configureCalled =
873 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
874 return mConfigureWasCalled;
875 });
876 if (!configureCalled) {
877 FAIL() << "Expected configure() to have been called.";
878 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879 mConfigureWasCalled = false;
880 }
881
882 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700883 std::unique_lock<std::mutex> lock(mLock);
884 base::ScopedLockAssertion assumeLocked(mLock);
885 const bool resetCalled =
886 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
887 return mResetWasCalled;
888 });
889 if (!resetCalled) {
890 FAIL() << "Expected reset() to have been called.";
891 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 mResetWasCalled = false;
893 }
894
Yi Kong9b14ac62018-07-17 13:48:38 -0700895 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700896 std::unique_lock<std::mutex> lock(mLock);
897 base::ScopedLockAssertion assumeLocked(mLock);
898 const bool processCalled =
899 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
900 return mProcessWasCalled;
901 });
902 if (!processCalled) {
903 FAIL() << "Expected process() to have been called.";
904 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 if (outLastEvent) {
906 *outLastEvent = mLastEvent;
907 }
908 mProcessWasCalled = false;
909 }
910
911 void setKeyCodeState(int32_t keyCode, int32_t state) {
912 mKeyCodeStates.replaceValueFor(keyCode, state);
913 }
914
915 void setScanCodeState(int32_t scanCode, int32_t state) {
916 mScanCodeStates.replaceValueFor(scanCode, state);
917 }
918
919 void setSwitchState(int32_t switchCode, int32_t state) {
920 mSwitchStates.replaceValueFor(switchCode, state);
921 }
922
923 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800924 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925 }
926
927private:
Chris Yea52ade12020-08-27 16:49:20 -0700928 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
Chris Yea52ade12020-08-27 16:49:20 -0700930 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931 InputMapper::populateDeviceInfo(deviceInfo);
932
933 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
934 deviceInfo->setKeyboardType(mKeyboardType);
935 }
936 }
937
Chris Yea52ade12020-08-27 16:49:20 -0700938 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700939 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800941
942 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800943 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800944 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
945 mViewport = config->getDisplayViewportByPort(*displayPort);
946 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700947
948 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 }
950
Chris Yea52ade12020-08-27 16:49:20 -0700951 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700952 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700954 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955 }
956
Chris Yea52ade12020-08-27 16:49:20 -0700957 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700958 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 mLastEvent = *rawEvent;
960 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700961 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800962 }
963
Chris Yea52ade12020-08-27 16:49:20 -0700964 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
966 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
967 }
968
Chris Yea52ade12020-08-27 16:49:20 -0700969 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
971 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
972 }
973
Chris Yea52ade12020-08-27 16:49:20 -0700974 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800975 ssize_t index = mSwitchStates.indexOfKey(switchCode);
976 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
977 }
978
Chris Yea52ade12020-08-27 16:49:20 -0700979 // Return true if the device has non-empty key layout.
980 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
981 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 for (size_t i = 0; i < numCodes; i++) {
983 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
984 if (keyCodes[i] == mSupportedKeyCodes[j]) {
985 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986 }
987 }
988 }
Chris Yea52ade12020-08-27 16:49:20 -0700989 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 return result;
991 }
992
993 virtual int32_t getMetaState() {
994 return mMetaState;
995 }
996
997 virtual void fadePointer() {
998 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800999
1000 virtual std::optional<int32_t> getAssociatedDisplay() {
1001 if (mViewport) {
1002 return std::make_optional(mViewport->displayId);
1003 }
1004 return std::nullopt;
1005 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001006};
1007
1008
1009// --- InstrumentedInputReader ---
1010
1011class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001012 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013
1014public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001015 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1016 const sp<InputReaderPolicyInterface>& policy,
1017 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001018 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001020 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001022 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001024 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001025 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026 InputDeviceIdentifier identifier;
1027 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001028 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001029 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001030 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 }
1032
Prabir Pradhan28efc192019-11-05 01:10:04 +00001033 // Make the protected loopOnce method accessible to tests.
1034 using InputReader::loopOnce;
1035
Michael Wrightd02c5b62014-02-10 15:10:22 -08001036protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001037 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1038 const InputDeviceIdentifier& identifier)
1039 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001040 if (!mNextDevices.empty()) {
1041 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1042 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 return device;
1044 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001045 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046 }
1047
arthurhungdcef2dc2020-08-11 14:47:50 +08001048 // --- FakeInputReaderContext ---
1049 class FakeInputReaderContext : public ContextImpl {
1050 int32_t mGlobalMetaState;
1051 bool mUpdateGlobalMetaStateWasCalled;
1052 int32_t mGeneration;
1053
1054 public:
1055 FakeInputReaderContext(InputReader* reader)
1056 : ContextImpl(reader),
1057 mGlobalMetaState(0),
1058 mUpdateGlobalMetaStateWasCalled(false),
1059 mGeneration(1) {}
1060
1061 virtual ~FakeInputReaderContext() {}
1062
1063 void assertUpdateGlobalMetaStateWasCalled() {
1064 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1065 << "Expected updateGlobalMetaState() to have been called.";
1066 mUpdateGlobalMetaStateWasCalled = false;
1067 }
1068
1069 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1070
1071 uint32_t getGeneration() { return mGeneration; }
1072
1073 void updateGlobalMetaState() override {
1074 mUpdateGlobalMetaStateWasCalled = true;
1075 ContextImpl::updateGlobalMetaState();
1076 }
1077
1078 int32_t getGlobalMetaState() override {
1079 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1080 }
1081
1082 int32_t bumpGeneration() override {
1083 mGeneration = ContextImpl::bumpGeneration();
1084 return mGeneration;
1085 }
1086 } mFakeContext;
1087
Michael Wrightd02c5b62014-02-10 15:10:22 -08001088 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001089
1090public:
1091 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092};
1093
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001094// --- InputReaderPolicyTest ---
1095class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001096protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001097 sp<FakeInputReaderPolicy> mFakePolicy;
1098
Chris Yea52ade12020-08-27 16:49:20 -07001099 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1100 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001101};
1102
1103/**
1104 * Check that empty set of viewports is an acceptable configuration.
1105 * Also try to get internal viewport two different ways - by type and by uniqueId.
1106 *
1107 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1108 * Such configuration is not currently allowed.
1109 */
1110TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001111 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001112
1113 // We didn't add any viewports yet, so there shouldn't be any.
1114 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001115 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001116 ASSERT_FALSE(internalViewport);
1117
1118 // Add an internal viewport, then clear it
1119 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001120 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT,
1121 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001122
1123 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001124 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001125 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001126 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001127
1128 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001129 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001130 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001131 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001132
1133 mFakePolicy->clearViewports();
1134 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001135 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001136 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001137 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001138 ASSERT_FALSE(internalViewport);
1139}
1140
1141TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1142 const std::string internalUniqueId = "local:0";
1143 const std::string externalUniqueId = "local:1";
1144 const std::string virtualUniqueId1 = "virtual:2";
1145 const std::string virtualUniqueId2 = "virtual:3";
1146 constexpr int32_t virtualDisplayId1 = 2;
1147 constexpr int32_t virtualDisplayId2 = 3;
1148
1149 // Add an internal viewport
1150 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001151 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT,
1152 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001153 // Add an external viewport
1154 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001155 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT,
1156 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001157 // Add an virtual viewport
1158 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001159 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT,
1160 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001161 // Add another virtual viewport
1162 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001163 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT,
1164 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001165
1166 // Check matching by type for internal
1167 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001168 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001169 ASSERT_TRUE(internalViewport);
1170 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1171
1172 // Check matching by type for external
1173 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001174 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001175 ASSERT_TRUE(externalViewport);
1176 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1177
1178 // Check matching by uniqueId for virtual viewport #1
1179 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001180 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001181 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001182 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001183 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1184 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1185
1186 // Check matching by uniqueId for virtual viewport #2
1187 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001188 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001189 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001190 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001191 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1192 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1193}
1194
1195
1196/**
1197 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1198 * that lookup works by checking display id.
1199 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1200 */
1201TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1202 const std::string uniqueId1 = "uniqueId1";
1203 const std::string uniqueId2 = "uniqueId2";
1204 constexpr int32_t displayId1 = 2;
1205 constexpr int32_t displayId2 = 3;
1206
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001207 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1208 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001209 for (const ViewportType& type : types) {
1210 mFakePolicy->clearViewports();
1211 // Add a viewport
1212 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001213 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001214 // Add another viewport
1215 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001216 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001217
1218 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001219 std::optional<DisplayViewport> viewport1 =
1220 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001221 ASSERT_TRUE(viewport1);
1222 ASSERT_EQ(displayId1, viewport1->displayId);
1223 ASSERT_EQ(type, viewport1->type);
1224
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001225 std::optional<DisplayViewport> viewport2 =
1226 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001227 ASSERT_TRUE(viewport2);
1228 ASSERT_EQ(displayId2, viewport2->displayId);
1229 ASSERT_EQ(type, viewport2->type);
1230
1231 // When there are multiple viewports of the same kind, and uniqueId is not specified
1232 // in the call to getDisplayViewport, then that situation is not supported.
1233 // The viewports can be stored in any order, so we cannot rely on the order, since that
1234 // is just implementation detail.
1235 // However, we can check that it still returns *a* viewport, we just cannot assert
1236 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001237 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001238 ASSERT_TRUE(someViewport);
1239 }
1240}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001242/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001243 * When we have multiple internal displays make sure we always return the default display when
1244 * querying by type.
1245 */
1246TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1247 const std::string uniqueId1 = "uniqueId1";
1248 const std::string uniqueId2 = "uniqueId2";
1249 constexpr int32_t nonDefaultDisplayId = 2;
1250 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1251 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1252
1253 // Add the default display first and ensure it gets returned.
1254 mFakePolicy->clearViewports();
1255 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1256 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT,
1257 ViewportType::INTERNAL);
1258 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1259 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT,
1260 ViewportType::INTERNAL);
1261
1262 std::optional<DisplayViewport> viewport =
1263 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1264 ASSERT_TRUE(viewport);
1265 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1266 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1267
1268 // Add the default display second to make sure order doesn't matter.
1269 mFakePolicy->clearViewports();
1270 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1271 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT,
1272 ViewportType::INTERNAL);
1273 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1274 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT,
1275 ViewportType::INTERNAL);
1276
1277 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1278 ASSERT_TRUE(viewport);
1279 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1280 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1281}
1282
1283/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001284 * Check getDisplayViewportByPort
1285 */
1286TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001287 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001288 const std::string uniqueId1 = "uniqueId1";
1289 const std::string uniqueId2 = "uniqueId2";
1290 constexpr int32_t displayId1 = 1;
1291 constexpr int32_t displayId2 = 2;
1292 const uint8_t hdmi1 = 0;
1293 const uint8_t hdmi2 = 1;
1294 const uint8_t hdmi3 = 2;
1295
1296 mFakePolicy->clearViewports();
1297 // Add a viewport that's associated with some display port that's not of interest.
1298 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1299 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1300 // Add another viewport, connected to HDMI1 port
1301 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1302 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1303
1304 // Check that correct display viewport was returned by comparing the display ports.
1305 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1306 ASSERT_TRUE(hdmi1Viewport);
1307 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1308 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1309
1310 // Check that we can still get the same viewport using the uniqueId
1311 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1312 ASSERT_TRUE(hdmi1Viewport);
1313 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1314 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1315 ASSERT_EQ(type, hdmi1Viewport->type);
1316
1317 // Check that we cannot find a port with "HDMI2", because we never added one
1318 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1319 ASSERT_FALSE(hdmi2Viewport);
1320}
1321
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322// --- InputReaderTest ---
1323
1324class InputReaderTest : public testing::Test {
1325protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001326 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001327 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001328 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001329 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001330
Chris Yea52ade12020-08-27 16:49:20 -07001331 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001332 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001333 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001334 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001335
Prabir Pradhan28efc192019-11-05 01:10:04 +00001336 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1337 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001338 }
1339
Chris Yea52ade12020-08-27 16:49:20 -07001340 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001341 mFakeListener.clear();
1342 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001343 }
1344
Chris Ye1b0c7342020-07-28 21:57:03 -07001345 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001346 const PropertyMap* configuration) {
1347 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001348
1349 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001350 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001351 }
1352 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001353 mReader->loopOnce();
1354 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001355 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1356 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001357 }
1358
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001359 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001360 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001361 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001362 }
1363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001364 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001365 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001366 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001367 }
1368
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001369 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001370 const std::string& name,
1371 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001372 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001373 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1374 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001375 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001376 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001377 return mapper;
1378 }
1379};
1380
Chris Ye98d3f532020-10-01 21:48:59 -07001381TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001382 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1383 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1384 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001385
Chris Ye98d3f532020-10-01 21:48:59 -07001386 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001387 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001388 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001389 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001390 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1391 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1392 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001393}
1394
1395TEST_F(InputReaderTest, PolicyGetInputDevices) {
1396 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1397 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1398 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399
1400 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001401 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001402 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001403 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001404 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001405 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1406 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1407 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1408}
1409
Chris Yee7310032020-09-22 15:36:28 -07001410TEST_F(InputReaderTest, GetMergedInputDevices) {
1411 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1412 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1413 // Add two subdevices to device
1414 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1415 // Must add at least one mapper or the device will be ignored!
1416 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1417 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1418
1419 // Push same device instance for next device to be added, so they'll have same identifier.
1420 mReader->pushNextDevice(device);
1421 mReader->pushNextDevice(device);
1422 ASSERT_NO_FATAL_FAILURE(
1423 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1424 ASSERT_NO_FATAL_FAILURE(
1425 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1426
1427 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001428 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001429}
1430
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001431TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001432 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001433 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001434 constexpr int32_t eventHubId = 1;
1435 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001436 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001437 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001438 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001439 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001440
Yi Kong9b14ac62018-07-17 13:48:38 -07001441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001442
1443 NotifyDeviceResetArgs resetArgs;
1444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001445 ASSERT_EQ(deviceId, resetArgs.deviceId);
1446
1447 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001448 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001449 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001450
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001452 ASSERT_EQ(deviceId, resetArgs.deviceId);
1453 ASSERT_EQ(device->isEnabled(), false);
1454
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001455 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001456 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001459 ASSERT_EQ(device->isEnabled(), false);
1460
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001461 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001462 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001464 ASSERT_EQ(deviceId, resetArgs.deviceId);
1465 ASSERT_EQ(device->isEnabled(), true);
1466}
1467
Michael Wrightd02c5b62014-02-10 15:10:22 -08001468TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001469 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001470 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001471 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001472 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001473 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001474 AINPUT_SOURCE_KEYBOARD, nullptr);
1475 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476
1477 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1478 AINPUT_SOURCE_ANY, AKEYCODE_A))
1479 << "Should return unknown when the device id is >= 0 but unknown.";
1480
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001481 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1482 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1483 << "Should return unknown when the device id is valid but the sources are not "
1484 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001485
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001486 ASSERT_EQ(AKEY_STATE_DOWN,
1487 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1488 AKEYCODE_A))
1489 << "Should return value provided by mapper when device id is valid and the device "
1490 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001491
1492 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1493 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1494 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1495
1496 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1497 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1498 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1499}
1500
1501TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001502 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001503 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001504 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001505 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001506 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001507 AINPUT_SOURCE_KEYBOARD, nullptr);
1508 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001509
1510 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1511 AINPUT_SOURCE_ANY, KEY_A))
1512 << "Should return unknown when the device id is >= 0 but unknown.";
1513
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001514 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1515 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1516 << "Should return unknown when the device id is valid but the sources are not "
1517 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001518
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 ASSERT_EQ(AKEY_STATE_DOWN,
1520 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1521 KEY_A))
1522 << "Should return value provided by mapper when device id is valid and the device "
1523 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524
1525 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1526 AINPUT_SOURCE_TRACKBALL, KEY_A))
1527 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1528
1529 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1530 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1531 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1532}
1533
1534TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001535 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001536 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001537 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001538 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001539 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001540 AINPUT_SOURCE_KEYBOARD, nullptr);
1541 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001542
1543 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1544 AINPUT_SOURCE_ANY, SW_LID))
1545 << "Should return unknown when the device id is >= 0 but unknown.";
1546
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001547 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1548 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1549 << "Should return unknown when the device id is valid but the sources are not "
1550 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001551
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001552 ASSERT_EQ(AKEY_STATE_DOWN,
1553 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1554 SW_LID))
1555 << "Should return value provided by mapper when device id is valid and the device "
1556 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001557
1558 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1559 AINPUT_SOURCE_TRACKBALL, SW_LID))
1560 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1561
1562 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1563 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1564 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1565}
1566
1567TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001568 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001569 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001570 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001571 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001572 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001573 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001574
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001575 mapper.addSupportedKeyCode(AKEYCODE_A);
1576 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577
1578 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1579 uint8_t flags[4] = { 0, 0, 0, 1 };
1580
1581 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1582 << "Should return false when device id is >= 0 but unknown.";
1583 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1584
1585 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001586 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1587 << "Should return false when device id is valid but the sources are not supported by "
1588 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001589 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1590
1591 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001592 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1593 keyCodes, flags))
1594 << "Should return value provided by mapper when device id is valid and the device "
1595 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1597
1598 flags[3] = 1;
1599 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1600 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1601 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1602
1603 flags[3] = 1;
1604 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1605 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1606 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1607}
1608
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001609TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001610 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001611 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001612
1613 NotifyConfigurationChangedArgs args;
1614
1615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1616 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1617}
1618
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001619TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001620 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001621 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001622 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001623 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001624 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001625 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001626
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001628 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1630
1631 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001632 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001633 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001634 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001635 ASSERT_EQ(EV_KEY, event.type);
1636 ASSERT_EQ(KEY_A, event.code);
1637 ASSERT_EQ(1, event.value);
1638}
1639
Garfield Tan1c7bc862020-01-28 13:24:04 -08001640TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001642 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001643 constexpr int32_t eventHubId = 1;
1644 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001645 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001646 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001647 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001649
1650 NotifyDeviceResetArgs resetArgs;
1651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001652 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001653
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001654 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001655 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001657 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001658 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001659
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001660 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001661 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001663 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001664 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001665
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001666 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001667 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001669 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001670 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001671}
1672
Garfield Tan1c7bc862020-01-28 13:24:04 -08001673TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1674 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001675 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001676 constexpr int32_t eventHubId = 1;
1677 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1678 // Must add at least one mapper or the device will be ignored!
1679 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001680 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001681 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1682
1683 NotifyDeviceResetArgs resetArgs;
1684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1685 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1686}
1687
Arthur Hungc23540e2018-11-29 20:42:11 +08001688TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001689 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001690 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001691 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001692 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001693 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1694 FakeInputMapper& mapper =
1695 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001696 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001697
1698 const uint8_t hdmi1 = 1;
1699
1700 // Associated touch screen with second display.
1701 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1702
1703 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001704 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001705 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001706 DISPLAY_ORIENTATION_0, "local:0", NO_PORT,
1707 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001708 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001709 DISPLAY_ORIENTATION_0, "local:1", hdmi1,
1710 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001711 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001712 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001713
1714 // Add the device, and make sure all of the callbacks are triggered.
1715 // The device is added after the input port associations are processed since
1716 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001717 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001720 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001721
Arthur Hung2c9a3342019-07-23 14:18:59 +08001722 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001723 ASSERT_EQ(deviceId, device->getId());
1724 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1725 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001726
1727 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001728 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001729 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001730 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001731}
1732
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001733TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1734 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1735 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1736 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1737 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1738 // Must add at least one mapper or the device will be ignored!
1739 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1740 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1741 mReader->pushNextDevice(device);
1742 mReader->pushNextDevice(device);
1743 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1744 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1745
1746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1747
1748 NotifyDeviceResetArgs resetArgs;
1749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1750 ASSERT_EQ(deviceId, resetArgs.deviceId);
1751 ASSERT_TRUE(device->isEnabled());
1752 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1753 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1754
1755 disableDevice(deviceId);
1756 mReader->loopOnce();
1757
1758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1759 ASSERT_EQ(deviceId, resetArgs.deviceId);
1760 ASSERT_FALSE(device->isEnabled());
1761 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1762 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1763
1764 enableDevice(deviceId);
1765 mReader->loopOnce();
1766
1767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1768 ASSERT_EQ(deviceId, resetArgs.deviceId);
1769 ASSERT_TRUE(device->isEnabled());
1770 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1771 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1772}
1773
1774TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1775 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1776 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1777 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1778 // Add two subdevices to device
1779 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1780 FakeInputMapper& mapperDevice1 =
1781 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1782 FakeInputMapper& mapperDevice2 =
1783 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1784 mReader->pushNextDevice(device);
1785 mReader->pushNextDevice(device);
1786 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1787 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1788
1789 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1790 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1791
1792 ASSERT_EQ(AKEY_STATE_DOWN,
1793 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1794 ASSERT_EQ(AKEY_STATE_DOWN,
1795 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1796 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1797 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1798}
1799
Prabir Pradhan7e186182020-11-10 13:56:45 -08001800TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1801 NotifyPointerCaptureChangedArgs args;
1802
1803 mFakePolicy->setPointerCapture(true);
1804 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1805 mReader->loopOnce();
1806 mFakeListener->assertNotifyCaptureWasCalled(&args);
1807 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1808
1809 mFakePolicy->setPointerCapture(false);
1810 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1811 mReader->loopOnce();
1812 mFakeListener->assertNotifyCaptureWasCalled(&args);
1813 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1814
1815 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1816 // does not change.
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
Chris Ye87143712020-11-10 05:05:58 +00001823class FakeVibratorInputMapper : public FakeInputMapper {
1824public:
1825 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1826 : FakeInputMapper(deviceContext, sources) {}
1827
1828 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1829};
1830
1831TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1832 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1833 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1834 constexpr int32_t eventHubId = 1;
1835 const char* DEVICE_LOCATION = "BLUETOOTH";
1836 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1837 FakeVibratorInputMapper& mapper =
1838 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1839 mReader->pushNextDevice(device);
1840
1841 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1842 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1843
1844 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1845 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1846}
1847
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001848// --- InputReaderIntegrationTest ---
1849
1850// These tests create and interact with the InputReader only through its interface.
1851// The InputReader is started during SetUp(), which starts its processing in its own
1852// thread. The tests use linux uinput to emulate input devices.
1853// NOTE: Interacting with the physical device while these tests are running may cause
1854// the tests to fail.
1855class InputReaderIntegrationTest : public testing::Test {
1856protected:
1857 sp<TestInputListener> mTestListener;
1858 sp<FakeInputReaderPolicy> mFakePolicy;
1859 sp<InputReaderInterface> mReader;
1860
Chris Yea52ade12020-08-27 16:49:20 -07001861 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001862 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001863 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1864 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001865
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001866 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001867 ASSERT_EQ(mReader->start(), OK);
1868
1869 // Since this test is run on a real device, all the input devices connected
1870 // to the test device will show up in mReader. We wait for those input devices to
1871 // show up before beginning the tests.
1872 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1873 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1874 }
1875
Chris Yea52ade12020-08-27 16:49:20 -07001876 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001877 ASSERT_EQ(mReader->stop(), OK);
1878 mTestListener.clear();
1879 mFakePolicy.clear();
1880 }
1881};
1882
1883TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1884 // An invalid input device that is only used for this test.
1885 class InvalidUinputDevice : public UinputDevice {
1886 public:
1887 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1888
1889 private:
1890 void configureDevice(int fd, uinput_user_dev* device) override {}
1891 };
1892
1893 const size_t numDevices = mFakePolicy->getInputDevices().size();
1894
1895 // UinputDevice does not set any event or key bits, so InputReader should not
1896 // consider it as a valid device.
1897 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1898 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1899 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1900 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1901
1902 invalidDevice.reset();
1903 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1904 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1905 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1906}
1907
1908TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1909 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1910
1911 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1912 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1913 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1914 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1915
1916 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07001917 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
1918 const auto& it =
1919 std::find_if(inputDevices.begin(), inputDevices.end(),
1920 [&keyboard](const InputDeviceInfo& info) {
1921 return info.getIdentifier().name == keyboard->getName();
1922 });
1923
1924 ASSERT_NE(it, inputDevices.end());
1925 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
1926 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
1927 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001928
1929 keyboard.reset();
1930 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1931 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1932 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1933}
1934
1935TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1936 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1937 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1938
1939 NotifyConfigurationChangedArgs configChangedArgs;
1940 ASSERT_NO_FATAL_FAILURE(
1941 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001942 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001943 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1944
1945 NotifyKeyArgs keyArgs;
1946 keyboard->pressAndReleaseHomeKey();
1947 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1948 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001949 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001950 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001951 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1952 prevTimestamp = keyArgs.eventTime;
1953
1954 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1955 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001956 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001957 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1958}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001959
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001960/**
1961 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1962 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1963 * are passed to the listener.
1964 */
1965static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1966TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1967 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1968 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1969 NotifyKeyArgs keyArgs;
1970
1971 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1972 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1973 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1974 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1975
1976 controller->pressAndReleaseKey(BTN_GEAR_UP);
1977 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1978 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1979 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1980}
1981
Arthur Hungaab25622020-01-16 11:22:11 +08001982// --- TouchProcessTest ---
1983class TouchIntegrationTest : public InputReaderIntegrationTest {
1984protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001985 const std::string UNIQUE_ID = "local:0";
1986
Chris Yea52ade12020-08-27 16:49:20 -07001987 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08001988 InputReaderIntegrationTest::SetUp();
1989 // At least add an internal display.
1990 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1991 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001992 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001993
1994 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1995 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1996 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1997 }
1998
1999 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2000 int32_t orientation, const std::string& uniqueId,
2001 std::optional<uint8_t> physicalPort,
2002 ViewportType viewportType) {
2003 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
2004 physicalPort, viewportType);
2005 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2006 }
2007
2008 std::unique_ptr<UinputTouchScreen> mDevice;
2009};
2010
2011TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2012 NotifyMotionArgs args;
2013 const Point centerPoint = mDevice->getCenterPoint();
2014
2015 // ACTION_DOWN
2016 mDevice->sendDown(centerPoint);
2017 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2018 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2019
2020 // ACTION_MOVE
2021 mDevice->sendMove(centerPoint + Point(1, 1));
2022 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2024
2025 // ACTION_UP
2026 mDevice->sendUp();
2027 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2028 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2029}
2030
2031TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2032 NotifyMotionArgs args;
2033 const Point centerPoint = mDevice->getCenterPoint();
2034
2035 // ACTION_DOWN
2036 mDevice->sendDown(centerPoint);
2037 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2038 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2039
2040 // ACTION_POINTER_DOWN (Second slot)
2041 const Point secondPoint = centerPoint + Point(100, 100);
2042 mDevice->sendSlot(SECOND_SLOT);
2043 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2044 mDevice->sendDown(secondPoint + Point(1, 1));
2045 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2046 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2047 args.action);
2048
2049 // ACTION_MOVE (Second slot)
2050 mDevice->sendMove(secondPoint);
2051 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2053
2054 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002055 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002056 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002057 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002058 args.action);
2059
2060 // ACTION_UP
2061 mDevice->sendSlot(FIRST_SLOT);
2062 mDevice->sendUp();
2063 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2064 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2065}
2066
2067TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2068 NotifyMotionArgs args;
2069 const Point centerPoint = mDevice->getCenterPoint();
2070
2071 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002072 mDevice->sendSlot(FIRST_SLOT);
2073 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002074 mDevice->sendDown(centerPoint);
2075 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2076 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2077
arthurhungcc7f9802020-04-30 17:55:40 +08002078 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002079 const Point secondPoint = centerPoint + Point(100, 100);
2080 mDevice->sendSlot(SECOND_SLOT);
2081 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2082 mDevice->sendDown(secondPoint);
2083 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2084 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2085 args.action);
2086
arthurhungcc7f9802020-04-30 17:55:40 +08002087 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002088 mDevice->sendMove(secondPoint + Point(1, 1));
2089 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2091
arthurhungcc7f9802020-04-30 17:55:40 +08002092 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2093 // a palm event.
2094 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002095 mDevice->sendToolType(MT_TOOL_PALM);
2096 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002097 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2098 args.action);
2099 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002100
arthurhungcc7f9802020-04-30 17:55:40 +08002101 // Send up to second slot, expect first slot send moving.
2102 mDevice->sendPointerUp();
2103 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002105
arthurhungcc7f9802020-04-30 17:55:40 +08002106 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002107 mDevice->sendSlot(FIRST_SLOT);
2108 mDevice->sendUp();
2109
arthurhungcc7f9802020-04-30 17:55:40 +08002110 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2111 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002112}
2113
Michael Wrightd02c5b62014-02-10 15:10:22 -08002114// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002115class InputDeviceTest : public testing::Test {
2116protected:
2117 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002118 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002119 static const int32_t DEVICE_ID;
2120 static const int32_t DEVICE_GENERATION;
2121 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002122 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002123 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002124
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002125 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002126 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002127 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002128 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002129 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002130
Chris Yea52ade12020-08-27 16:49:20 -07002131 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002132 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002133 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002134 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002135 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2136 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002137 InputDeviceIdentifier identifier;
2138 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002139 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002140 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002141 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002142 mReader->pushNextDevice(mDevice);
2143 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2144 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002145 }
2146
Chris Yea52ade12020-08-27 16:49:20 -07002147 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002148 mFakeListener.clear();
2149 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002150 }
2151};
2152
2153const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002154const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002155const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002156const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2157const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002158const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2159 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002160const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002161
2162TEST_F(InputDeviceTest, ImmutableProperties) {
2163 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002164 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002165 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002166}
2167
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002168TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2169 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002170}
2171
Michael Wrightd02c5b62014-02-10 15:10:22 -08002172TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2173 // Configuration.
2174 InputReaderConfiguration config;
2175 mDevice->configure(ARBITRARY_TIME, &config, 0);
2176
2177 // Reset.
2178 mDevice->reset(ARBITRARY_TIME);
2179
2180 NotifyDeviceResetArgs resetArgs;
2181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2182 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2183 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2184
2185 // Metadata.
2186 ASSERT_TRUE(mDevice->isIgnored());
2187 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2188
2189 InputDeviceInfo info;
2190 mDevice->getDeviceInfo(&info);
2191 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002192 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002193 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2194 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2195
2196 // State queries.
2197 ASSERT_EQ(0, mDevice->getMetaState());
2198
2199 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2200 << "Ignored device should return unknown key code state.";
2201 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2202 << "Ignored device should return unknown scan code state.";
2203 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2204 << "Ignored device should return unknown switch state.";
2205
2206 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2207 uint8_t flags[2] = { 0, 1 };
2208 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2209 << "Ignored device should never mark any key codes.";
2210 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2211 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2212}
2213
2214TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2215 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002216 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002217
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002218 FakeInputMapper& mapper1 =
2219 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002220 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2221 mapper1.setMetaState(AMETA_ALT_ON);
2222 mapper1.addSupportedKeyCode(AKEYCODE_A);
2223 mapper1.addSupportedKeyCode(AKEYCODE_B);
2224 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2225 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2226 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2227 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2228 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002229
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002230 FakeInputMapper& mapper2 =
2231 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002232 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002233
2234 InputReaderConfiguration config;
2235 mDevice->configure(ARBITRARY_TIME, &config, 0);
2236
2237 String8 propertyValue;
2238 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2239 << "Device should have read configuration during configuration phase.";
2240 ASSERT_STREQ("value", propertyValue.string());
2241
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002242 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2243 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002244
2245 // Reset
2246 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002247 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2248 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002249
2250 NotifyDeviceResetArgs resetArgs;
2251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2252 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2253 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2254
2255 // Metadata.
2256 ASSERT_FALSE(mDevice->isIgnored());
2257 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2258
2259 InputDeviceInfo info;
2260 mDevice->getDeviceInfo(&info);
2261 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002262 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002263 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2264 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2265
2266 // State queries.
2267 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2268 << "Should query mappers and combine meta states.";
2269
2270 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2271 << "Should return unknown key code state when source not supported.";
2272 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2273 << "Should return unknown scan code state when source not supported.";
2274 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2275 << "Should return unknown switch state when source not supported.";
2276
2277 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2278 << "Should query mapper when source is supported.";
2279 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2280 << "Should query mapper when source is supported.";
2281 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2282 << "Should query mapper when source is supported.";
2283
2284 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2285 uint8_t flags[4] = { 0, 0, 0, 1 };
2286 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2287 << "Should do nothing when source is unsupported.";
2288 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2289 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2290 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2291 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2292
2293 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2294 << "Should query mapper when source is supported.";
2295 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2296 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2297 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2298 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2299
2300 // Event handling.
2301 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002302 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303 mDevice->process(&event, 1);
2304
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002305 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2306 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002307}
2308
Arthur Hung2c9a3342019-07-23 14:18:59 +08002309// A single input device is associated with a specific display. Check that:
2310// 1. Device is disabled if the viewport corresponding to the associated display is not found
2311// 2. Device is disabled when setEnabled API is called
2312TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002313 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002314
2315 // First Configuration.
2316 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2317
2318 // Device should be enabled by default.
2319 ASSERT_TRUE(mDevice->isEnabled());
2320
2321 // Prepare associated info.
2322 constexpr uint8_t hdmi = 1;
2323 const std::string UNIQUE_ID = "local:1";
2324
2325 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2326 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2327 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2328 // Device should be disabled because it is associated with a specific display via
2329 // input port <-> display port association, but the corresponding display is not found
2330 ASSERT_FALSE(mDevice->isEnabled());
2331
2332 // Prepare displays.
2333 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002334 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002335 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2336 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2337 ASSERT_TRUE(mDevice->isEnabled());
2338
2339 // Device should be disabled after set disable.
2340 mFakePolicy->addDisabledDevice(mDevice->getId());
2341 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2342 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2343 ASSERT_FALSE(mDevice->isEnabled());
2344
2345 // Device should still be disabled even found the associated display.
2346 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2347 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2348 ASSERT_FALSE(mDevice->isEnabled());
2349}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002350
2351// --- InputMapperTest ---
2352
2353class InputMapperTest : public testing::Test {
2354protected:
2355 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002356 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002357 static const int32_t DEVICE_ID;
2358 static const int32_t DEVICE_GENERATION;
2359 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002360 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002361 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002362
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002363 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002364 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002365 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002366 std::unique_ptr<InstrumentedInputReader> mReader;
2367 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002368
Chris Ye1b0c7342020-07-28 21:57:03 -07002369 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002370 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002371 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002372 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002373 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2374 mFakeListener);
2375 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002376 }
2377
Chris Yea52ade12020-08-27 16:49:20 -07002378 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002379
Chris Yea52ade12020-08-27 16:49:20 -07002380 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381 mFakeListener.clear();
2382 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383 }
2384
2385 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002386 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387 }
2388
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002389 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002390 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002391 mReader->requestRefreshConfiguration(changes);
2392 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002393 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002394 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2395 }
2396
arthurhungdcef2dc2020-08-11 14:47:50 +08002397 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2398 const std::string& location, int32_t eventHubId,
2399 Flags<InputDeviceClass> classes) {
2400 InputDeviceIdentifier identifier;
2401 identifier.name = name;
2402 identifier.location = location;
2403 std::shared_ptr<InputDevice> device =
2404 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2405 identifier);
2406 mReader->pushNextDevice(device);
2407 mFakeEventHub->addDevice(eventHubId, name, classes);
2408 mReader->loopOnce();
2409 return device;
2410 }
2411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002412 template <class T, typename... Args>
2413 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002414 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002415 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002417 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002418 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419 }
2420
2421 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002422 int32_t orientation, const std::string& uniqueId,
2423 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002424 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002425 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002426 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2427 }
2428
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002429 void clearViewports() {
2430 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002431 }
2432
arthurhungdcef2dc2020-08-11 14:47:50 +08002433 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434 RawEvent event;
2435 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002436 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437 event.type = type;
2438 event.code = code;
2439 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002440 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002441 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002442 }
2443
2444 static void assertMotionRange(const InputDeviceInfo& info,
2445 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2446 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002447 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2449 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2450 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2451 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2452 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2453 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2454 }
2455
2456 static void assertPointerCoords(const PointerCoords& coords,
2457 float x, float y, float pressure, float size,
2458 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2459 float orientation, float distance) {
2460 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2461 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2462 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2463 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2464 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2465 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2466 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2467 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2468 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2469 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2470 }
2471
Michael Wright17db18e2020-06-26 20:51:44 +01002472 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002474 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002475 ASSERT_NEAR(x, actualX, 1);
2476 ASSERT_NEAR(y, actualY, 1);
2477 }
2478};
2479
2480const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002481const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002482const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2484const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002485const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2486 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002487const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002488
2489// --- SwitchInputMapperTest ---
2490
2491class SwitchInputMapperTest : public InputMapperTest {
2492protected:
2493};
2494
2495TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002496 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002498 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002499}
2500
2501TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002502 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002503
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002504 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002505 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002506
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002507 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002508 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002509}
2510
2511TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002512 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002514 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2515 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2516 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2517 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002518
2519 NotifySwitchArgs args;
2520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2521 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002522 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2523 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524 args.switchMask);
2525 ASSERT_EQ(uint32_t(0), args.policyFlags);
2526}
2527
Chris Ye87143712020-11-10 05:05:58 +00002528// --- VibratorInputMapperTest ---
2529class VibratorInputMapperTest : public InputMapperTest {
2530protected:
2531 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2532};
2533
2534TEST_F(VibratorInputMapperTest, GetSources) {
2535 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2536
2537 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2538}
2539
2540TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2541 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2542
2543 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2544}
2545
2546TEST_F(VibratorInputMapperTest, Vibrate) {
2547 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
2548 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2549
2550 VibrationElement pattern(2);
2551 VibrationSequence sequence(2);
2552 pattern.duration = std::chrono::milliseconds(200);
2553 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2554 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2555 sequence.addElement(pattern);
2556 pattern.duration = std::chrono::milliseconds(500);
2557 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2558 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2559 sequence.addElement(pattern);
2560
2561 std::vector<int64_t> timings = {0, 1};
2562 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2563
2564 ASSERT_FALSE(mapper.isVibrating());
2565 mapper.vibrate(sequence, -1 /* repeat */, 0 /* token */);
2566 ASSERT_TRUE(mapper.isVibrating());
2567}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002568
2569// --- KeyboardInputMapperTest ---
2570
2571class KeyboardInputMapperTest : public InputMapperTest {
2572protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002573 const std::string UNIQUE_ID = "local:0";
2574
2575 void prepareDisplay(int32_t orientation);
2576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002577 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002578 int32_t originalKeyCode, int32_t rotatedKeyCode,
2579 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580};
2581
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002582/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2583 * orientation.
2584 */
2585void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002586 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2587 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002588}
2589
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002590void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002591 int32_t originalScanCode, int32_t originalKeyCode,
2592 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002593 NotifyKeyArgs args;
2594
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002595 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2597 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2598 ASSERT_EQ(originalScanCode, args.scanCode);
2599 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002600 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002601
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002602 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2604 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2605 ASSERT_EQ(originalScanCode, args.scanCode);
2606 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002607 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002608}
2609
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002611 KeyboardInputMapper& mapper =
2612 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2613 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002615 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616}
2617
2618TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2619 const int32_t USAGE_A = 0x070004;
2620 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002621 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2622 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002623 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2624 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2625 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002627 KeyboardInputMapper& mapper =
2628 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2629 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002630 // Initial metastate to AMETA_NONE.
2631 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2632 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002633
2634 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002635 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636 NotifyKeyArgs args;
2637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2638 ASSERT_EQ(DEVICE_ID, args.deviceId);
2639 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2640 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2641 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2642 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2643 ASSERT_EQ(KEY_HOME, args.scanCode);
2644 ASSERT_EQ(AMETA_NONE, args.metaState);
2645 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2646 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2647 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2648
2649 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002650 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2652 ASSERT_EQ(DEVICE_ID, args.deviceId);
2653 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2654 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2655 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2656 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2657 ASSERT_EQ(KEY_HOME, args.scanCode);
2658 ASSERT_EQ(AMETA_NONE, args.metaState);
2659 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2660 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2661 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2662
2663 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002664 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2665 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2667 ASSERT_EQ(DEVICE_ID, args.deviceId);
2668 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2669 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2670 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2671 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2672 ASSERT_EQ(0, args.scanCode);
2673 ASSERT_EQ(AMETA_NONE, args.metaState);
2674 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2675 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2676 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2677
2678 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002679 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2680 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2682 ASSERT_EQ(DEVICE_ID, args.deviceId);
2683 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2684 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2685 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2686 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2687 ASSERT_EQ(0, args.scanCode);
2688 ASSERT_EQ(AMETA_NONE, args.metaState);
2689 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2690 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2691 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2692
2693 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002694 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2695 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2697 ASSERT_EQ(DEVICE_ID, args.deviceId);
2698 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2699 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2700 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2701 ASSERT_EQ(0, args.keyCode);
2702 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2703 ASSERT_EQ(AMETA_NONE, args.metaState);
2704 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2705 ASSERT_EQ(0U, args.policyFlags);
2706 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2707
2708 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002709 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2710 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2712 ASSERT_EQ(DEVICE_ID, args.deviceId);
2713 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2714 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2715 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2716 ASSERT_EQ(0, args.keyCode);
2717 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2718 ASSERT_EQ(AMETA_NONE, args.metaState);
2719 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2720 ASSERT_EQ(0U, args.policyFlags);
2721 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2722}
2723
2724TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002725 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2726 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002727 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2728 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2729 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002730
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002731 KeyboardInputMapper& mapper =
2732 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2733 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002734
arthurhungc903df12020-08-11 15:08:42 +08002735 // Initial metastate to AMETA_NONE.
2736 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2737 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738
2739 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002740 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741 NotifyKeyArgs args;
2742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2743 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002744 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002745 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002746
2747 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002748 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2750 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002751 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002752
2753 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002754 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2756 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002758
2759 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002760 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2762 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002763 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002764 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765}
2766
2767TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002768 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2769 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2771 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002773 KeyboardInputMapper& mapper =
2774 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2775 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002777 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2779 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2780 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2781 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2782 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2783 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2784 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2785 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2786}
2787
2788TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002789 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2790 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2791 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2792 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793
Michael Wrightd02c5b62014-02-10 15:10:22 -08002794 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002795 KeyboardInputMapper& mapper =
2796 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2797 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002799 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002800 ASSERT_NO_FATAL_FAILURE(
2801 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2802 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2803 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2804 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2805 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2806 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2807 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002809 clearViewports();
2810 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002811 ASSERT_NO_FATAL_FAILURE(
2812 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2813 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2814 AKEYCODE_DPAD_UP, DISPLAY_ID));
2815 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2816 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2817 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2818 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002820 clearViewports();
2821 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002822 ASSERT_NO_FATAL_FAILURE(
2823 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2824 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2825 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2826 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2827 AKEYCODE_DPAD_UP, DISPLAY_ID));
2828 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2829 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002831 clearViewports();
2832 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002833 ASSERT_NO_FATAL_FAILURE(
2834 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2835 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2836 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2837 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2838 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2839 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2840 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841
2842 // Special case: if orientation changes while key is down, we still emit the same keycode
2843 // in the key up as we did in the key down.
2844 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002845 clearViewports();
2846 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002847 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2849 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2850 ASSERT_EQ(KEY_UP, args.scanCode);
2851 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2852
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002853 clearViewports();
2854 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002855 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2857 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2858 ASSERT_EQ(KEY_UP, args.scanCode);
2859 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2860}
2861
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002862TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2863 // If the keyboard is not orientation aware,
2864 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002865 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002866
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002867 KeyboardInputMapper& mapper =
2868 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2869 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002870 NotifyKeyArgs args;
2871
2872 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002873 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002875 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2877 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2878
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002879 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002880 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002882 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2884 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2885}
2886
2887TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2888 // If the keyboard is orientation aware,
2889 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002890 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002891
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002892 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002893 KeyboardInputMapper& mapper =
2894 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2895 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002896 NotifyKeyArgs args;
2897
2898 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2899 // ^--- already checked by the previous test
2900
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002901 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002902 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002903 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002905 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2907 ASSERT_EQ(DISPLAY_ID, args.displayId);
2908
2909 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002910 clearViewports();
2911 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002912 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002913 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002915 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2917 ASSERT_EQ(newDisplayId, args.displayId);
2918}
2919
Michael Wrightd02c5b62014-02-10 15:10:22 -08002920TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002921 KeyboardInputMapper& mapper =
2922 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2923 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002925 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002926 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002928 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002929 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930}
2931
2932TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002933 KeyboardInputMapper& mapper =
2934 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2935 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002937 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002938 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002940 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002941 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002942}
2943
2944TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002945 KeyboardInputMapper& mapper =
2946 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2947 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002949 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950
2951 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2952 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002953 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954 ASSERT_TRUE(flags[0]);
2955 ASSERT_FALSE(flags[1]);
2956}
2957
2958TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002959 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2960 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2961 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2962 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2963 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002966 KeyboardInputMapper& mapper =
2967 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2968 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07002969 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08002970 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2971 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972
2973 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002974 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2975 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2976 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977
2978 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002979 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2980 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002981 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2982 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2983 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002984 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985
2986 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002987 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2988 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002989 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2990 ASSERT_TRUE(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 | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993
2994 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002995 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2996 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002997 ASSERT_FALSE(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_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001
3002 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003003 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3004 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 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_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003008 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009
3010 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003011 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3012 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003013 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3014 ASSERT_FALSE(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_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003017
3018 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003019 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3020 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 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_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003024 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025}
3026
Chris Yea52ade12020-08-27 16:49:20 -07003027TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3028 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3029 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3030 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3031 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3032
3033 KeyboardInputMapper& mapper =
3034 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3035 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3036
3037 // Initial metastate should be AMETA_NONE as no meta keys added.
3038 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3039 // Meta state should be AMETA_NONE after reset
3040 mapper.reset(ARBITRARY_TIME);
3041 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3042 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3043 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3044 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3045
3046 NotifyKeyArgs args;
3047 // Press button "A"
3048 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
3049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3050 ASSERT_EQ(AMETA_NONE, args.metaState);
3051 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3052 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3053 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3054
3055 // Button up.
3056 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
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_UP, args.action);
3061 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3062}
3063
Arthur Hung2c9a3342019-07-23 14:18:59 +08003064TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3065 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003066 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3067 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3068 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3069 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003070
3071 // keyboard 2.
3072 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003073 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003074 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003075 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003076 std::shared_ptr<InputDevice> device2 =
3077 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3078 Flags<InputDeviceClass>(0));
3079
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003080 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3081 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3082 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3083 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003084
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003085 KeyboardInputMapper& mapper =
3086 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3087 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003090 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003092 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3093 device2->reset(ARBITRARY_TIME);
3094
3095 // Prepared displays and associated info.
3096 constexpr uint8_t hdmi1 = 0;
3097 constexpr uint8_t hdmi2 = 1;
3098 const std::string SECONDARY_UNIQUE_ID = "local:1";
3099
3100 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3101 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3102
3103 // No associated display viewport found, should disable the device.
3104 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3105 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3106 ASSERT_FALSE(device2->isEnabled());
3107
3108 // Prepare second display.
3109 constexpr int32_t newDisplayId = 2;
3110 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003111 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003112 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003113 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003114 // Default device will reconfigure above, need additional reconfiguration for another device.
3115 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3116 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3117
3118 // Device should be enabled after the associated display is found.
3119 ASSERT_TRUE(mDevice->isEnabled());
3120 ASSERT_TRUE(device2->isEnabled());
3121
3122 // Test pad key events
3123 ASSERT_NO_FATAL_FAILURE(
3124 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3125 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3126 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3127 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3128 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3129 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3130 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3131
3132 ASSERT_NO_FATAL_FAILURE(
3133 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3134 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3135 AKEYCODE_DPAD_RIGHT, newDisplayId));
3136 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3137 AKEYCODE_DPAD_DOWN, newDisplayId));
3138 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3139 AKEYCODE_DPAD_LEFT, newDisplayId));
3140}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141
arthurhungc903df12020-08-11 15:08:42 +08003142TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3143 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3144 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3145 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3146 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3147 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3148 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3149
3150 KeyboardInputMapper& mapper =
3151 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3152 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3153 // Initial metastate to AMETA_NONE.
3154 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3155 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3156
3157 // Initialization should have turned all of the lights off.
3158 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3159 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3160 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3161
3162 // Toggle caps lock on.
3163 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3164 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3165 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3166 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3167
3168 // Toggle num lock on.
3169 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3170 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3171 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3172 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3173
3174 // Toggle scroll lock on.
3175 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3176 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3177 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3178 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3179
3180 mFakeEventHub->removeDevice(EVENTHUB_ID);
3181 mReader->loopOnce();
3182
3183 // keyboard 2 should default toggle keys.
3184 const std::string USB2 = "USB2";
3185 const std::string DEVICE_NAME2 = "KEYBOARD2";
3186 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3187 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3188 std::shared_ptr<InputDevice> device2 =
3189 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3190 Flags<InputDeviceClass>(0));
3191 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3192 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3193 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3194 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3195 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3196 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3197
arthurhung6fe95782020-10-05 22:41:16 +08003198 KeyboardInputMapper& mapper2 =
3199 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3200 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003201 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3202 device2->reset(ARBITRARY_TIME);
3203
3204 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3205 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3206 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003207 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3208 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003209}
3210
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003211// --- KeyboardInputMapperTest_ExternalDevice ---
3212
3213class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3214protected:
Chris Yea52ade12020-08-27 16:49:20 -07003215 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003216};
3217
3218TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003219 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3220 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003221
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003222 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3223 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3224 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3225 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003226
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003227 KeyboardInputMapper& mapper =
3228 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3229 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003230
3231 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3232 NotifyKeyArgs args;
3233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3234 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3235
3236 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3238 ASSERT_EQ(uint32_t(0), args.policyFlags);
3239
3240 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3242 ASSERT_EQ(uint32_t(0), args.policyFlags);
3243
3244 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 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_PLAYPAUSE, 1);
3249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3250 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3251
3252 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3254 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3255}
3256
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003257TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003258 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003259
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003260 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3261 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3262 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003263
Powei Fengd041c5d2019-05-03 17:11:33 -07003264 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 KeyboardInputMapper& mapper =
3266 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3267 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003268
3269 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3270 NotifyKeyArgs args;
3271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3272 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3273
3274 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3276 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3277
3278 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3280 ASSERT_EQ(uint32_t(0), args.policyFlags);
3281
3282 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3284 ASSERT_EQ(uint32_t(0), args.policyFlags);
3285
3286 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3288 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3289
3290 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3292 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3293}
3294
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295// --- CursorInputMapperTest ---
3296
3297class CursorInputMapperTest : public InputMapperTest {
3298protected:
3299 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3300
Michael Wright17db18e2020-06-26 20:51:44 +01003301 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302
Chris Yea52ade12020-08-27 16:49:20 -07003303 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304 InputMapperTest::SetUp();
3305
Michael Wright17db18e2020-06-26 20:51:44 +01003306 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003307 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308 }
3309
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003310 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3311 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003312
3313 void prepareDisplay(int32_t orientation) {
3314 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003315 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003316 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3317 orientation, uniqueId, NO_PORT, viewportType);
3318 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319};
3320
3321const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3322
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003323void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3324 int32_t originalY, int32_t rotatedX,
3325 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003326 NotifyMotionArgs args;
3327
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003328 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3329 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3330 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3334 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3335 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3336 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3337}
3338
3339TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003341 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003343 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344}
3345
3346TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003348 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003350 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351}
3352
3353TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003355 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356
3357 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003358 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359
3360 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003361 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3362 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3364 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3365
3366 // When the bounds are set, then there should be a valid motion range.
3367 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3368
3369 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003370 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371
3372 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3373 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3374 1, 800 - 1, 0.0f, 0.0f));
3375 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3376 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3377 2, 480 - 1, 0.0f, 0.0f));
3378 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3379 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3380 0.0f, 1.0f, 0.0f, 0.0f));
3381}
3382
3383TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003385 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386
3387 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003388 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389
3390 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3391 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3392 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3393 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3394 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3395 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3396 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3397 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3398 0.0f, 1.0f, 0.0f, 0.0f));
3399}
3400
3401TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003403 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404
arthurhungdcef2dc2020-08-11 14:47:50 +08003405 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406
3407 NotifyMotionArgs args;
3408
3409 // Button press.
3410 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003411 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3412 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3414 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3415 ASSERT_EQ(DEVICE_ID, args.deviceId);
3416 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3417 ASSERT_EQ(uint32_t(0), args.policyFlags);
3418 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3419 ASSERT_EQ(0, args.flags);
3420 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3421 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3422 ASSERT_EQ(0, args.edgeFlags);
3423 ASSERT_EQ(uint32_t(1), args.pointerCount);
3424 ASSERT_EQ(0, args.pointerProperties[0].id);
3425 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3427 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3428 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3429 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3430 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3431
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3433 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3434 ASSERT_EQ(DEVICE_ID, args.deviceId);
3435 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3436 ASSERT_EQ(uint32_t(0), args.policyFlags);
3437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3438 ASSERT_EQ(0, args.flags);
3439 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3440 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3441 ASSERT_EQ(0, args.edgeFlags);
3442 ASSERT_EQ(uint32_t(1), args.pointerCount);
3443 ASSERT_EQ(0, args.pointerProperties[0].id);
3444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3446 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3447 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3448 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3449 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3450
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003452 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3453 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3455 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3456 ASSERT_EQ(DEVICE_ID, args.deviceId);
3457 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3458 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3460 ASSERT_EQ(0, args.flags);
3461 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3462 ASSERT_EQ(0, args.buttonState);
3463 ASSERT_EQ(0, args.edgeFlags);
3464 ASSERT_EQ(uint32_t(1), args.pointerCount);
3465 ASSERT_EQ(0, args.pointerProperties[0].id);
3466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3468 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3469 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3470 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3471 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3472
3473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3474 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3475 ASSERT_EQ(DEVICE_ID, args.deviceId);
3476 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3477 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3479 ASSERT_EQ(0, args.flags);
3480 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3481 ASSERT_EQ(0, args.buttonState);
3482 ASSERT_EQ(0, args.edgeFlags);
3483 ASSERT_EQ(uint32_t(1), args.pointerCount);
3484 ASSERT_EQ(0, args.pointerProperties[0].id);
3485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3487 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3488 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3489 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3490 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3491}
3492
3493TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003495 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496
3497 NotifyMotionArgs args;
3498
3499 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003500 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3501 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3505 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3506
3507 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003508 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
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 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3514}
3515
3516TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519
3520 NotifyMotionArgs args;
3521
3522 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003523 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3524 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3526 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3528 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3529
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3533 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3534
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003536 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3537 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003539 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3541 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3542
3543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3546 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3547}
3548
3549TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003551 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552
3553 NotifyMotionArgs args;
3554
3555 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003556 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3557 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3558 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3559 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3561 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3563 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3564 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3565
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3567 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3569 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3570 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3571
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003573 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3574 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3575 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3579 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3580 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3581
3582 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003583 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3584 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3588 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3589
3590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3593 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3594}
3595
3596TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003597 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003598 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003599
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003600 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003601 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3602 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3603 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3604 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3605 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3606 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3607 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3608 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3609}
3610
3611TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612 addConfigurationProperty("cursor.mode", "navigation");
3613 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003614 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003615
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003616 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3618 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3619 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3620 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3621 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3622 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3623 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3624 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3625
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003626 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3628 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3629 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3630 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3631 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3632 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3633 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3634 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3635
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003636 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003637 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3638 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3639 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3640 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3641 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3642 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3643 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3644 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3645
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003646 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3648 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3649 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3650 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3651 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3652 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3653 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3654 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3655}
3656
3657TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003659 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003660
3661 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3662 mFakePointerController->setPosition(100, 200);
3663 mFakePointerController->setButtonState(0);
3664
3665 NotifyMotionArgs motionArgs;
3666 NotifyKeyArgs keyArgs;
3667
3668 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003669 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3670 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3672 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3673 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3674 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3676 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3677
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3680 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3681 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3683 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3684
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003685 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3686 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003688 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689 ASSERT_EQ(0, motionArgs.buttonState);
3690 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3692 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3693
3694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003695 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696 ASSERT_EQ(0, motionArgs.buttonState);
3697 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3699 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3700
3701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003703 ASSERT_EQ(0, motionArgs.buttonState);
3704 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3706 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3707
3708 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003709 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3710 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3711 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3713 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3714 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3715 motionArgs.buttonState);
3716 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3717 mFakePointerController->getButtonState());
3718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3719 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3720
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3722 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3723 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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
3729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3731 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3732 motionArgs.buttonState);
3733 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3734 mFakePointerController->getButtonState());
3735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3736 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3737
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003738 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3739 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003741 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003742 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3743 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3745 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3746
3747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003749 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3750 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3752 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3753
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003754 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3755 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003757 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3758 ASSERT_EQ(0, motionArgs.buttonState);
3759 ASSERT_EQ(0, mFakePointerController->getButtonState());
3760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3761 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 -08003762 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3763 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003764
3765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766 ASSERT_EQ(0, motionArgs.buttonState);
3767 ASSERT_EQ(0, mFakePointerController->getButtonState());
3768 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3770 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 -08003771
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3773 ASSERT_EQ(0, motionArgs.buttonState);
3774 ASSERT_EQ(0, mFakePointerController->getButtonState());
3775 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3777 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3778
3779 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003780 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3781 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3783 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3784 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003785
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3789 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3791 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3792
3793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3795 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3796 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3798 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3799
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003800 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3801 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003803 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804 ASSERT_EQ(0, motionArgs.buttonState);
3805 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3807 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3808
3809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003811 ASSERT_EQ(0, motionArgs.buttonState);
3812 ASSERT_EQ(0, mFakePointerController->getButtonState());
3813
Michael Wrightd02c5b62014-02-10 15:10:22 -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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3817 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3818 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3819
3820 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003821 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3822 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3824 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3825 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003826
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003828 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3830 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3832 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3833
3834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3836 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3837 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3839 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3840
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003841 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3842 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003844 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003845 ASSERT_EQ(0, motionArgs.buttonState);
3846 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3848 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 -08003849
3850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3851 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3852 ASSERT_EQ(0, motionArgs.buttonState);
3853 ASSERT_EQ(0, mFakePointerController->getButtonState());
3854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3855 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3856
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3858 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3859 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3860
3861 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003862 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3863 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3865 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3866 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003867
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003869 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3871 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3873 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3874
3875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3876 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3877 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3878 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3880 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3881
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003882 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3883 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003885 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886 ASSERT_EQ(0, motionArgs.buttonState);
3887 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3889 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 -08003890
3891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3892 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3893 ASSERT_EQ(0, motionArgs.buttonState);
3894 ASSERT_EQ(0, mFakePointerController->getButtonState());
3895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3896 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3897
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3899 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3900 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3901
3902 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003903 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3904 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3906 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3907 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003908
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003910 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3912 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3914 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3915
3916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3917 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3918 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3919 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3921 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3922
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003923 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3924 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003926 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 ASSERT_EQ(0, motionArgs.buttonState);
3928 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3930 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 -08003931
3932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3933 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3934 ASSERT_EQ(0, motionArgs.buttonState);
3935 ASSERT_EQ(0, mFakePointerController->getButtonState());
3936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3937 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3938
Michael Wrightd02c5b62014-02-10 15:10:22 -08003939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3940 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3941 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3942}
3943
3944TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003946 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003947
3948 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3949 mFakePointerController->setPosition(100, 200);
3950 mFakePointerController->setButtonState(0);
3951
3952 NotifyMotionArgs args;
3953
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003954 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3955 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3956 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003958 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3959 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3961 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 +01003962 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003963}
3964
3965TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003966 addConfigurationProperty("cursor.mode", "pointer");
3967 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003968 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003969
3970 NotifyDeviceResetArgs resetArgs;
3971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3972 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3973 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3974
3975 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3976 mFakePointerController->setPosition(100, 200);
3977 mFakePointerController->setButtonState(0);
3978
3979 NotifyMotionArgs args;
3980
3981 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003982 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3983 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3984 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3986 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3989 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 +01003990 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003991
3992 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003993 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3994 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3996 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3997 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3999 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4001 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4002 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4004 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4005
4006 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004007 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
4008 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4010 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4011 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4013 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4015 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4016 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4018 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4019
4020 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004021 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
4022 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
4023 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4025 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4028 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 +01004029 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004030
4031 // Disable pointer capture and check that the device generation got bumped
4032 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004033 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004034 mFakePolicy->setPointerCapture(false);
4035 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004036 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004037
4038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4039 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4040 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4041
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004042 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4043 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4044 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4046 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4049 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 +01004050 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051}
4052
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004053TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004054 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004055
Garfield Tan888a6a42020-01-09 11:39:16 -08004056 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004057 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004058 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4059 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004060 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004061 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4062 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4063
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004064 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4065 mFakePointerController->setPosition(100, 200);
4066 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004067
4068 NotifyMotionArgs args;
4069 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4070 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4071 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
4072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4073 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4074 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4076 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 +01004077 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004078 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4079}
4080
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081// --- TouchInputMapperTest ---
4082
4083class TouchInputMapperTest : public InputMapperTest {
4084protected:
4085 static const int32_t RAW_X_MIN;
4086 static const int32_t RAW_X_MAX;
4087 static const int32_t RAW_Y_MIN;
4088 static const int32_t RAW_Y_MAX;
4089 static const int32_t RAW_TOUCH_MIN;
4090 static const int32_t RAW_TOUCH_MAX;
4091 static const int32_t RAW_TOOL_MIN;
4092 static const int32_t RAW_TOOL_MAX;
4093 static const int32_t RAW_PRESSURE_MIN;
4094 static const int32_t RAW_PRESSURE_MAX;
4095 static const int32_t RAW_ORIENTATION_MIN;
4096 static const int32_t RAW_ORIENTATION_MAX;
4097 static const int32_t RAW_DISTANCE_MIN;
4098 static const int32_t RAW_DISTANCE_MAX;
4099 static const int32_t RAW_TILT_MIN;
4100 static const int32_t RAW_TILT_MAX;
4101 static const int32_t RAW_ID_MIN;
4102 static const int32_t RAW_ID_MAX;
4103 static const int32_t RAW_SLOT_MIN;
4104 static const int32_t RAW_SLOT_MAX;
4105 static const float X_PRECISION;
4106 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004107 static const float X_PRECISION_VIRTUAL;
4108 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109
4110 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004111 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112
4113 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4114
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004115 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004116 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004117
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118 enum Axes {
4119 POSITION = 1 << 0,
4120 TOUCH = 1 << 1,
4121 TOOL = 1 << 2,
4122 PRESSURE = 1 << 3,
4123 ORIENTATION = 1 << 4,
4124 MINOR = 1 << 5,
4125 ID = 1 << 6,
4126 DISTANCE = 1 << 7,
4127 TILT = 1 << 8,
4128 SLOT = 1 << 9,
4129 TOOL_TYPE = 1 << 10,
4130 };
4131
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004132 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4133 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004134 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004136 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137 int32_t toRawX(float displayX);
4138 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004139 float toCookedX(float rawX, float rawY);
4140 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004142 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004144 float toDisplayY(int32_t rawY, int32_t displayHeight);
4145
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146};
4147
4148const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4149const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4150const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4151const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4152const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4153const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4154const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4155const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004156const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4157const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004158const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4159const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4160const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4161const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4162const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4163const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4164const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4165const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4166const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4167const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4168const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4169const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004170const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4171 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4172const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4173 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004174const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4175 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176
4177const float TouchInputMapperTest::GEOMETRIC_SCALE =
4178 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4179 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4180
4181const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4182 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4183 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4184};
4185
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004186void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004187 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4188 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004189}
4190
4191void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4192 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4193 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194}
4195
Santos Cordonfa5cf462017-04-05 10:37:00 -07004196void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004197 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4198 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4199 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004200}
4201
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004203 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4204 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4205 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4206 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207}
4208
Jason Gerecke489fda82012-09-07 17:19:40 -07004209void TouchInputMapperTest::prepareLocationCalibration() {
4210 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4211}
4212
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213int32_t TouchInputMapperTest::toRawX(float displayX) {
4214 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4215}
4216
4217int32_t TouchInputMapperTest::toRawY(float displayY) {
4218 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4219}
4220
Jason Gerecke489fda82012-09-07 17:19:40 -07004221float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4222 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4223 return rawX;
4224}
4225
4226float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4227 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4228 return rawY;
4229}
4230
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004232 return toDisplayX(rawX, DISPLAY_WIDTH);
4233}
4234
4235float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4236 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237}
4238
4239float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004240 return toDisplayY(rawY, DISPLAY_HEIGHT);
4241}
4242
4243float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4244 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245}
4246
4247
4248// --- SingleTouchInputMapperTest ---
4249
4250class SingleTouchInputMapperTest : public TouchInputMapperTest {
4251protected:
4252 void prepareButtons();
4253 void prepareAxes(int axes);
4254
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004255 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4256 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4257 void processUp(SingleTouchInputMapper& mappery);
4258 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4259 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4260 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4261 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4262 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4263 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264};
4265
4266void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004267 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268}
4269
4270void SingleTouchInputMapperTest::prepareAxes(int axes) {
4271 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004272 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4273 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 }
4275 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004276 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4277 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 }
4279 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004280 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4281 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 }
4283 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004284 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4285 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 }
4287 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004288 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4289 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 }
4291}
4292
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004293void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004294 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4295 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4296 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297}
4298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004299void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004300 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4301 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302}
4303
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004304void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004305 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306}
4307
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004308void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004309 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310}
4311
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004312void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4313 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004314 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315}
4316
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004317void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004318 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319}
4320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004321void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4322 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004323 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4324 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325}
4326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004327void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4328 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004329 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330}
4331
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004332void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004333 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334}
4335
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 prepareButtons();
4338 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004339 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004341 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342}
4343
4344TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004345 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4346 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347 prepareButtons();
4348 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004349 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004351 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352}
4353
4354TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 prepareButtons();
4356 prepareAxes(POSITION);
4357 addConfigurationProperty("touch.deviceType", "touchPad");
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_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 prepareButtons();
4365 prepareAxes(POSITION);
4366 addConfigurationProperty("touch.deviceType", "touchScreen");
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_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370}
4371
4372TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 addConfigurationProperty("touch.deviceType", "touchScreen");
4374 prepareDisplay(DISPLAY_ORIENTATION_0);
4375 prepareButtons();
4376 prepareAxes(POSITION);
4377 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004378 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379
4380 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004381 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382
4383 // Virtual key is down.
4384 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4385 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4386 processDown(mapper, x, y);
4387 processSync(mapper);
4388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004390 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391
4392 // Virtual key is up.
4393 processUp(mapper);
4394 processSync(mapper);
4395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004397 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398}
4399
4400TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 addConfigurationProperty("touch.deviceType", "touchScreen");
4402 prepareDisplay(DISPLAY_ORIENTATION_0);
4403 prepareButtons();
4404 prepareAxes(POSITION);
4405 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004406 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407
4408 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004409 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410
4411 // Virtual key is down.
4412 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4413 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4414 processDown(mapper, x, y);
4415 processSync(mapper);
4416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004418 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419
4420 // Virtual key is up.
4421 processUp(mapper);
4422 processSync(mapper);
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4424
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004425 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426}
4427
4428TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 addConfigurationProperty("touch.deviceType", "touchScreen");
4430 prepareDisplay(DISPLAY_ORIENTATION_0);
4431 prepareButtons();
4432 prepareAxes(POSITION);
4433 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004434 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435
4436 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4437 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004438 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 ASSERT_TRUE(flags[0]);
4440 ASSERT_FALSE(flags[1]);
4441}
4442
4443TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 addConfigurationProperty("touch.deviceType", "touchScreen");
4445 prepareDisplay(DISPLAY_ORIENTATION_0);
4446 prepareButtons();
4447 prepareAxes(POSITION);
4448 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004449 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450
arthurhungdcef2dc2020-08-11 14:47:50 +08004451 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452
4453 NotifyKeyArgs args;
4454
4455 // Press virtual key.
4456 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4457 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4458 processDown(mapper, x, y);
4459 processSync(mapper);
4460
4461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4462 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4463 ASSERT_EQ(DEVICE_ID, args.deviceId);
4464 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4465 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4466 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4467 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4468 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4469 ASSERT_EQ(KEY_HOME, args.scanCode);
4470 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4471 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4472
4473 // Release virtual key.
4474 processUp(mapper);
4475 processSync(mapper);
4476
4477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4478 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4479 ASSERT_EQ(DEVICE_ID, args.deviceId);
4480 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4481 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4482 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4483 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4484 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4485 ASSERT_EQ(KEY_HOME, args.scanCode);
4486 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4487 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4488
4489 // Should not have sent any motions.
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4491}
4492
4493TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 addConfigurationProperty("touch.deviceType", "touchScreen");
4495 prepareDisplay(DISPLAY_ORIENTATION_0);
4496 prepareButtons();
4497 prepareAxes(POSITION);
4498 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004499 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500
arthurhungdcef2dc2020-08-11 14:47:50 +08004501 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502
4503 NotifyKeyArgs keyArgs;
4504
4505 // Press virtual key.
4506 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4507 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4508 processDown(mapper, x, y);
4509 processSync(mapper);
4510
4511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4512 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4513 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4514 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4515 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4516 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4517 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4518 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4519 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4520 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4521 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4522
4523 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4524 // into the display area.
4525 y -= 100;
4526 processMove(mapper, x, y);
4527 processSync(mapper);
4528
4529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4530 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4531 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4532 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4533 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4534 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4535 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4536 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4537 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4538 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4539 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4540 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4541
4542 NotifyMotionArgs motionArgs;
4543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4545 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4546 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4547 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4549 ASSERT_EQ(0, motionArgs.flags);
4550 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4551 ASSERT_EQ(0, motionArgs.buttonState);
4552 ASSERT_EQ(0, motionArgs.edgeFlags);
4553 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4554 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4557 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4558 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4559 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4560 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4561
4562 // Keep moving out of bounds. Should generate a pointer move.
4563 y -= 50;
4564 processMove(mapper, x, y);
4565 processSync(mapper);
4566
4567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4569 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4570 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4571 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4573 ASSERT_EQ(0, motionArgs.flags);
4574 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4575 ASSERT_EQ(0, motionArgs.buttonState);
4576 ASSERT_EQ(0, motionArgs.edgeFlags);
4577 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4578 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4581 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4582 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4583 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4585
4586 // Release out of bounds. Should generate a pointer up.
4587 processUp(mapper);
4588 processSync(mapper);
4589
4590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4592 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4593 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4594 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4595 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4596 ASSERT_EQ(0, motionArgs.flags);
4597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4598 ASSERT_EQ(0, motionArgs.buttonState);
4599 ASSERT_EQ(0, motionArgs.edgeFlags);
4600 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4604 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4605 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4606 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4607 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4608
4609 // Should not have sent any more keys or motions.
4610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4612}
4613
4614TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615 addConfigurationProperty("touch.deviceType", "touchScreen");
4616 prepareDisplay(DISPLAY_ORIENTATION_0);
4617 prepareButtons();
4618 prepareAxes(POSITION);
4619 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004620 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621
arthurhungdcef2dc2020-08-11 14:47:50 +08004622 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623
4624 NotifyMotionArgs motionArgs;
4625
4626 // Initially go down out of bounds.
4627 int32_t x = -10;
4628 int32_t y = -10;
4629 processDown(mapper, x, y);
4630 processSync(mapper);
4631
4632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4633
4634 // Move into the display area. Should generate a pointer down.
4635 x = 50;
4636 y = 75;
4637 processMove(mapper, x, y);
4638 processSync(mapper);
4639
4640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4641 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4642 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4643 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4644 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4645 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4646 ASSERT_EQ(0, motionArgs.flags);
4647 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4648 ASSERT_EQ(0, motionArgs.buttonState);
4649 ASSERT_EQ(0, motionArgs.edgeFlags);
4650 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4651 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4654 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4655 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4656 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4657 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4658
4659 // Release. Should generate a pointer up.
4660 processUp(mapper);
4661 processSync(mapper);
4662
4663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4664 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4665 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4666 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4667 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4668 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4669 ASSERT_EQ(0, motionArgs.flags);
4670 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4671 ASSERT_EQ(0, motionArgs.buttonState);
4672 ASSERT_EQ(0, motionArgs.edgeFlags);
4673 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4674 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4677 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4678 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4679 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4680 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4681
4682 // Should not have sent any more keys or motions.
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4685}
4686
Santos Cordonfa5cf462017-04-05 10:37:00 -07004687TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004688 addConfigurationProperty("touch.deviceType", "touchScreen");
4689 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4690
4691 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4692 prepareButtons();
4693 prepareAxes(POSITION);
4694 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004695 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004696
arthurhungdcef2dc2020-08-11 14:47:50 +08004697 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004698
4699 NotifyMotionArgs motionArgs;
4700
4701 // Down.
4702 int32_t x = 100;
4703 int32_t y = 125;
4704 processDown(mapper, x, y);
4705 processSync(mapper);
4706
4707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4708 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4709 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4710 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4711 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4712 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4713 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4714 ASSERT_EQ(0, motionArgs.flags);
4715 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4716 ASSERT_EQ(0, motionArgs.buttonState);
4717 ASSERT_EQ(0, motionArgs.edgeFlags);
4718 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4719 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4722 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4723 1, 0, 0, 0, 0, 0, 0, 0));
4724 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4725 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4726 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4727
4728 // Move.
4729 x += 50;
4730 y += 75;
4731 processMove(mapper, x, y);
4732 processSync(mapper);
4733
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4735 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4736 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4737 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4738 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4739 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4741 ASSERT_EQ(0, motionArgs.flags);
4742 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4743 ASSERT_EQ(0, motionArgs.buttonState);
4744 ASSERT_EQ(0, motionArgs.edgeFlags);
4745 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4746 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4747 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4749 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4750 1, 0, 0, 0, 0, 0, 0, 0));
4751 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4752 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4753 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4754
4755 // Up.
4756 processUp(mapper);
4757 processSync(mapper);
4758
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4760 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4761 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4762 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4763 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4764 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4765 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4766 ASSERT_EQ(0, motionArgs.flags);
4767 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4768 ASSERT_EQ(0, motionArgs.buttonState);
4769 ASSERT_EQ(0, motionArgs.edgeFlags);
4770 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4771 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4774 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4775 1, 0, 0, 0, 0, 0, 0, 0));
4776 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4777 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4778 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4779
4780 // Should not have sent any more keys or motions.
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4783}
4784
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 addConfigurationProperty("touch.deviceType", "touchScreen");
4787 prepareDisplay(DISPLAY_ORIENTATION_0);
4788 prepareButtons();
4789 prepareAxes(POSITION);
4790 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004791 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792
arthurhungdcef2dc2020-08-11 14:47:50 +08004793 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794
4795 NotifyMotionArgs motionArgs;
4796
4797 // Down.
4798 int32_t x = 100;
4799 int32_t y = 125;
4800 processDown(mapper, x, y);
4801 processSync(mapper);
4802
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4804 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4805 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4806 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4807 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4808 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4809 ASSERT_EQ(0, motionArgs.flags);
4810 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4811 ASSERT_EQ(0, motionArgs.buttonState);
4812 ASSERT_EQ(0, motionArgs.edgeFlags);
4813 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4814 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4815 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4817 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4818 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4819 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4820 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4821
4822 // Move.
4823 x += 50;
4824 y += 75;
4825 processMove(mapper, x, y);
4826 processSync(mapper);
4827
4828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4830 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4831 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4832 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4834 ASSERT_EQ(0, motionArgs.flags);
4835 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4836 ASSERT_EQ(0, motionArgs.buttonState);
4837 ASSERT_EQ(0, motionArgs.edgeFlags);
4838 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4842 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4843 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4844 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4845 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4846
4847 // Up.
4848 processUp(mapper);
4849 processSync(mapper);
4850
4851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4852 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4853 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4854 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4855 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4856 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4857 ASSERT_EQ(0, motionArgs.flags);
4858 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4859 ASSERT_EQ(0, motionArgs.buttonState);
4860 ASSERT_EQ(0, motionArgs.edgeFlags);
4861 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4862 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4863 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4865 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4866 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4867 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4868 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4869
4870 // Should not have sent any more keys or motions.
4871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4873}
4874
4875TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876 addConfigurationProperty("touch.deviceType", "touchScreen");
4877 prepareButtons();
4878 prepareAxes(POSITION);
4879 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004880 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881
4882 NotifyMotionArgs args;
4883
4884 // Rotation 90.
4885 prepareDisplay(DISPLAY_ORIENTATION_90);
4886 processDown(mapper, toRawX(50), toRawY(75));
4887 processSync(mapper);
4888
4889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4890 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4891 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4892
4893 processUp(mapper);
4894 processSync(mapper);
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4896}
4897
4898TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899 addConfigurationProperty("touch.deviceType", "touchScreen");
4900 prepareButtons();
4901 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004902 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903
4904 NotifyMotionArgs args;
4905
4906 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004907 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 prepareDisplay(DISPLAY_ORIENTATION_0);
4909 processDown(mapper, toRawX(50), toRawY(75));
4910 processSync(mapper);
4911
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4913 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4914 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4915
4916 processUp(mapper);
4917 processSync(mapper);
4918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4919
4920 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004921 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 prepareDisplay(DISPLAY_ORIENTATION_90);
4923 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4924 processSync(mapper);
4925
4926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4927 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4928 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4929
4930 processUp(mapper);
4931 processSync(mapper);
4932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4933
4934 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004935 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 prepareDisplay(DISPLAY_ORIENTATION_180);
4937 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4938 processSync(mapper);
4939
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4941 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4942 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4943
4944 processUp(mapper);
4945 processSync(mapper);
4946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4947
4948 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004949 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950 prepareDisplay(DISPLAY_ORIENTATION_270);
4951 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4952 processSync(mapper);
4953
4954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4955 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4956 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4957
4958 processUp(mapper);
4959 processSync(mapper);
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4961}
4962
4963TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 addConfigurationProperty("touch.deviceType", "touchScreen");
4965 prepareDisplay(DISPLAY_ORIENTATION_0);
4966 prepareButtons();
4967 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004968 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969
4970 // These calculations are based on the input device calibration documentation.
4971 int32_t rawX = 100;
4972 int32_t rawY = 200;
4973 int32_t rawPressure = 10;
4974 int32_t rawToolMajor = 12;
4975 int32_t rawDistance = 2;
4976 int32_t rawTiltX = 30;
4977 int32_t rawTiltY = 110;
4978
4979 float x = toDisplayX(rawX);
4980 float y = toDisplayY(rawY);
4981 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4982 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4983 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4984 float distance = float(rawDistance);
4985
4986 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4987 float tiltScale = M_PI / 180;
4988 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4989 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4990 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4991 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4992
4993 processDown(mapper, rawX, rawY);
4994 processPressure(mapper, rawPressure);
4995 processToolMajor(mapper, rawToolMajor);
4996 processDistance(mapper, rawDistance);
4997 processTilt(mapper, rawTiltX, rawTiltY);
4998 processSync(mapper);
4999
5000 NotifyMotionArgs args;
5001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5003 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5004 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5005}
5006
Jason Gerecke489fda82012-09-07 17:19:40 -07005007TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005008 addConfigurationProperty("touch.deviceType", "touchScreen");
5009 prepareDisplay(DISPLAY_ORIENTATION_0);
5010 prepareLocationCalibration();
5011 prepareButtons();
5012 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005013 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005014
5015 int32_t rawX = 100;
5016 int32_t rawY = 200;
5017
5018 float x = toDisplayX(toCookedX(rawX, rawY));
5019 float y = toDisplayY(toCookedY(rawX, rawY));
5020
5021 processDown(mapper, rawX, rawY);
5022 processSync(mapper);
5023
5024 NotifyMotionArgs args;
5025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5027 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5028}
5029
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005031 addConfigurationProperty("touch.deviceType", "touchScreen");
5032 prepareDisplay(DISPLAY_ORIENTATION_0);
5033 prepareButtons();
5034 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005035 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005036
5037 NotifyMotionArgs motionArgs;
5038 NotifyKeyArgs keyArgs;
5039
5040 processDown(mapper, 100, 200);
5041 processSync(mapper);
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5043 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5044 ASSERT_EQ(0, motionArgs.buttonState);
5045
5046 // press BTN_LEFT, release BTN_LEFT
5047 processKey(mapper, BTN_LEFT, 1);
5048 processSync(mapper);
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5051 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5052
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5055 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5056
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 processKey(mapper, BTN_LEFT, 0);
5058 processSync(mapper);
5059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005062
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005065 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066
5067 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5068 processKey(mapper, BTN_RIGHT, 1);
5069 processKey(mapper, BTN_MIDDLE, 1);
5070 processSync(mapper);
5071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5073 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5074 motionArgs.buttonState);
5075
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5078 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5079
5080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5082 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5083 motionArgs.buttonState);
5084
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085 processKey(mapper, BTN_RIGHT, 0);
5086 processSync(mapper);
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005090
5091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005093 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005094
5095 processKey(mapper, BTN_MIDDLE, 0);
5096 processSync(mapper);
5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005100
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005103 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104
5105 // press BTN_BACK, release BTN_BACK
5106 processKey(mapper, BTN_BACK, 1);
5107 processSync(mapper);
5108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5109 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5110 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005111
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005114 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5115
5116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5118 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119
5120 processKey(mapper, BTN_BACK, 0);
5121 processSync(mapper);
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005123 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005125
5126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005128 ASSERT_EQ(0, motionArgs.buttonState);
5129
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5131 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5132 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5133
5134 // press BTN_SIDE, release BTN_SIDE
5135 processKey(mapper, BTN_SIDE, 1);
5136 processSync(mapper);
5137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5138 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5139 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005140
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005143 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5144
5145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5146 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5147 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148
5149 processKey(mapper, BTN_SIDE, 0);
5150 processSync(mapper);
5151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005152 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005154
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005157 ASSERT_EQ(0, motionArgs.buttonState);
5158
Michael Wrightd02c5b62014-02-10 15:10:22 -08005159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5160 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5161 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5162
5163 // press BTN_FORWARD, release BTN_FORWARD
5164 processKey(mapper, BTN_FORWARD, 1);
5165 processSync(mapper);
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5167 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5168 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005169
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005172 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5173
5174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5176 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177
5178 processKey(mapper, BTN_FORWARD, 0);
5179 processSync(mapper);
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005183
5184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005186 ASSERT_EQ(0, motionArgs.buttonState);
5187
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5189 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5190 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5191
5192 // press BTN_EXTRA, release BTN_EXTRA
5193 processKey(mapper, BTN_EXTRA, 1);
5194 processSync(mapper);
5195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5196 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5197 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005198
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005201 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5202
5203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5205 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206
5207 processKey(mapper, BTN_EXTRA, 0);
5208 processSync(mapper);
5209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005212
5213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005215 ASSERT_EQ(0, motionArgs.buttonState);
5216
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5218 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5219 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5220
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5222
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223 // press BTN_STYLUS, release BTN_STYLUS
5224 processKey(mapper, BTN_STYLUS, 1);
5225 processSync(mapper);
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005228 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5229
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5232 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005233
5234 processKey(mapper, BTN_STYLUS, 0);
5235 processSync(mapper);
5236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005237 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005239
5240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005242 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005243
5244 // press BTN_STYLUS2, release BTN_STYLUS2
5245 processKey(mapper, BTN_STYLUS2, 1);
5246 processSync(mapper);
5247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005249 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5250
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5253 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254
5255 processKey(mapper, BTN_STYLUS2, 0);
5256 processSync(mapper);
5257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005258 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005260
5261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005263 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264
5265 // release touch
5266 processUp(mapper);
5267 processSync(mapper);
5268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5269 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5270 ASSERT_EQ(0, motionArgs.buttonState);
5271}
5272
5273TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274 addConfigurationProperty("touch.deviceType", "touchScreen");
5275 prepareDisplay(DISPLAY_ORIENTATION_0);
5276 prepareButtons();
5277 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005278 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279
5280 NotifyMotionArgs motionArgs;
5281
5282 // default tool type is finger
5283 processDown(mapper, 100, 200);
5284 processSync(mapper);
5285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5286 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5288
5289 // eraser
5290 processKey(mapper, BTN_TOOL_RUBBER, 1);
5291 processSync(mapper);
5292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5295
5296 // stylus
5297 processKey(mapper, BTN_TOOL_RUBBER, 0);
5298 processKey(mapper, BTN_TOOL_PEN, 1);
5299 processSync(mapper);
5300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5303
5304 // brush
5305 processKey(mapper, BTN_TOOL_PEN, 0);
5306 processKey(mapper, BTN_TOOL_BRUSH, 1);
5307 processSync(mapper);
5308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5311
5312 // pencil
5313 processKey(mapper, BTN_TOOL_BRUSH, 0);
5314 processKey(mapper, BTN_TOOL_PENCIL, 1);
5315 processSync(mapper);
5316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5319
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005320 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321 processKey(mapper, BTN_TOOL_PENCIL, 0);
5322 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5323 processSync(mapper);
5324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5327
5328 // mouse
5329 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5330 processKey(mapper, BTN_TOOL_MOUSE, 1);
5331 processSync(mapper);
5332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5335
5336 // lens
5337 processKey(mapper, BTN_TOOL_MOUSE, 0);
5338 processKey(mapper, BTN_TOOL_LENS, 1);
5339 processSync(mapper);
5340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5343
5344 // double-tap
5345 processKey(mapper, BTN_TOOL_LENS, 0);
5346 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5347 processSync(mapper);
5348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5351
5352 // triple-tap
5353 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5354 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5355 processSync(mapper);
5356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5359
5360 // quad-tap
5361 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5362 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5363 processSync(mapper);
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5367
5368 // finger
5369 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5370 processKey(mapper, BTN_TOOL_FINGER, 1);
5371 processSync(mapper);
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5375
5376 // stylus trumps finger
5377 processKey(mapper, BTN_TOOL_PEN, 1);
5378 processSync(mapper);
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5382
5383 // eraser trumps stylus
5384 processKey(mapper, BTN_TOOL_RUBBER, 1);
5385 processSync(mapper);
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5389
5390 // mouse trumps eraser
5391 processKey(mapper, BTN_TOOL_MOUSE, 1);
5392 processSync(mapper);
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5395 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5396
5397 // back to default tool type
5398 processKey(mapper, BTN_TOOL_MOUSE, 0);
5399 processKey(mapper, BTN_TOOL_RUBBER, 0);
5400 processKey(mapper, BTN_TOOL_PEN, 0);
5401 processKey(mapper, BTN_TOOL_FINGER, 0);
5402 processSync(mapper);
5403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5406}
5407
5408TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409 addConfigurationProperty("touch.deviceType", "touchScreen");
5410 prepareDisplay(DISPLAY_ORIENTATION_0);
5411 prepareButtons();
5412 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005413 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005414 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415
5416 NotifyMotionArgs motionArgs;
5417
5418 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5419 processKey(mapper, BTN_TOOL_FINGER, 1);
5420 processMove(mapper, 100, 200);
5421 processSync(mapper);
5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5423 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5425 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5426
5427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5430 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5431
5432 // move a little
5433 processMove(mapper, 150, 250);
5434 processSync(mapper);
5435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5436 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5438 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5439
5440 // down when BTN_TOUCH is pressed, pressure defaults to 1
5441 processKey(mapper, BTN_TOUCH, 1);
5442 processSync(mapper);
5443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5444 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5446 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5447
5448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5449 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5451 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5452
5453 // up when BTN_TOUCH is released, hover restored
5454 processKey(mapper, BTN_TOUCH, 0);
5455 processSync(mapper);
5456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5457 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5459 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5460
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5462 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5464 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5465
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5467 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5469 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5470
5471 // exit hover when pointer goes away
5472 processKey(mapper, BTN_TOOL_FINGER, 0);
5473 processSync(mapper);
5474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5475 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5477 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5478}
5479
5480TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005481 addConfigurationProperty("touch.deviceType", "touchScreen");
5482 prepareDisplay(DISPLAY_ORIENTATION_0);
5483 prepareButtons();
5484 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005485 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005486
5487 NotifyMotionArgs motionArgs;
5488
5489 // initially hovering because pressure is 0
5490 processDown(mapper, 100, 200);
5491 processPressure(mapper, 0);
5492 processSync(mapper);
5493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5494 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5496 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5497
5498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5499 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5501 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5502
5503 // move a little
5504 processMove(mapper, 150, 250);
5505 processSync(mapper);
5506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5507 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5509 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5510
5511 // down when pressure is non-zero
5512 processPressure(mapper, RAW_PRESSURE_MAX);
5513 processSync(mapper);
5514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5515 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5517 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5518
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5520 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5522 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5523
5524 // up when pressure becomes 0, hover restored
5525 processPressure(mapper, 0);
5526 processSync(mapper);
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5528 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5530 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5531
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5535 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5536
5537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5540 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5541
5542 // exit hover when pointer goes away
5543 processUp(mapper);
5544 processSync(mapper);
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5548 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5549}
5550
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551// --- MultiTouchInputMapperTest ---
5552
5553class MultiTouchInputMapperTest : public TouchInputMapperTest {
5554protected:
5555 void prepareAxes(int axes);
5556
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005557 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5558 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5559 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5560 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5561 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5562 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5563 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5564 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5565 void processId(MultiTouchInputMapper& mapper, int32_t id);
5566 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5567 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5568 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5569 void processMTSync(MultiTouchInputMapper& mapper);
5570 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005571};
5572
5573void MultiTouchInputMapperTest::prepareAxes(int axes) {
5574 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005575 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5576 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577 }
5578 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005579 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5580 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005582 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5583 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584 }
5585 }
5586 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005587 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5588 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005590 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5591 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592 }
5593 }
5594 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005595 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5596 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597 }
5598 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005599 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5600 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601 }
5602 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005603 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5604 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605 }
5606 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005607 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5608 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609 }
5610 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005611 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5612 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613 }
5614 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005615 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005616 }
5617}
5618
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005619void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5620 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005621 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5622 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623}
5624
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005625void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5626 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005627 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005628}
5629
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005630void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5631 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005632 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005633}
5634
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005635void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005636 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005637}
5638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005639void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005640 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005641}
5642
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005643void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5644 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005645 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005646}
5647
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005648void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005649 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005650}
5651
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005652void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005653 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654}
5655
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005656void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005657 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658}
5659
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005660void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005661 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005662}
5663
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005664void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005665 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666}
5667
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005668void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5669 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005670 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671}
5672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005673void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005674 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675}
5676
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005677void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005678 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679}
5680
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682 addConfigurationProperty("touch.deviceType", "touchScreen");
5683 prepareDisplay(DISPLAY_ORIENTATION_0);
5684 prepareAxes(POSITION);
5685 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005686 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005687
arthurhungdcef2dc2020-08-11 14:47:50 +08005688 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689
5690 NotifyMotionArgs motionArgs;
5691
5692 // Two fingers down at once.
5693 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5694 processPosition(mapper, x1, y1);
5695 processMTSync(mapper);
5696 processPosition(mapper, x2, y2);
5697 processMTSync(mapper);
5698 processSync(mapper);
5699
5700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5701 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5702 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5703 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5704 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5705 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5706 ASSERT_EQ(0, motionArgs.flags);
5707 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5708 ASSERT_EQ(0, motionArgs.buttonState);
5709 ASSERT_EQ(0, motionArgs.edgeFlags);
5710 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5711 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5714 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5715 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5716 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5717 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5718
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5720 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5721 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5722 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5723 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5724 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5725 motionArgs.action);
5726 ASSERT_EQ(0, motionArgs.flags);
5727 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5728 ASSERT_EQ(0, motionArgs.buttonState);
5729 ASSERT_EQ(0, motionArgs.edgeFlags);
5730 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5731 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5732 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5733 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5736 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5738 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5739 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5740 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5741 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5742
5743 // Move.
5744 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5745 processPosition(mapper, x1, y1);
5746 processMTSync(mapper);
5747 processPosition(mapper, x2, y2);
5748 processMTSync(mapper);
5749 processSync(mapper);
5750
5751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5752 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5753 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5754 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5755 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5757 ASSERT_EQ(0, motionArgs.flags);
5758 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5759 ASSERT_EQ(0, motionArgs.buttonState);
5760 ASSERT_EQ(0, motionArgs.edgeFlags);
5761 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5762 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5764 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5767 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5769 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5770 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5771 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5772 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5773
5774 // First finger up.
5775 x2 += 15; y2 -= 20;
5776 processPosition(mapper, x2, y2);
5777 processMTSync(mapper);
5778 processSync(mapper);
5779
5780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5781 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5782 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5783 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5784 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5785 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5786 motionArgs.action);
5787 ASSERT_EQ(0, motionArgs.flags);
5788 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5789 ASSERT_EQ(0, motionArgs.buttonState);
5790 ASSERT_EQ(0, motionArgs.edgeFlags);
5791 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5792 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5794 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5797 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5799 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5800 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5801 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5802 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5803
5804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5805 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5806 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5807 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5808 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5810 ASSERT_EQ(0, motionArgs.flags);
5811 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5812 ASSERT_EQ(0, motionArgs.buttonState);
5813 ASSERT_EQ(0, motionArgs.edgeFlags);
5814 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5815 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5818 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5819 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5820 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5821 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5822
5823 // Move.
5824 x2 += 20; y2 -= 25;
5825 processPosition(mapper, x2, y2);
5826 processMTSync(mapper);
5827 processSync(mapper);
5828
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5830 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5831 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5832 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5833 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5835 ASSERT_EQ(0, motionArgs.flags);
5836 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5837 ASSERT_EQ(0, motionArgs.buttonState);
5838 ASSERT_EQ(0, motionArgs.edgeFlags);
5839 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5840 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5843 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5844 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5845 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5846 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5847
5848 // New finger down.
5849 int32_t x3 = 700, y3 = 300;
5850 processPosition(mapper, x2, y2);
5851 processMTSync(mapper);
5852 processPosition(mapper, x3, y3);
5853 processMTSync(mapper);
5854 processSync(mapper);
5855
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5858 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5859 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5860 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5861 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5862 motionArgs.action);
5863 ASSERT_EQ(0, motionArgs.flags);
5864 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5865 ASSERT_EQ(0, motionArgs.buttonState);
5866 ASSERT_EQ(0, motionArgs.edgeFlags);
5867 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5868 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5870 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5871 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5873 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5875 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5876 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5877 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5878 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5879
5880 // Second finger up.
5881 x3 += 30; y3 -= 20;
5882 processPosition(mapper, x3, y3);
5883 processMTSync(mapper);
5884 processSync(mapper);
5885
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5887 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5888 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5889 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5890 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5891 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5892 motionArgs.action);
5893 ASSERT_EQ(0, motionArgs.flags);
5894 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5895 ASSERT_EQ(0, motionArgs.buttonState);
5896 ASSERT_EQ(0, motionArgs.edgeFlags);
5897 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5898 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5900 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5903 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5905 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5906 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5907 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5908 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5909
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5911 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5912 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5913 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5914 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5916 ASSERT_EQ(0, motionArgs.flags);
5917 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5918 ASSERT_EQ(0, motionArgs.buttonState);
5919 ASSERT_EQ(0, motionArgs.edgeFlags);
5920 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5924 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5925 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5926 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5927 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5928
5929 // Last finger up.
5930 processMTSync(mapper);
5931 processSync(mapper);
5932
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5934 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5935 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5936 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5937 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5938 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5939 ASSERT_EQ(0, motionArgs.flags);
5940 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5941 ASSERT_EQ(0, motionArgs.buttonState);
5942 ASSERT_EQ(0, motionArgs.edgeFlags);
5943 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5944 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5947 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5948 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5949 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5950 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5951
5952 // Should not have sent any more keys or motions.
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5955}
5956
5957TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958 addConfigurationProperty("touch.deviceType", "touchScreen");
5959 prepareDisplay(DISPLAY_ORIENTATION_0);
5960 prepareAxes(POSITION | ID);
5961 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005962 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005963
arthurhungdcef2dc2020-08-11 14:47:50 +08005964 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965
5966 NotifyMotionArgs motionArgs;
5967
5968 // Two fingers down at once.
5969 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5970 processPosition(mapper, x1, y1);
5971 processId(mapper, 1);
5972 processMTSync(mapper);
5973 processPosition(mapper, x2, y2);
5974 processId(mapper, 2);
5975 processMTSync(mapper);
5976 processSync(mapper);
5977
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5979 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5980 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5981 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5984 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5985
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5988 motionArgs.action);
5989 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5990 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5992 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5995 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5997 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5998
5999 // Move.
6000 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6001 processPosition(mapper, x1, y1);
6002 processId(mapper, 1);
6003 processMTSync(mapper);
6004 processPosition(mapper, x2, y2);
6005 processId(mapper, 2);
6006 processMTSync(mapper);
6007 processSync(mapper);
6008
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6011 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6012 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6014 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6017 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6019 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6020
6021 // First finger up.
6022 x2 += 15; y2 -= 20;
6023 processPosition(mapper, x2, y2);
6024 processId(mapper, 2);
6025 processMTSync(mapper);
6026 processSync(mapper);
6027
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6030 motionArgs.action);
6031 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6032 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6034 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6037 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6039 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6040
6041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6043 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6044 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6047 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6048
6049 // Move.
6050 x2 += 20; y2 -= 25;
6051 processPosition(mapper, x2, y2);
6052 processId(mapper, 2);
6053 processMTSync(mapper);
6054 processSync(mapper);
6055
6056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6058 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6059 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6062 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6063
6064 // New finger down.
6065 int32_t x3 = 700, y3 = 300;
6066 processPosition(mapper, x2, y2);
6067 processId(mapper, 2);
6068 processMTSync(mapper);
6069 processPosition(mapper, x3, y3);
6070 processId(mapper, 3);
6071 processMTSync(mapper);
6072 processSync(mapper);
6073
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6075 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6076 motionArgs.action);
6077 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6078 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6080 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6083 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6085 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6086
6087 // Second finger up.
6088 x3 += 30; y3 -= 20;
6089 processPosition(mapper, x3, y3);
6090 processId(mapper, 3);
6091 processMTSync(mapper);
6092 processSync(mapper);
6093
6094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6095 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6096 motionArgs.action);
6097 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6098 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6100 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6103 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6105 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6106
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6109 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6110 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6113 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6114
6115 // Last finger up.
6116 processMTSync(mapper);
6117 processSync(mapper);
6118
6119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6120 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6121 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6122 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6125 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6126
6127 // Should not have sent any more keys or motions.
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6130}
6131
6132TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133 addConfigurationProperty("touch.deviceType", "touchScreen");
6134 prepareDisplay(DISPLAY_ORIENTATION_0);
6135 prepareAxes(POSITION | ID | SLOT);
6136 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006137 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138
arthurhungdcef2dc2020-08-11 14:47:50 +08006139 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140
6141 NotifyMotionArgs motionArgs;
6142
6143 // Two fingers down at once.
6144 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6145 processPosition(mapper, x1, y1);
6146 processId(mapper, 1);
6147 processSlot(mapper, 1);
6148 processPosition(mapper, x2, y2);
6149 processId(mapper, 2);
6150 processSync(mapper);
6151
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6153 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6154 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6155 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6158 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6159
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6161 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6162 motionArgs.action);
6163 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6164 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6166 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6169 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6171 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6172
6173 // Move.
6174 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6175 processSlot(mapper, 0);
6176 processPosition(mapper, x1, y1);
6177 processSlot(mapper, 1);
6178 processPosition(mapper, x2, y2);
6179 processSync(mapper);
6180
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6183 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6184 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6186 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6187 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6189 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6191 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6192
6193 // First finger up.
6194 x2 += 15; y2 -= 20;
6195 processSlot(mapper, 0);
6196 processId(mapper, -1);
6197 processSlot(mapper, 1);
6198 processPosition(mapper, x2, y2);
6199 processSync(mapper);
6200
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6202 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6203 motionArgs.action);
6204 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6205 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6207 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6210 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6212 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6213
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6216 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6217 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6220 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6221
6222 // Move.
6223 x2 += 20; y2 -= 25;
6224 processPosition(mapper, x2, y2);
6225 processSync(mapper);
6226
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6229 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6230 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6233 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6234
6235 // New finger down.
6236 int32_t x3 = 700, y3 = 300;
6237 processPosition(mapper, x2, y2);
6238 processSlot(mapper, 0);
6239 processId(mapper, 3);
6240 processPosition(mapper, x3, y3);
6241 processSync(mapper);
6242
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6245 motionArgs.action);
6246 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6247 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6249 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6252 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6254 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6255
6256 // Second finger up.
6257 x3 += 30; y3 -= 20;
6258 processSlot(mapper, 1);
6259 processId(mapper, -1);
6260 processSlot(mapper, 0);
6261 processPosition(mapper, x3, y3);
6262 processSync(mapper);
6263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6266 motionArgs.action);
6267 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6268 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6270 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6273 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6275 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6276
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6279 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6283 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6284
6285 // Last finger up.
6286 processId(mapper, -1);
6287 processSync(mapper);
6288
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6291 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6292 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6295 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6296
6297 // Should not have sent any more keys or motions.
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6300}
6301
6302TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 addConfigurationProperty("touch.deviceType", "touchScreen");
6304 prepareDisplay(DISPLAY_ORIENTATION_0);
6305 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006306 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307
6308 // These calculations are based on the input device calibration documentation.
6309 int32_t rawX = 100;
6310 int32_t rawY = 200;
6311 int32_t rawTouchMajor = 7;
6312 int32_t rawTouchMinor = 6;
6313 int32_t rawToolMajor = 9;
6314 int32_t rawToolMinor = 8;
6315 int32_t rawPressure = 11;
6316 int32_t rawDistance = 0;
6317 int32_t rawOrientation = 3;
6318 int32_t id = 5;
6319
6320 float x = toDisplayX(rawX);
6321 float y = toDisplayY(rawY);
6322 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6323 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6324 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6325 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6326 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6327 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6328 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6329 float distance = float(rawDistance);
6330
6331 processPosition(mapper, rawX, rawY);
6332 processTouchMajor(mapper, rawTouchMajor);
6333 processTouchMinor(mapper, rawTouchMinor);
6334 processToolMajor(mapper, rawToolMajor);
6335 processToolMinor(mapper, rawToolMinor);
6336 processPressure(mapper, rawPressure);
6337 processOrientation(mapper, rawOrientation);
6338 processDistance(mapper, rawDistance);
6339 processId(mapper, id);
6340 processMTSync(mapper);
6341 processSync(mapper);
6342
6343 NotifyMotionArgs args;
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6345 ASSERT_EQ(0, args.pointerProperties[0].id);
6346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6347 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6348 orientation, distance));
6349}
6350
6351TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006352 addConfigurationProperty("touch.deviceType", "touchScreen");
6353 prepareDisplay(DISPLAY_ORIENTATION_0);
6354 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6355 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006356 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357
6358 // These calculations are based on the input device calibration documentation.
6359 int32_t rawX = 100;
6360 int32_t rawY = 200;
6361 int32_t rawTouchMajor = 140;
6362 int32_t rawTouchMinor = 120;
6363 int32_t rawToolMajor = 180;
6364 int32_t rawToolMinor = 160;
6365
6366 float x = toDisplayX(rawX);
6367 float y = toDisplayY(rawY);
6368 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6369 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6370 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6371 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6372 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6373
6374 processPosition(mapper, rawX, rawY);
6375 processTouchMajor(mapper, rawTouchMajor);
6376 processTouchMinor(mapper, rawTouchMinor);
6377 processToolMajor(mapper, rawToolMajor);
6378 processToolMinor(mapper, rawToolMinor);
6379 processMTSync(mapper);
6380 processSync(mapper);
6381
6382 NotifyMotionArgs args;
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6385 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6386}
6387
6388TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 addConfigurationProperty("touch.deviceType", "touchScreen");
6390 prepareDisplay(DISPLAY_ORIENTATION_0);
6391 prepareAxes(POSITION | TOUCH | TOOL);
6392 addConfigurationProperty("touch.size.calibration", "diameter");
6393 addConfigurationProperty("touch.size.scale", "10");
6394 addConfigurationProperty("touch.size.bias", "160");
6395 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006396 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397
6398 // These calculations are based on the input device calibration documentation.
6399 // Note: We only provide a single common touch/tool value because the device is assumed
6400 // not to emit separate values for each pointer (isSummed = 1).
6401 int32_t rawX = 100;
6402 int32_t rawY = 200;
6403 int32_t rawX2 = 150;
6404 int32_t rawY2 = 250;
6405 int32_t rawTouchMajor = 5;
6406 int32_t rawToolMajor = 8;
6407
6408 float x = toDisplayX(rawX);
6409 float y = toDisplayY(rawY);
6410 float x2 = toDisplayX(rawX2);
6411 float y2 = toDisplayY(rawY2);
6412 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6413 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6414 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6415
6416 processPosition(mapper, rawX, rawY);
6417 processTouchMajor(mapper, rawTouchMajor);
6418 processToolMajor(mapper, rawToolMajor);
6419 processMTSync(mapper);
6420 processPosition(mapper, rawX2, rawY2);
6421 processTouchMajor(mapper, rawTouchMajor);
6422 processToolMajor(mapper, rawToolMajor);
6423 processMTSync(mapper);
6424 processSync(mapper);
6425
6426 NotifyMotionArgs args;
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6428 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6429
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6432 args.action);
6433 ASSERT_EQ(size_t(2), args.pointerCount);
6434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6435 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6437 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6438}
6439
6440TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441 addConfigurationProperty("touch.deviceType", "touchScreen");
6442 prepareDisplay(DISPLAY_ORIENTATION_0);
6443 prepareAxes(POSITION | TOUCH | TOOL);
6444 addConfigurationProperty("touch.size.calibration", "area");
6445 addConfigurationProperty("touch.size.scale", "43");
6446 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006447 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006448
6449 // These calculations are based on the input device calibration documentation.
6450 int32_t rawX = 100;
6451 int32_t rawY = 200;
6452 int32_t rawTouchMajor = 5;
6453 int32_t rawToolMajor = 8;
6454
6455 float x = toDisplayX(rawX);
6456 float y = toDisplayY(rawY);
6457 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6458 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6459 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6460
6461 processPosition(mapper, rawX, rawY);
6462 processTouchMajor(mapper, rawTouchMajor);
6463 processToolMajor(mapper, rawToolMajor);
6464 processMTSync(mapper);
6465 processSync(mapper);
6466
6467 NotifyMotionArgs args;
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6470 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6471}
6472
6473TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006474 addConfigurationProperty("touch.deviceType", "touchScreen");
6475 prepareDisplay(DISPLAY_ORIENTATION_0);
6476 prepareAxes(POSITION | PRESSURE);
6477 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6478 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006479 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480
Michael Wrightaa449c92017-12-13 21:21:43 +00006481 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006482 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006483 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6484 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6485 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6486
Michael Wrightd02c5b62014-02-10 15:10:22 -08006487 // These calculations are based on the input device calibration documentation.
6488 int32_t rawX = 100;
6489 int32_t rawY = 200;
6490 int32_t rawPressure = 60;
6491
6492 float x = toDisplayX(rawX);
6493 float y = toDisplayY(rawY);
6494 float pressure = float(rawPressure) * 0.01f;
6495
6496 processPosition(mapper, rawX, rawY);
6497 processPressure(mapper, rawPressure);
6498 processMTSync(mapper);
6499 processSync(mapper);
6500
6501 NotifyMotionArgs args;
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6504 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6505}
6506
6507TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006508 addConfigurationProperty("touch.deviceType", "touchScreen");
6509 prepareDisplay(DISPLAY_ORIENTATION_0);
6510 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006511 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006512
6513 NotifyMotionArgs motionArgs;
6514 NotifyKeyArgs keyArgs;
6515
6516 processId(mapper, 1);
6517 processPosition(mapper, 100, 200);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6521 ASSERT_EQ(0, motionArgs.buttonState);
6522
6523 // press BTN_LEFT, release BTN_LEFT
6524 processKey(mapper, BTN_LEFT, 1);
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6528 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6529
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6532 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6533
Michael Wrightd02c5b62014-02-10 15:10:22 -08006534 processKey(mapper, BTN_LEFT, 0);
6535 processSync(mapper);
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006539
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006542 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006543
6544 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6545 processKey(mapper, BTN_RIGHT, 1);
6546 processKey(mapper, BTN_MIDDLE, 1);
6547 processSync(mapper);
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6550 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6551 motionArgs.buttonState);
6552
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6554 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6555 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6556
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6559 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6560 motionArgs.buttonState);
6561
Michael Wrightd02c5b62014-02-10 15:10:22 -08006562 processKey(mapper, BTN_RIGHT, 0);
6563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006566 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006567
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006570 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006571
6572 processKey(mapper, BTN_MIDDLE, 0);
6573 processSync(mapper);
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006575 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006576 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006577
6578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006580 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006581
6582 // press BTN_BACK, release BTN_BACK
6583 processKey(mapper, BTN_BACK, 1);
6584 processSync(mapper);
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6586 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6587 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006588
Michael Wrightd02c5b62014-02-10 15:10:22 -08006589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006591 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6592
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6595 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006596
6597 processKey(mapper, BTN_BACK, 0);
6598 processSync(mapper);
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006600 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006601 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006602
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006605 ASSERT_EQ(0, motionArgs.buttonState);
6606
Michael Wrightd02c5b62014-02-10 15:10:22 -08006607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6608 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6609 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6610
6611 // press BTN_SIDE, release BTN_SIDE
6612 processKey(mapper, BTN_SIDE, 1);
6613 processSync(mapper);
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6615 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6616 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006617
Michael Wrightd02c5b62014-02-10 15:10:22 -08006618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006620 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6621
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6624 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006625
6626 processKey(mapper, BTN_SIDE, 0);
6627 processSync(mapper);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006630 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006631
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006634 ASSERT_EQ(0, motionArgs.buttonState);
6635
Michael Wrightd02c5b62014-02-10 15:10:22 -08006636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6637 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6638 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6639
6640 // press BTN_FORWARD, release BTN_FORWARD
6641 processKey(mapper, BTN_FORWARD, 1);
6642 processSync(mapper);
6643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6644 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6645 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006646
Michael Wrightd02c5b62014-02-10 15:10:22 -08006647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006649 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6650
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6652 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6653 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006654
6655 processKey(mapper, BTN_FORWARD, 0);
6656 processSync(mapper);
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006658 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006659 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006660
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006663 ASSERT_EQ(0, motionArgs.buttonState);
6664
Michael Wrightd02c5b62014-02-10 15:10:22 -08006665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6666 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6667 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6668
6669 // press BTN_EXTRA, release BTN_EXTRA
6670 processKey(mapper, BTN_EXTRA, 1);
6671 processSync(mapper);
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6673 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6674 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006675
Michael Wrightd02c5b62014-02-10 15:10:22 -08006676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006678 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6679
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6682 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006683
6684 processKey(mapper, BTN_EXTRA, 0);
6685 processSync(mapper);
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006688 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006689
6690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006692 ASSERT_EQ(0, motionArgs.buttonState);
6693
Michael Wrightd02c5b62014-02-10 15:10:22 -08006694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6695 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6696 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6697
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6699
Michael Wrightd02c5b62014-02-10 15:10:22 -08006700 // press BTN_STYLUS, release BTN_STYLUS
6701 processKey(mapper, BTN_STYLUS, 1);
6702 processSync(mapper);
6703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006705 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6706
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6709 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006710
6711 processKey(mapper, BTN_STYLUS, 0);
6712 processSync(mapper);
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006715 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006716
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006719 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006720
6721 // press BTN_STYLUS2, release BTN_STYLUS2
6722 processKey(mapper, BTN_STYLUS2, 1);
6723 processSync(mapper);
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006726 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6727
6728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6729 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6730 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006731
6732 processKey(mapper, BTN_STYLUS2, 0);
6733 processSync(mapper);
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006735 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006736 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006737
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006740 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006741
6742 // release touch
6743 processId(mapper, -1);
6744 processSync(mapper);
6745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6746 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6747 ASSERT_EQ(0, motionArgs.buttonState);
6748}
6749
6750TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006751 addConfigurationProperty("touch.deviceType", "touchScreen");
6752 prepareDisplay(DISPLAY_ORIENTATION_0);
6753 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006754 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006755
6756 NotifyMotionArgs motionArgs;
6757
6758 // default tool type is finger
6759 processId(mapper, 1);
6760 processPosition(mapper, 100, 200);
6761 processSync(mapper);
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6763 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6765
6766 // eraser
6767 processKey(mapper, BTN_TOOL_RUBBER, 1);
6768 processSync(mapper);
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6772
6773 // stylus
6774 processKey(mapper, BTN_TOOL_RUBBER, 0);
6775 processKey(mapper, BTN_TOOL_PEN, 1);
6776 processSync(mapper);
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6779 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6780
6781 // brush
6782 processKey(mapper, BTN_TOOL_PEN, 0);
6783 processKey(mapper, BTN_TOOL_BRUSH, 1);
6784 processSync(mapper);
6785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6788
6789 // pencil
6790 processKey(mapper, BTN_TOOL_BRUSH, 0);
6791 processKey(mapper, BTN_TOOL_PENCIL, 1);
6792 processSync(mapper);
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6796
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006797 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006798 processKey(mapper, BTN_TOOL_PENCIL, 0);
6799 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6800 processSync(mapper);
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6804
6805 // mouse
6806 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6807 processKey(mapper, BTN_TOOL_MOUSE, 1);
6808 processSync(mapper);
6809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6812
6813 // lens
6814 processKey(mapper, BTN_TOOL_MOUSE, 0);
6815 processKey(mapper, BTN_TOOL_LENS, 1);
6816 processSync(mapper);
6817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6820
6821 // double-tap
6822 processKey(mapper, BTN_TOOL_LENS, 0);
6823 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6824 processSync(mapper);
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6828
6829 // triple-tap
6830 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6831 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6832 processSync(mapper);
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6836
6837 // quad-tap
6838 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6839 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6840 processSync(mapper);
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6844
6845 // finger
6846 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6847 processKey(mapper, BTN_TOOL_FINGER, 1);
6848 processSync(mapper);
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6852
6853 // stylus trumps finger
6854 processKey(mapper, BTN_TOOL_PEN, 1);
6855 processSync(mapper);
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6859
6860 // eraser trumps stylus
6861 processKey(mapper, BTN_TOOL_RUBBER, 1);
6862 processSync(mapper);
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6866
6867 // mouse trumps eraser
6868 processKey(mapper, BTN_TOOL_MOUSE, 1);
6869 processSync(mapper);
6870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6873
6874 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6875 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6876 processSync(mapper);
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6879 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6880
6881 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6882 processToolType(mapper, MT_TOOL_PEN);
6883 processSync(mapper);
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6887
6888 // back to default tool type
6889 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6890 processKey(mapper, BTN_TOOL_MOUSE, 0);
6891 processKey(mapper, BTN_TOOL_RUBBER, 0);
6892 processKey(mapper, BTN_TOOL_PEN, 0);
6893 processKey(mapper, BTN_TOOL_FINGER, 0);
6894 processSync(mapper);
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6898}
6899
6900TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006901 addConfigurationProperty("touch.deviceType", "touchScreen");
6902 prepareDisplay(DISPLAY_ORIENTATION_0);
6903 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006904 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006905 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006906
6907 NotifyMotionArgs motionArgs;
6908
6909 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6910 processId(mapper, 1);
6911 processPosition(mapper, 100, 200);
6912 processSync(mapper);
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6914 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6916 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6917
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6919 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6921 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6922
6923 // move a little
6924 processPosition(mapper, 150, 250);
6925 processSync(mapper);
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6927 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6929 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6930
6931 // down when BTN_TOUCH is pressed, pressure defaults to 1
6932 processKey(mapper, BTN_TOUCH, 1);
6933 processSync(mapper);
6934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6935 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6937 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6938
6939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6940 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6942 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6943
6944 // up when BTN_TOUCH is released, hover restored
6945 processKey(mapper, BTN_TOUCH, 0);
6946 processSync(mapper);
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6948 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6950 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6951
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6953 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6955 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6956
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6958 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6960 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6961
6962 // exit hover when pointer goes away
6963 processId(mapper, -1);
6964 processSync(mapper);
6965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6966 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6968 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6969}
6970
6971TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972 addConfigurationProperty("touch.deviceType", "touchScreen");
6973 prepareDisplay(DISPLAY_ORIENTATION_0);
6974 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006975 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006976
6977 NotifyMotionArgs motionArgs;
6978
6979 // initially hovering because pressure is 0
6980 processId(mapper, 1);
6981 processPosition(mapper, 100, 200);
6982 processPressure(mapper, 0);
6983 processSync(mapper);
6984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6985 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6987 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6988
6989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6990 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6992 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6993
6994 // move a little
6995 processPosition(mapper, 150, 250);
6996 processSync(mapper);
6997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6998 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7000 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7001
7002 // down when pressure becomes non-zero
7003 processPressure(mapper, RAW_PRESSURE_MAX);
7004 processSync(mapper);
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7006 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7008 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7009
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7011 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7013 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7014
7015 // up when pressure becomes 0, hover restored
7016 processPressure(mapper, 0);
7017 processSync(mapper);
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7019 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7021 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7022
7023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7024 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7026 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7027
7028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7029 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7031 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7032
7033 // exit hover when pointer goes away
7034 processId(mapper, -1);
7035 processSync(mapper);
7036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7037 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7039 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7040}
7041
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007042/**
7043 * Set the input device port <--> display port associations, and check that the
7044 * events are routed to the display that matches the display port.
7045 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7046 */
7047TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007048 const std::string usb2 = "USB2";
7049 const uint8_t hdmi1 = 0;
7050 const uint8_t hdmi2 = 1;
7051 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007052 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007053
7054 addConfigurationProperty("touch.deviceType", "touchScreen");
7055 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007056 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007057
7058 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7059 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7060
7061 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7062 // for this input device is specified, and the matching viewport is not present,
7063 // the input device should be disabled (at the mapper level).
7064
7065 // Add viewport for display 2 on hdmi2
7066 prepareSecondaryDisplay(type, hdmi2);
7067 // Send a touch event
7068 processPosition(mapper, 100, 100);
7069 processSync(mapper);
7070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7071
7072 // Add viewport for display 1 on hdmi1
7073 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7074 // Send a touch event again
7075 processPosition(mapper, 100, 100);
7076 processSync(mapper);
7077
7078 NotifyMotionArgs args;
7079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7080 ASSERT_EQ(DISPLAY_ID, args.displayId);
7081}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007083TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007084 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007085 std::shared_ptr<FakePointerController> fakePointerController =
7086 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007087 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007088 fakePointerController->setPosition(100, 200);
7089 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007090 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7091
Garfield Tan888a6a42020-01-09 11:39:16 -08007092 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007093 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007094
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007095 prepareDisplay(DISPLAY_ORIENTATION_0);
7096 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007097 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007098
7099 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007100 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007101
7102 NotifyMotionArgs motionArgs;
7103 processPosition(mapper, 100, 100);
7104 processSync(mapper);
7105
7106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7107 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7108 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7109}
7110
Arthur Hung7c645402019-01-25 17:45:42 +08007111TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7112 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007113 prepareAxes(POSITION | ID | SLOT);
7114 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007115 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007116
7117 // Create the second touch screen device, and enable multi fingers.
7118 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007119 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007120 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007121 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007122 std::shared_ptr<InputDevice> device2 =
7123 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7124 Flags<InputDeviceClass>(0));
7125
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007126 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7127 0 /*flat*/, 0 /*fuzz*/);
7128 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7129 0 /*flat*/, 0 /*fuzz*/);
7130 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7131 0 /*flat*/, 0 /*fuzz*/);
7132 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7133 0 /*flat*/, 0 /*fuzz*/);
7134 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7135 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7136 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007137
7138 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007139 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007140 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7141 device2->reset(ARBITRARY_TIME);
7142
7143 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007144 std::shared_ptr<FakePointerController> fakePointerController =
7145 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007146 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7147 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7148
7149 // Setup policy for associated displays and show touches.
7150 const uint8_t hdmi1 = 0;
7151 const uint8_t hdmi2 = 1;
7152 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7153 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7154 mFakePolicy->setShowTouches(true);
7155
7156 // Create displays.
7157 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007158 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007159
7160 // Default device will reconfigure above, need additional reconfiguration for another device.
7161 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007162 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007163
7164 // Two fingers down at default display.
7165 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7166 processPosition(mapper, x1, y1);
7167 processId(mapper, 1);
7168 processSlot(mapper, 1);
7169 processPosition(mapper, x2, y2);
7170 processId(mapper, 2);
7171 processSync(mapper);
7172
7173 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7174 fakePointerController->getSpots().find(DISPLAY_ID);
7175 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7176 ASSERT_EQ(size_t(2), iter->second.size());
7177
7178 // Two fingers down at second display.
7179 processPosition(mapper2, x1, y1);
7180 processId(mapper2, 1);
7181 processSlot(mapper2, 1);
7182 processPosition(mapper2, x2, y2);
7183 processId(mapper2, 2);
7184 processSync(mapper2);
7185
7186 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7187 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7188 ASSERT_EQ(size_t(2), iter->second.size());
7189}
7190
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007191TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007192 prepareAxes(POSITION);
7193 addConfigurationProperty("touch.deviceType", "touchScreen");
7194 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007195 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007196
7197 NotifyMotionArgs motionArgs;
7198 // Unrotated video frame
7199 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7200 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007201 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007202 processPosition(mapper, 100, 200);
7203 processSync(mapper);
7204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7205 ASSERT_EQ(frames, motionArgs.videoFrames);
7206
7207 // Subsequent touch events should not have any videoframes
7208 // This is implemented separately in FakeEventHub,
7209 // but that should match the behaviour of TouchVideoDevice.
7210 processPosition(mapper, 200, 200);
7211 processSync(mapper);
7212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7213 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7214}
7215
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007216TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007217 prepareAxes(POSITION);
7218 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007219 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007220 // Unrotated video frame
7221 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7222 NotifyMotionArgs motionArgs;
7223
7224 // Test all 4 orientations
7225 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7226 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7227 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7228 clearViewports();
7229 prepareDisplay(orientation);
7230 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007231 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007232 processPosition(mapper, 100, 200);
7233 processSync(mapper);
7234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7235 frames[0].rotate(orientation);
7236 ASSERT_EQ(frames, motionArgs.videoFrames);
7237 }
7238}
7239
7240TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007241 prepareAxes(POSITION);
7242 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007243 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007244 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7245 // so mix these.
7246 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7247 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7248 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7249 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7250 NotifyMotionArgs motionArgs;
7251
7252 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007253 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007254 processPosition(mapper, 100, 200);
7255 processSync(mapper);
7256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7257 std::for_each(frames.begin(), frames.end(),
7258 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7259 ASSERT_EQ(frames, motionArgs.videoFrames);
7260}
7261
Arthur Hung9da14732019-09-02 16:16:58 +08007262/**
7263 * If we had defined port associations, but the viewport is not ready, the touch device would be
7264 * expected to be disabled, and it should be enabled after the viewport has found.
7265 */
7266TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007267 constexpr uint8_t hdmi2 = 1;
7268 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007269 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007270
7271 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7272
7273 addConfigurationProperty("touch.deviceType", "touchScreen");
7274 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007275 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007276
7277 ASSERT_EQ(mDevice->isEnabled(), false);
7278
7279 // Add display on hdmi2, the device should be enabled and can receive touch event.
7280 prepareSecondaryDisplay(type, hdmi2);
7281 ASSERT_EQ(mDevice->isEnabled(), true);
7282
7283 // Send a touch event.
7284 processPosition(mapper, 100, 100);
7285 processSync(mapper);
7286
7287 NotifyMotionArgs args;
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7289 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7290}
7291
Arthur Hung6cd19a42019-08-30 19:04:12 +08007292
Arthur Hung6cd19a42019-08-30 19:04:12 +08007293
Arthur Hung421eb1c2020-01-16 00:09:42 +08007294TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007295 addConfigurationProperty("touch.deviceType", "touchScreen");
7296 prepareDisplay(DISPLAY_ORIENTATION_0);
7297 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007298 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007299
7300 NotifyMotionArgs motionArgs;
7301
7302 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7303 // finger down
7304 processId(mapper, 1);
7305 processPosition(mapper, x1, y1);
7306 processSync(mapper);
7307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7308 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7310
7311 // finger move
7312 processId(mapper, 1);
7313 processPosition(mapper, x2, y2);
7314 processSync(mapper);
7315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7318
7319 // finger up.
7320 processId(mapper, -1);
7321 processSync(mapper);
7322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7323 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7324 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7325
7326 // new finger down
7327 processId(mapper, 1);
7328 processPosition(mapper, x3, y3);
7329 processSync(mapper);
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7331 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7333}
7334
7335/**
arthurhungcc7f9802020-04-30 17:55:40 +08007336 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7337 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007338 */
arthurhungcc7f9802020-04-30 17:55:40 +08007339TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007340 addConfigurationProperty("touch.deviceType", "touchScreen");
7341 prepareDisplay(DISPLAY_ORIENTATION_0);
7342 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007343 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007344
7345 NotifyMotionArgs motionArgs;
7346
7347 // default tool type is finger
7348 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007349 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007350 processPosition(mapper, x1, y1);
7351 processSync(mapper);
7352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7353 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7355
7356 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7357 processToolType(mapper, MT_TOOL_PALM);
7358 processSync(mapper);
7359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7360 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7361
7362 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007363 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007364 processPosition(mapper, x2, y2);
7365 processSync(mapper);
7366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7367
7368 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007369 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007370 processSync(mapper);
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7372
7373 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007374 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007375 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007376 processPosition(mapper, x3, y3);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7379 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7381}
7382
arthurhungbf89a482020-04-17 17:37:55 +08007383/**
arthurhungcc7f9802020-04-30 17:55:40 +08007384 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7385 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007386 */
arthurhungcc7f9802020-04-30 17:55:40 +08007387TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007388 addConfigurationProperty("touch.deviceType", "touchScreen");
7389 prepareDisplay(DISPLAY_ORIENTATION_0);
7390 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7391 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7392
7393 NotifyMotionArgs motionArgs;
7394
7395 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007396 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7397 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007398 processPosition(mapper, x1, y1);
7399 processSync(mapper);
7400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7401 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7403
7404 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007405 processSlot(mapper, SECOND_SLOT);
7406 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007407 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007408 processSync(mapper);
7409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7410 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7411 motionArgs.action);
7412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7413
7414 // If the tool type of the first finger changes to MT_TOOL_PALM,
7415 // we expect to receive ACTION_POINTER_UP with cancel flag.
7416 processSlot(mapper, FIRST_SLOT);
7417 processId(mapper, FIRST_TRACKING_ID);
7418 processToolType(mapper, MT_TOOL_PALM);
7419 processSync(mapper);
7420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7421 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7422 motionArgs.action);
7423 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7424
7425 // The following MOVE events of second finger should be processed.
7426 processSlot(mapper, SECOND_SLOT);
7427 processId(mapper, SECOND_TRACKING_ID);
7428 processPosition(mapper, x2 + 1, y2 + 1);
7429 processSync(mapper);
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7432 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7433
7434 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7435 // it. Second finger receive move.
7436 processSlot(mapper, FIRST_SLOT);
7437 processId(mapper, INVALID_TRACKING_ID);
7438 processSync(mapper);
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7441 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7442
7443 // Second finger keeps moving.
7444 processSlot(mapper, SECOND_SLOT);
7445 processId(mapper, SECOND_TRACKING_ID);
7446 processPosition(mapper, x2 + 2, y2 + 2);
7447 processSync(mapper);
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7450 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7451
7452 // Second finger up.
7453 processId(mapper, INVALID_TRACKING_ID);
7454 processSync(mapper);
7455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7456 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7457 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7458}
7459
7460/**
7461 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7462 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7463 */
7464TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7465 addConfigurationProperty("touch.deviceType", "touchScreen");
7466 prepareDisplay(DISPLAY_ORIENTATION_0);
7467 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7468 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7469
7470 NotifyMotionArgs motionArgs;
7471
7472 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7473 // First finger down.
7474 processId(mapper, FIRST_TRACKING_ID);
7475 processPosition(mapper, x1, y1);
7476 processSync(mapper);
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7479 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7480
7481 // Second finger down.
7482 processSlot(mapper, SECOND_SLOT);
7483 processId(mapper, SECOND_TRACKING_ID);
7484 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007485 processSync(mapper);
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7487 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7488 motionArgs.action);
7489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7490
arthurhungcc7f9802020-04-30 17:55:40 +08007491 // If the tool type of the first finger changes to MT_TOOL_PALM,
7492 // we expect to receive ACTION_POINTER_UP with cancel flag.
7493 processSlot(mapper, FIRST_SLOT);
7494 processId(mapper, FIRST_TRACKING_ID);
7495 processToolType(mapper, MT_TOOL_PALM);
7496 processSync(mapper);
7497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7498 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7499 motionArgs.action);
7500 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7501
7502 // Second finger keeps moving.
7503 processSlot(mapper, SECOND_SLOT);
7504 processId(mapper, SECOND_TRACKING_ID);
7505 processPosition(mapper, x2 + 1, y2 + 1);
7506 processSync(mapper);
7507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7508 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7509
7510 // second finger becomes palm, receive cancel due to only 1 finger is active.
7511 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007512 processToolType(mapper, MT_TOOL_PALM);
7513 processSync(mapper);
7514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7515 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7516
arthurhungcc7f9802020-04-30 17:55:40 +08007517 // third finger down.
7518 processSlot(mapper, THIRD_SLOT);
7519 processId(mapper, THIRD_TRACKING_ID);
7520 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007521 processPosition(mapper, x3, y3);
7522 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7524 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007526 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7527
7528 // third finger move
7529 processId(mapper, THIRD_TRACKING_ID);
7530 processPosition(mapper, x3 + 1, y3 + 1);
7531 processSync(mapper);
7532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7534
7535 // first finger up, third finger receive move.
7536 processSlot(mapper, FIRST_SLOT);
7537 processId(mapper, INVALID_TRACKING_ID);
7538 processSync(mapper);
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7541 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7542
7543 // second finger up, third finger receive move.
7544 processSlot(mapper, SECOND_SLOT);
7545 processId(mapper, INVALID_TRACKING_ID);
7546 processSync(mapper);
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7549 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7550
7551 // third finger up.
7552 processSlot(mapper, THIRD_SLOT);
7553 processId(mapper, INVALID_TRACKING_ID);
7554 processSync(mapper);
7555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7556 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7557 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7558}
7559
7560/**
7561 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7562 * and the active finger could still be allowed to receive the events
7563 */
7564TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7565 addConfigurationProperty("touch.deviceType", "touchScreen");
7566 prepareDisplay(DISPLAY_ORIENTATION_0);
7567 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7568 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7569
7570 NotifyMotionArgs motionArgs;
7571
7572 // default tool type is finger
7573 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7574 processId(mapper, FIRST_TRACKING_ID);
7575 processPosition(mapper, x1, y1);
7576 processSync(mapper);
7577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7578 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7580
7581 // Second finger down.
7582 processSlot(mapper, SECOND_SLOT);
7583 processId(mapper, SECOND_TRACKING_ID);
7584 processPosition(mapper, x2, y2);
7585 processSync(mapper);
7586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7587 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7588 motionArgs.action);
7589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7590
7591 // If the tool type of the second finger changes to MT_TOOL_PALM,
7592 // we expect to receive ACTION_POINTER_UP with cancel flag.
7593 processId(mapper, SECOND_TRACKING_ID);
7594 processToolType(mapper, MT_TOOL_PALM);
7595 processSync(mapper);
7596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7597 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7598 motionArgs.action);
7599 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7600
7601 // The following MOVE event should be processed.
7602 processSlot(mapper, FIRST_SLOT);
7603 processId(mapper, FIRST_TRACKING_ID);
7604 processPosition(mapper, x1 + 1, y1 + 1);
7605 processSync(mapper);
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7608 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7609
7610 // second finger up.
7611 processSlot(mapper, SECOND_SLOT);
7612 processId(mapper, INVALID_TRACKING_ID);
7613 processSync(mapper);
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7616
7617 // first finger keep moving
7618 processSlot(mapper, FIRST_SLOT);
7619 processId(mapper, FIRST_TRACKING_ID);
7620 processPosition(mapper, x1 + 2, y1 + 2);
7621 processSync(mapper);
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7624
7625 // first finger up.
7626 processId(mapper, INVALID_TRACKING_ID);
7627 processSync(mapper);
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7629 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7630 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007631}
7632
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007633// --- MultiTouchInputMapperTest_ExternalDevice ---
7634
7635class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7636protected:
Chris Yea52ade12020-08-27 16:49:20 -07007637 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007638};
7639
7640/**
7641 * Expect fallback to internal viewport if device is external and external viewport is not present.
7642 */
7643TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7644 prepareAxes(POSITION);
7645 addConfigurationProperty("touch.deviceType", "touchScreen");
7646 prepareDisplay(DISPLAY_ORIENTATION_0);
7647 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7648
7649 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7650
7651 NotifyMotionArgs motionArgs;
7652
7653 // Expect the event to be sent to the internal viewport,
7654 // because an external viewport is not present.
7655 processPosition(mapper, 100, 100);
7656 processSync(mapper);
7657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7658 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7659
7660 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007661 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007662 processPosition(mapper, 100, 100);
7663 processSync(mapper);
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7665 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7666}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007667
7668/**
7669 * Test touch should not work if outside of surface.
7670 */
7671class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7672protected:
7673 void halfDisplayToCenterHorizontal(int32_t orientation) {
7674 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007675 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007676
7677 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7678 internalViewport->orientation = orientation;
7679 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7680 internalViewport->logicalLeft = 0;
7681 internalViewport->logicalTop = 0;
7682 internalViewport->logicalRight = DISPLAY_HEIGHT;
7683 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7684
7685 internalViewport->physicalLeft = 0;
7686 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7687 internalViewport->physicalRight = DISPLAY_HEIGHT;
7688 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7689
7690 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7691 internalViewport->deviceHeight = DISPLAY_WIDTH;
7692 } else {
7693 internalViewport->logicalLeft = 0;
7694 internalViewport->logicalTop = 0;
7695 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7696 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7697
7698 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7699 internalViewport->physicalTop = 0;
7700 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7701 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7702
7703 internalViewport->deviceWidth = DISPLAY_WIDTH;
7704 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7705 }
7706
7707 mFakePolicy->updateViewport(internalViewport.value());
7708 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7709 }
7710
7711 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7712 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7713 int32_t yExpected) {
7714 // touch on outside area should not work.
7715 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7716 processSync(mapper);
7717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7718
7719 // touch on inside area should receive the event.
7720 NotifyMotionArgs args;
7721 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7722 processSync(mapper);
7723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7724 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7725 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7726
7727 // Reset.
7728 mapper.reset(ARBITRARY_TIME);
7729 }
7730};
7731
7732TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7733 addConfigurationProperty("touch.deviceType", "touchScreen");
7734 prepareDisplay(DISPLAY_ORIENTATION_0);
7735 prepareAxes(POSITION);
7736 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7737
7738 // Touch on center of normal display should work.
7739 const int32_t x = DISPLAY_WIDTH / 4;
7740 const int32_t y = DISPLAY_HEIGHT / 2;
7741 processPosition(mapper, toRawX(x), toRawY(y));
7742 processSync(mapper);
7743 NotifyMotionArgs args;
7744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7746 0.0f, 0.0f, 0.0f, 0.0f));
7747 // Reset.
7748 mapper.reset(ARBITRARY_TIME);
7749
7750 // Let physical display be different to device, and make surface and physical could be 1:1.
7751 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7752
7753 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7754 const int32_t yExpected = y;
7755 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7756}
7757
7758TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7759 addConfigurationProperty("touch.deviceType", "touchScreen");
7760 prepareDisplay(DISPLAY_ORIENTATION_0);
7761 prepareAxes(POSITION);
7762 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7763
7764 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7765 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7766
7767 const int32_t x = DISPLAY_WIDTH / 4;
7768 const int32_t y = DISPLAY_HEIGHT / 2;
7769
7770 // expect x/y = swap x/y then reverse y.
7771 const int32_t xExpected = y;
7772 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7773 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7774}
7775
7776TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7777 addConfigurationProperty("touch.deviceType", "touchScreen");
7778 prepareDisplay(DISPLAY_ORIENTATION_0);
7779 prepareAxes(POSITION);
7780 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7781
7782 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7783 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7784
7785 const int32_t x = DISPLAY_WIDTH / 4;
7786 const int32_t y = DISPLAY_HEIGHT / 2;
7787
7788 // expect x/y = swap x/y then reverse x.
7789 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7790 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7791 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7792}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007793
7794TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7795 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7796 std::shared_ptr<FakePointerController> fakePointerController =
7797 std::make_shared<FakePointerController>();
7798 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7799 fakePointerController->setPosition(0, 0);
7800 fakePointerController->setButtonState(0);
7801
7802 // prepare device and capture
7803 prepareDisplay(DISPLAY_ORIENTATION_0);
7804 prepareAxes(POSITION | ID | SLOT);
7805 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7806 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7807 mFakePolicy->setPointerCapture(true);
7808 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7809 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7810
7811 // captured touchpad should be a touchpad source
7812 NotifyDeviceResetArgs resetArgs;
7813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7814 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7815
Chris Yef74dc422020-09-02 22:41:50 -07007816 InputDeviceInfo deviceInfo;
7817 mDevice->getDeviceInfo(&deviceInfo);
7818
7819 const InputDeviceInfo::MotionRange* relRangeX =
7820 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
7821 ASSERT_NE(relRangeX, nullptr);
7822 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
7823 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
7824 const InputDeviceInfo::MotionRange* relRangeY =
7825 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
7826 ASSERT_NE(relRangeY, nullptr);
7827 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
7828 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
7829
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007830 // run captured pointer tests - note that this is unscaled, so input listener events should be
7831 // identical to what the hardware sends (accounting for any
7832 // calibration).
7833 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007834 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007835 processId(mapper, 1);
7836 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7837 processKey(mapper, BTN_TOUCH, 1);
7838 processSync(mapper);
7839
7840 // expect coord[0] to contain initial location of touch 0
7841 NotifyMotionArgs args;
7842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7843 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7844 ASSERT_EQ(1U, args.pointerCount);
7845 ASSERT_EQ(0, args.pointerProperties[0].id);
7846 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7847 ASSERT_NO_FATAL_FAILURE(
7848 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7849
7850 // FINGER 1 DOWN
7851 processSlot(mapper, 1);
7852 processId(mapper, 2);
7853 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7854 processSync(mapper);
7855
7856 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007858 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7859 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007860 ASSERT_EQ(2U, args.pointerCount);
7861 ASSERT_EQ(0, args.pointerProperties[0].id);
7862 ASSERT_EQ(1, args.pointerProperties[1].id);
7863 ASSERT_NO_FATAL_FAILURE(
7864 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7865 ASSERT_NO_FATAL_FAILURE(
7866 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7867
7868 // FINGER 1 MOVE
7869 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7870 processSync(mapper);
7871
7872 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7873 // from move
7874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7876 ASSERT_NO_FATAL_FAILURE(
7877 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7878 ASSERT_NO_FATAL_FAILURE(
7879 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7880
7881 // FINGER 0 MOVE
7882 processSlot(mapper, 0);
7883 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7884 processSync(mapper);
7885
7886 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7889 ASSERT_NO_FATAL_FAILURE(
7890 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7891 ASSERT_NO_FATAL_FAILURE(
7892 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7893
7894 // BUTTON DOWN
7895 processKey(mapper, BTN_LEFT, 1);
7896 processSync(mapper);
7897
7898 // touchinputmapper design sends a move before button press
7899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7903
7904 // BUTTON UP
7905 processKey(mapper, BTN_LEFT, 0);
7906 processSync(mapper);
7907
7908 // touchinputmapper design sends a move after button release
7909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7913
7914 // FINGER 0 UP
7915 processId(mapper, -1);
7916 processSync(mapper);
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7918 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7919
7920 // FINGER 1 MOVE
7921 processSlot(mapper, 1);
7922 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7923 processSync(mapper);
7924
7925 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7928 ASSERT_EQ(1U, args.pointerCount);
7929 ASSERT_EQ(1, args.pointerProperties[0].id);
7930 ASSERT_NO_FATAL_FAILURE(
7931 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7932
7933 // FINGER 1 UP
7934 processId(mapper, -1);
7935 processKey(mapper, BTN_TOUCH, 0);
7936 processSync(mapper);
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7938 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7939
7940 // non captured touchpad should be a mouse source
7941 mFakePolicy->setPointerCapture(false);
7942 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7944 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7945}
7946
7947TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7948 std::shared_ptr<FakePointerController> fakePointerController =
7949 std::make_shared<FakePointerController>();
7950 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7951 fakePointerController->setPosition(0, 0);
7952 fakePointerController->setButtonState(0);
7953
7954 // prepare device and capture
7955 prepareDisplay(DISPLAY_ORIENTATION_0);
7956 prepareAxes(POSITION | ID | SLOT);
7957 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7958 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7959 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7961 // run uncaptured pointer tests - pushes out generic events
7962 // FINGER 0 DOWN
7963 processId(mapper, 3);
7964 processPosition(mapper, 100, 100);
7965 processKey(mapper, BTN_TOUCH, 1);
7966 processSync(mapper);
7967
7968 // start at (100,100), cursor should be at (0,0) * scale
7969 NotifyMotionArgs args;
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7972 ASSERT_NO_FATAL_FAILURE(
7973 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7974
7975 // FINGER 0 MOVE
7976 processPosition(mapper, 200, 200);
7977 processSync(mapper);
7978
7979 // compute scaling to help with touch position checking
7980 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7981 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7982 float scale =
7983 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7984
7985 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7987 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7989 0, 0, 0, 0, 0, 0, 0));
7990}
7991
7992TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7993 std::shared_ptr<FakePointerController> fakePointerController =
7994 std::make_shared<FakePointerController>();
7995
7996 prepareDisplay(DISPLAY_ORIENTATION_0);
7997 prepareAxes(POSITION | ID | SLOT);
7998 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7999 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8000 mFakePolicy->setPointerCapture(false);
8001 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8002
8003 // uncaptured touchpad should be a pointer device
8004 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8005
8006 // captured touchpad should be a touchpad device
8007 mFakePolicy->setPointerCapture(true);
8008 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8009 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8010}
8011
Michael Wrightd02c5b62014-02-10 15:10:22 -08008012} // namespace android