blob: e9bb169d64f6d1a01757f1785b9fd5fbdd81eebe [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>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070030#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080032#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <math.h>
34
Michael Wright7a376672020-06-26 20:51:44 +010035#include <memory>
36
Michael Wrightd02c5b62014-02-10 15:10:22 -080037namespace android {
38
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039using std::chrono_literals::operator""ms;
40
41// Timeout for waiting for an expected event
42static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
43
Michael Wrightd02c5b62014-02-10 15:10:22 -080044// An arbitrary time value.
45static const nsecs_t ARBITRARY_TIME = 1234;
46
47// Arbitrary display properties.
arthurhung65600042020-04-30 17:55:40 +080048static constexpr int32_t DISPLAY_ID = 0;
49static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
50static constexpr int32_t DISPLAY_WIDTH = 480;
51static constexpr int32_t DISPLAY_HEIGHT = 800;
52static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
53static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
54static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070055static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070056static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080057
arthurhung65600042020-04-30 17:55:40 +080058static constexpr int32_t FIRST_SLOT = 0;
59static constexpr int32_t SECOND_SLOT = 1;
60static constexpr int32_t THIRD_SLOT = 2;
61static constexpr int32_t INVALID_TRACKING_ID = -1;
62static constexpr int32_t FIRST_TRACKING_ID = 0;
63static constexpr int32_t SECOND_TRACKING_ID = 1;
64static constexpr int32_t THIRD_TRACKING_ID = 2;
65
Michael Wrightd02c5b62014-02-10 15:10:22 -080066// Error tolerance for floating point assertions.
67static const float EPSILON = 0.001f;
68
69template<typename T>
70static inline T min(T a, T b) {
71 return a < b ? a : b;
72}
73
74static inline float avg(float x, float y) {
75 return (x + y) / 2;
76}
77
78
79// --- FakePointerController ---
80
81class FakePointerController : public PointerControllerInterface {
82 bool mHaveBounds;
83 float mMinX, mMinY, mMaxX, mMaxY;
84 float mX, mY;
85 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080086 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080087
Michael Wrightd02c5b62014-02-10 15:10:22 -080088public:
89 FakePointerController() :
90 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080091 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080092 }
93
Michael Wright7a376672020-06-26 20:51:44 +010094 virtual ~FakePointerController() {}
95
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 void setBounds(float minX, float minY, float maxX, float maxY) {
97 mHaveBounds = true;
98 mMinX = minX;
99 mMinY = minY;
100 mMaxX = maxX;
101 mMaxY = maxY;
102 }
103
104 virtual void setPosition(float x, float y) {
105 mX = x;
106 mY = y;
107 }
108
109 virtual void setButtonState(int32_t buttonState) {
110 mButtonState = buttonState;
111 }
112
113 virtual int32_t getButtonState() const {
114 return mButtonState;
115 }
116
117 virtual void getPosition(float* outX, float* outY) const {
118 *outX = mX;
119 *outY = mY;
120 }
121
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800122 virtual int32_t getDisplayId() const {
123 return mDisplayId;
124 }
125
Garfield Tan888a6a42020-01-09 11:39:16 -0800126 virtual void setDisplayViewport(const DisplayViewport& viewport) {
127 mDisplayId = viewport.displayId;
128 }
129
Arthur Hung7c645402019-01-25 17:45:42 +0800130 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
131 return mSpotsByDisplay;
132 }
133
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134private:
135 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
136 *outMinX = mMinX;
137 *outMinY = mMinY;
138 *outMaxX = mMaxX;
139 *outMaxY = mMaxY;
140 return mHaveBounds;
141 }
142
143 virtual void move(float deltaX, float deltaY) {
144 mX += deltaX;
145 if (mX < mMinX) mX = mMinX;
146 if (mX > mMaxX) mX = mMaxX;
147 mY += deltaY;
148 if (mY < mMinY) mY = mMinY;
149 if (mY > mMaxY) mY = mMaxY;
150 }
151
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100152 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153 }
154
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100155 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156 }
157
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100158 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159 }
160
Arthur Hung7c645402019-01-25 17:45:42 +0800161 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
162 int32_t displayId) {
163 std::vector<int32_t> newSpots;
164 // Add spots for fingers that are down.
165 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
166 uint32_t id = idBits.clearFirstMarkedBit();
167 newSpots.push_back(id);
168 }
169
170 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 }
172
173 virtual void clearSpots() {
174 }
Arthur Hung7c645402019-01-25 17:45:42 +0800175
176 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177};
178
179
180// --- FakeInputReaderPolicy ---
181
182class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700183 std::mutex mLock;
184 std::condition_variable mDevicesChangedCondition;
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 InputReaderConfiguration mConfig;
Michael Wright7a376672020-06-26 20:51:44 +0100187 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700188 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
189 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100190 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700191 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192
193protected:
194 virtual ~FakeInputReaderPolicy() { }
195
196public:
197 FakeInputReaderPolicy() {
198 }
199
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700200 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800201 waitForInputDevices([](bool devicesChanged) {
202 if (!devicesChanged) {
203 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
204 }
205 });
206 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700207
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800208 void assertInputDevicesNotChanged() {
209 waitForInputDevices([](bool devicesChanged) {
210 if (devicesChanged) {
211 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
212 }
213 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700214 }
215
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700216 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100217 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100218 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700219 }
220
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700221 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
222 return mConfig.getDisplayViewportByUniqueId(uniqueId);
223 }
224 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
225 return mConfig.getDisplayViewportByType(type);
226 }
227
228 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
229 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700230 }
231
232 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700233 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
234 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700235 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700236 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700237 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239 }
240
Arthur Hung6cd19a42019-08-30 19:04:12 +0800241 bool updateViewport(const DisplayViewport& viewport) {
242 size_t count = mViewports.size();
243 for (size_t i = 0; i < count; i++) {
244 const DisplayViewport& currentViewport = mViewports[i];
245 if (currentViewport.displayId == viewport.displayId) {
246 mViewports[i] = viewport;
247 mConfig.setDisplayViewports(mViewports);
248 return true;
249 }
250 }
251 // no viewport found.
252 return false;
253 }
254
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100255 void addExcludedDeviceName(const std::string& deviceName) {
256 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257 }
258
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700259 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
260 mConfig.portAssociations.insert({inputPort, displayPort});
261 }
262
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000263 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700264
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000265 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700266
Michael Wright7a376672020-06-26 20:51:44 +0100267 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
268 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 }
270
271 const InputReaderConfiguration* getReaderConfiguration() const {
272 return &mConfig;
273 }
274
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800275 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276 return mInputDevices;
277 }
278
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100279 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700280 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700281 return transform;
282 }
283
284 void setTouchAffineTransformation(const TouchAffineTransformation t) {
285 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800286 }
287
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800288 void setPointerCapture(bool enabled) {
289 mConfig.pointerCapture = enabled;
290 }
291
Arthur Hung7c645402019-01-25 17:45:42 +0800292 void setShowTouches(bool enabled) {
293 mConfig.showTouches = enabled;
294 }
295
Garfield Tan888a6a42020-01-09 11:39:16 -0800296 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
297 mConfig.defaultPointerDisplayId = pointerDisplayId;
298 }
299
Nathaniel R. Lewiseba157b2018-02-22 13:31:42 -0800300 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
301
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700303 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700304 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
305 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700306 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
307 || orientation == DISPLAY_ORIENTATION_270);
308 DisplayViewport v;
309 v.displayId = displayId;
310 v.orientation = orientation;
311 v.logicalLeft = 0;
312 v.logicalTop = 0;
313 v.logicalRight = isRotated ? height : width;
314 v.logicalBottom = isRotated ? width : height;
315 v.physicalLeft = 0;
316 v.physicalTop = 0;
317 v.physicalRight = isRotated ? height : width;
318 v.physicalBottom = isRotated ? width : height;
319 v.deviceWidth = isRotated ? height : width;
320 v.deviceHeight = isRotated ? width : height;
321 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700322 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100323 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700324 return v;
325 }
326
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
328 *outConfig = mConfig;
329 }
330
Michael Wright7a376672020-06-26 20:51:44 +0100331 virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
332 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 }
334
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800335 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700336 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700338 mInputDevicesChanged = true;
339 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 }
341
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100342 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700343 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100346 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
347 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800349
350 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
351 std::unique_lock<std::mutex> lock(mLock);
352 base::ScopedLockAssertion assumeLocked(mLock);
353
354 const bool devicesChanged =
355 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
356 return mInputDevicesChanged;
357 });
358 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
359 mInputDevicesChanged = false;
360 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361};
362
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363// --- FakeEventHub ---
364
365class FakeEventHub : public EventHubInterface {
366 struct KeyInfo {
367 int32_t keyCode;
368 uint32_t flags;
369 };
370
371 struct Device {
372 InputDeviceIdentifier identifier;
373 uint32_t classes;
374 PropertyMap configuration;
375 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
376 KeyedVector<int, bool> relativeAxes;
377 KeyedVector<int32_t, int32_t> keyCodeStates;
378 KeyedVector<int32_t, int32_t> scanCodeStates;
379 KeyedVector<int32_t, int32_t> switchStates;
380 KeyedVector<int32_t, int32_t> absoluteAxisValue;
381 KeyedVector<int32_t, KeyInfo> keysByScanCode;
382 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
383 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800384 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700385 bool enabled;
386
387 status_t enable() {
388 enabled = true;
389 return OK;
390 }
391
392 status_t disable() {
393 enabled = false;
394 return OK;
395 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800396
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700397 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700398 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399 }
400 };
401
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700402 std::mutex mLock;
403 std::condition_variable mEventsCondition;
404
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100406 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700407 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600408 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700410public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 virtual ~FakeEventHub() {
412 for (size_t i = 0; i < mDevices.size(); i++) {
413 delete mDevices.valueAt(i);
414 }
415 }
416
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 FakeEventHub() { }
418
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100419 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 Device* device = new Device(classes);
421 device->identifier.name = name;
422 mDevices.add(deviceId, device);
423
424 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
425 }
426
427 void removeDevice(int32_t deviceId) {
428 delete mDevices.valueFor(deviceId);
429 mDevices.removeItem(deviceId);
430
431 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
432 }
433
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700434 bool isDeviceEnabled(int32_t deviceId) {
435 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700436 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700437 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
438 return false;
439 }
440 return device->enabled;
441 }
442
443 status_t enableDevice(int32_t deviceId) {
444 status_t result;
445 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700446 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700447 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
448 return BAD_VALUE;
449 }
450 if (device->enabled) {
451 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
452 return OK;
453 }
454 result = device->enable();
455 return result;
456 }
457
458 status_t disableDevice(int32_t deviceId) {
459 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700460 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700461 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
462 return BAD_VALUE;
463 }
464 if (!device->enabled) {
465 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
466 return OK;
467 }
468 return device->disable();
469 }
470
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471 void finishDeviceScan() {
472 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
473 }
474
475 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
476 Device* device = getDevice(deviceId);
477 device->configuration.addProperty(key, value);
478 }
479
480 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
481 Device* device = getDevice(deviceId);
482 device->configuration.addAll(configuration);
483 }
484
485 void addAbsoluteAxis(int32_t deviceId, int axis,
486 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
487 Device* device = getDevice(deviceId);
488
489 RawAbsoluteAxisInfo info;
490 info.valid = true;
491 info.minValue = minValue;
492 info.maxValue = maxValue;
493 info.flat = flat;
494 info.fuzz = fuzz;
495 info.resolution = resolution;
496 device->absoluteAxes.add(axis, info);
497 }
498
499 void addRelativeAxis(int32_t deviceId, int32_t axis) {
500 Device* device = getDevice(deviceId);
501 device->relativeAxes.add(axis, true);
502 }
503
504 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
505 Device* device = getDevice(deviceId);
506 device->keyCodeStates.replaceValueFor(keyCode, state);
507 }
508
509 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
510 Device* device = getDevice(deviceId);
511 device->scanCodeStates.replaceValueFor(scanCode, state);
512 }
513
514 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
515 Device* device = getDevice(deviceId);
516 device->switchStates.replaceValueFor(switchCode, state);
517 }
518
519 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
520 Device* device = getDevice(deviceId);
521 device->absoluteAxisValue.replaceValueFor(axis, value);
522 }
523
524 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
525 int32_t keyCode, uint32_t flags) {
526 Device* device = getDevice(deviceId);
527 KeyInfo info;
528 info.keyCode = keyCode;
529 info.flags = flags;
530 if (scanCode) {
531 device->keysByScanCode.add(scanCode, info);
532 }
533 if (usageCode) {
534 device->keysByUsageCode.add(usageCode, info);
535 }
536 }
537
538 void addLed(int32_t deviceId, int32_t led, bool initialState) {
539 Device* device = getDevice(deviceId);
540 device->leds.add(led, initialState);
541 }
542
543 bool getLedState(int32_t deviceId, int32_t led) {
544 Device* device = getDevice(deviceId);
545 return device->leds.valueFor(led);
546 }
547
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100548 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549 return mExcludedDevices;
550 }
551
552 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
553 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800554 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555 }
556
557 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
558 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700559 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560 RawEvent event;
561 event.when = when;
562 event.deviceId = deviceId;
563 event.type = type;
564 event.code = code;
565 event.value = value;
566 mEvents.push_back(event);
567
568 if (type == EV_ABS) {
569 setAbsoluteAxisValue(deviceId, code, value);
570 }
571 }
572
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600573 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
574 std::vector<TouchVideoFrame>> videoFrames) {
575 mVideoFrames = std::move(videoFrames);
576 }
577
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700579 std::unique_lock<std::mutex> lock(mLock);
580 base::ScopedLockAssertion assumeLocked(mLock);
581 const bool queueIsEmpty =
582 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
583 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
584 if (!queueIsEmpty) {
585 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
586 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587 }
588
589private:
590 Device* getDevice(int32_t deviceId) const {
591 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100592 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 }
594
595 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
596 Device* device = getDevice(deviceId);
597 return device ? device->classes : 0;
598 }
599
600 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
601 Device* device = getDevice(deviceId);
602 return device ? device->identifier : InputDeviceIdentifier();
603 }
604
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100605 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 return 0;
607 }
608
609 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
610 Device* device = getDevice(deviceId);
611 if (device) {
612 *outConfiguration = device->configuration;
613 }
614 }
615
616 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
617 RawAbsoluteAxisInfo* outAxisInfo) const {
618 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800619 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620 ssize_t index = device->absoluteAxes.indexOfKey(axis);
621 if (index >= 0) {
622 *outAxisInfo = device->absoluteAxes.valueAt(index);
623 return OK;
624 }
625 }
626 outAxisInfo->clear();
627 return -1;
628 }
629
630 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
631 Device* device = getDevice(deviceId);
632 if (device) {
633 return device->relativeAxes.indexOfKey(axis) >= 0;
634 }
635 return false;
636 }
637
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100638 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800639 return false;
640 }
641
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700642 virtual status_t mapKey(int32_t deviceId,
643 int32_t scanCode, int32_t usageCode, int32_t metaState,
644 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 Device* device = getDevice(deviceId);
646 if (device) {
647 const KeyInfo* key = getKey(device, scanCode, usageCode);
648 if (key) {
649 if (outKeycode) {
650 *outKeycode = key->keyCode;
651 }
652 if (outFlags) {
653 *outFlags = key->flags;
654 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700655 if (outMetaState) {
656 *outMetaState = metaState;
657 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 return OK;
659 }
660 }
661 return NAME_NOT_FOUND;
662 }
663
664 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
665 if (usageCode) {
666 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
667 if (index >= 0) {
668 return &device->keysByUsageCode.valueAt(index);
669 }
670 }
671 if (scanCode) {
672 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
673 if (index >= 0) {
674 return &device->keysByScanCode.valueAt(index);
675 }
676 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700677 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678 }
679
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100680 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681 return NAME_NOT_FOUND;
682 }
683
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100684 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 mExcludedDevices = devices;
686 }
687
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100688 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700689 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800690 if (mEvents.empty()) {
691 return 0;
692 }
693
694 *buffer = *mEvents.begin();
695 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700696 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 return 1;
698 }
699
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800700 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600701 auto it = mVideoFrames.find(deviceId);
702 if (it != mVideoFrames.end()) {
703 std::vector<TouchVideoFrame> frames = std::move(it->second);
704 mVideoFrames.erase(deviceId);
705 return frames;
706 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800707 return {};
708 }
709
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
711 Device* device = getDevice(deviceId);
712 if (device) {
713 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
714 if (index >= 0) {
715 return device->scanCodeStates.valueAt(index);
716 }
717 }
718 return AKEY_STATE_UNKNOWN;
719 }
720
721 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
722 Device* device = getDevice(deviceId);
723 if (device) {
724 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
725 if (index >= 0) {
726 return device->keyCodeStates.valueAt(index);
727 }
728 }
729 return AKEY_STATE_UNKNOWN;
730 }
731
732 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
733 Device* device = getDevice(deviceId);
734 if (device) {
735 ssize_t index = device->switchStates.indexOfKey(sw);
736 if (index >= 0) {
737 return device->switchStates.valueAt(index);
738 }
739 }
740 return AKEY_STATE_UNKNOWN;
741 }
742
743 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
744 int32_t* outValue) const {
745 Device* device = getDevice(deviceId);
746 if (device) {
747 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
748 if (index >= 0) {
749 *outValue = device->absoluteAxisValue.valueAt(index);
750 return OK;
751 }
752 }
753 *outValue = 0;
754 return -1;
755 }
756
757 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
758 uint8_t* outFlags) const {
759 bool result = false;
760 Device* device = getDevice(deviceId);
761 if (device) {
762 for (size_t i = 0; i < numCodes; i++) {
763 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
764 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
765 outFlags[i] = 1;
766 result = true;
767 }
768 }
769 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
770 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
771 outFlags[i] = 1;
772 result = true;
773 }
774 }
775 }
776 }
777 return result;
778 }
779
780 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
781 Device* device = getDevice(deviceId);
782 if (device) {
783 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
784 return index >= 0;
785 }
786 return false;
787 }
788
789 virtual bool hasLed(int32_t deviceId, int32_t led) const {
790 Device* device = getDevice(deviceId);
791 return device && device->leds.indexOfKey(led) >= 0;
792 }
793
794 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
795 Device* device = getDevice(deviceId);
796 if (device) {
797 ssize_t index = device->leds.indexOfKey(led);
798 if (index >= 0) {
799 device->leds.replaceValueAt(led, on);
800 } else {
801 ADD_FAILURE()
802 << "Attempted to set the state of an LED that the EventHub declared "
803 "was not present. led=" << led;
804 }
805 }
806 }
807
808 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800809 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 outVirtualKeys.clear();
811
812 Device* device = getDevice(deviceId);
813 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800814 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800815 }
816 }
817
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100818 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700819 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 }
821
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100822 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 return false;
824 }
825
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100826 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 }
828
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100829 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800830 }
831
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100832 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833 return false;
834 }
835
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800836 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800837 }
838
839 virtual void monitor() {
840 }
841
842 virtual void requestReopenDevices() {
843 }
844
845 virtual void wake() {
846 }
847};
848
849
850// --- FakeInputReaderContext ---
851
852class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700853 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 sp<InputReaderPolicyInterface> mPolicy;
855 sp<InputListenerInterface> mListener;
856 int32_t mGlobalMetaState;
857 bool mUpdateGlobalMetaStateWasCalled;
858 int32_t mGeneration;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800859 int32_t mNextId;
Michael Wright7a376672020-06-26 20:51:44 +0100860 std::weak_ptr<PointerControllerInterface> mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861
862public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700863 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
864 const sp<InputReaderPolicyInterface>& policy,
865 const sp<InputListenerInterface>& listener)
866 : mEventHub(eventHub),
867 mPolicy(policy),
868 mListener(listener),
869 mGlobalMetaState(0),
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800870 mNextId(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800871
872 virtual ~FakeInputReaderContext() { }
873
874 void assertUpdateGlobalMetaStateWasCalled() {
875 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
876 << "Expected updateGlobalMetaState() to have been called.";
877 mUpdateGlobalMetaStateWasCalled = false;
878 }
879
880 void setGlobalMetaState(int32_t state) {
881 mGlobalMetaState = state;
882 }
883
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800884 uint32_t getGeneration() {
885 return mGeneration;
886 }
887
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800888 void updatePointerDisplay() {
Michael Wright7a376672020-06-26 20:51:44 +0100889 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800890 if (controller != nullptr) {
891 InputReaderConfiguration config;
892 mPolicy->getReaderConfiguration(&config);
893 auto viewport = config.getDisplayViewportById(config.defaultPointerDisplayId);
894 if (viewport) {
895 controller->setDisplayViewport(*viewport);
896 }
897 }
898 }
899
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900private:
901 virtual void updateGlobalMetaState() {
902 mUpdateGlobalMetaStateWasCalled = true;
903 }
904
905 virtual int32_t getGlobalMetaState() {
906 return mGlobalMetaState;
907 }
908
909 virtual EventHubInterface* getEventHub() {
910 return mEventHub.get();
911 }
912
913 virtual InputReaderPolicyInterface* getPolicy() {
914 return mPolicy.get();
915 }
916
917 virtual InputListenerInterface* getListener() {
918 return mListener.get();
919 }
920
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100921 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922 }
923
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800924 virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925
Michael Wright7a376672020-06-26 20:51:44 +0100926 virtual std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) {
927 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800928 if (controller == nullptr) {
929 controller = mPolicy->obtainPointerController(deviceId);
930 mPointerController = controller;
931 updatePointerDisplay();
932 }
933 return controller;
934 }
935
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 virtual void fadePointer() {
937 }
938
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100939 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 }
941
942 virtual int32_t bumpGeneration() {
943 return ++mGeneration;
944 }
Michael Wright842500e2015-03-13 17:32:02 -0700945
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800946 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700947
948 }
949
950 virtual void dispatchExternalStylusState(const StylusState&) {
951
952 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800953
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800954 virtual int32_t getNextId() { return mNextId++; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955};
956
957
958// --- FakeInputMapper ---
959
960class FakeInputMapper : public InputMapper {
961 uint32_t mSources;
962 int32_t mKeyboardType;
963 int32_t mMetaState;
964 KeyedVector<int32_t, int32_t> mKeyCodeStates;
965 KeyedVector<int32_t, int32_t> mScanCodeStates;
966 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800967 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700969 std::mutex mLock;
970 std::condition_variable mStateChangedCondition;
971 bool mConfigureWasCalled GUARDED_BY(mLock);
972 bool mResetWasCalled GUARDED_BY(mLock);
973 bool mProcessWasCalled GUARDED_BY(mLock);
974 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800975
Arthur Hungc23540e2018-11-29 20:42:11 +0800976 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800978 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
979 : InputMapper(deviceContext),
980 mSources(sources),
981 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800983 mConfigureWasCalled(false),
984 mResetWasCalled(false),
985 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986
987 virtual ~FakeInputMapper() { }
988
989 void setKeyboardType(int32_t keyboardType) {
990 mKeyboardType = keyboardType;
991 }
992
993 void setMetaState(int32_t metaState) {
994 mMetaState = metaState;
995 }
996
997 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700998 std::unique_lock<std::mutex> lock(mLock);
999 base::ScopedLockAssertion assumeLocked(mLock);
1000 const bool configureCalled =
1001 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1002 return mConfigureWasCalled;
1003 });
1004 if (!configureCalled) {
1005 FAIL() << "Expected configure() to have been called.";
1006 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007 mConfigureWasCalled = false;
1008 }
1009
1010 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001011 std::unique_lock<std::mutex> lock(mLock);
1012 base::ScopedLockAssertion assumeLocked(mLock);
1013 const bool resetCalled =
1014 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1015 return mResetWasCalled;
1016 });
1017 if (!resetCalled) {
1018 FAIL() << "Expected reset() to have been called.";
1019 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020 mResetWasCalled = false;
1021 }
1022
Yi Kong9b14ac62018-07-17 13:48:38 -07001023 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001024 std::unique_lock<std::mutex> lock(mLock);
1025 base::ScopedLockAssertion assumeLocked(mLock);
1026 const bool processCalled =
1027 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1028 return mProcessWasCalled;
1029 });
1030 if (!processCalled) {
1031 FAIL() << "Expected process() to have been called.";
1032 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001033 if (outLastEvent) {
1034 *outLastEvent = mLastEvent;
1035 }
1036 mProcessWasCalled = false;
1037 }
1038
1039 void setKeyCodeState(int32_t keyCode, int32_t state) {
1040 mKeyCodeStates.replaceValueFor(keyCode, state);
1041 }
1042
1043 void setScanCodeState(int32_t scanCode, int32_t state) {
1044 mScanCodeStates.replaceValueFor(scanCode, state);
1045 }
1046
1047 void setSwitchState(int32_t switchCode, int32_t state) {
1048 mSwitchStates.replaceValueFor(switchCode, state);
1049 }
1050
1051 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001052 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053 }
1054
1055private:
1056 virtual uint32_t getSources() {
1057 return mSources;
1058 }
1059
1060 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1061 InputMapper::populateDeviceInfo(deviceInfo);
1062
1063 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1064 deviceInfo->setKeyboardType(mKeyboardType);
1065 }
1066 }
1067
Arthur Hungc23540e2018-11-29 20:42:11 +08001068 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001069 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001071
1072 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001073 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001074 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1075 mViewport = config->getDisplayViewportByPort(*displayPort);
1076 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001077
1078 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079 }
1080
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001081 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001082 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001084 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085 }
1086
1087 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001088 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089 mLastEvent = *rawEvent;
1090 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001091 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092 }
1093
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001094 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1096 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1097 }
1098
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001099 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1101 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1102 }
1103
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001104 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1106 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1107 }
1108
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001109 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110 const int32_t* keyCodes, uint8_t* outFlags) {
1111 bool result = false;
1112 for (size_t i = 0; i < numCodes; i++) {
1113 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1114 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1115 outFlags[i] = 1;
1116 result = true;
1117 }
1118 }
1119 }
1120 return result;
1121 }
1122
1123 virtual int32_t getMetaState() {
1124 return mMetaState;
1125 }
1126
1127 virtual void fadePointer() {
1128 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001129
1130 virtual std::optional<int32_t> getAssociatedDisplay() {
1131 if (mViewport) {
1132 return std::make_optional(mViewport->displayId);
1133 }
1134 return std::nullopt;
1135 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001136};
1137
1138
1139// --- InstrumentedInputReader ---
1140
1141class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001142 std::shared_ptr<InputDevice> mNextDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143
1144public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001145 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1146 const sp<InputReaderPolicyInterface>& policy,
1147 const sp<InputListenerInterface>& listener)
1148 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001150 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001152 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001153
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001154 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001155 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156 InputDeviceIdentifier identifier;
1157 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001158 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001160 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161 }
1162
Prabir Pradhan28efc192019-11-05 01:10:04 +00001163 // Make the protected loopOnce method accessible to tests.
1164 using InputReader::loopOnce;
1165
Michael Wrightd02c5b62014-02-10 15:10:22 -08001166protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001167 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1168 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001169 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001170 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001171 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001172 return device;
1173 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001174 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175 }
1176
1177 friend class InputReaderTest;
1178};
1179
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001180// --- InputReaderPolicyTest ---
1181class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001182protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001183 sp<FakeInputReaderPolicy> mFakePolicy;
1184
Prabir Pradhan28efc192019-11-05 01:10:04 +00001185 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1186 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001187};
1188
1189/**
1190 * Check that empty set of viewports is an acceptable configuration.
1191 * Also try to get internal viewport two different ways - by type and by uniqueId.
1192 *
1193 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1194 * Such configuration is not currently allowed.
1195 */
1196TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001197 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001198
1199 // We didn't add any viewports yet, so there shouldn't be any.
1200 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001201 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001202 ASSERT_FALSE(internalViewport);
1203
1204 // Add an internal viewport, then clear it
1205 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001206 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001207
1208 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001209 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001211 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001212
1213 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001214 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001215 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001216 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001217
1218 mFakePolicy->clearViewports();
1219 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001220 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001221 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001222 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001223 ASSERT_FALSE(internalViewport);
1224}
1225
1226TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1227 const std::string internalUniqueId = "local:0";
1228 const std::string externalUniqueId = "local:1";
1229 const std::string virtualUniqueId1 = "virtual:2";
1230 const std::string virtualUniqueId2 = "virtual:3";
1231 constexpr int32_t virtualDisplayId1 = 2;
1232 constexpr int32_t virtualDisplayId2 = 3;
1233
1234 // Add an internal viewport
1235 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001236 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001237 // Add an external viewport
1238 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001239 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001240 // Add an virtual viewport
1241 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001242 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001243 // Add another virtual viewport
1244 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001245 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001246
1247 // Check matching by type for internal
1248 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001249 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001250 ASSERT_TRUE(internalViewport);
1251 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1252
1253 // Check matching by type for external
1254 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001255 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001256 ASSERT_TRUE(externalViewport);
1257 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1258
1259 // Check matching by uniqueId for virtual viewport #1
1260 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001261 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001262 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001263 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001264 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1265 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1266
1267 // Check matching by uniqueId for virtual viewport #2
1268 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001269 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001270 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001271 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001272 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1273 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1274}
1275
1276
1277/**
1278 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1279 * that lookup works by checking display id.
1280 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1281 */
1282TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1283 const std::string uniqueId1 = "uniqueId1";
1284 const std::string uniqueId2 = "uniqueId2";
1285 constexpr int32_t displayId1 = 2;
1286 constexpr int32_t displayId2 = 3;
1287
1288 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1289 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1290 for (const ViewportType& type : types) {
1291 mFakePolicy->clearViewports();
1292 // Add a viewport
1293 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001294 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001295 // Add another viewport
1296 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001297 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001298
1299 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001300 std::optional<DisplayViewport> viewport1 =
1301 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001302 ASSERT_TRUE(viewport1);
1303 ASSERT_EQ(displayId1, viewport1->displayId);
1304 ASSERT_EQ(type, viewport1->type);
1305
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001306 std::optional<DisplayViewport> viewport2 =
1307 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001308 ASSERT_TRUE(viewport2);
1309 ASSERT_EQ(displayId2, viewport2->displayId);
1310 ASSERT_EQ(type, viewport2->type);
1311
1312 // When there are multiple viewports of the same kind, and uniqueId is not specified
1313 // in the call to getDisplayViewport, then that situation is not supported.
1314 // The viewports can be stored in any order, so we cannot rely on the order, since that
1315 // is just implementation detail.
1316 // However, we can check that it still returns *a* viewport, we just cannot assert
1317 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001318 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001319 ASSERT_TRUE(someViewport);
1320 }
1321}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001323/**
1324 * Check getDisplayViewportByPort
1325 */
1326TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1327 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1328 const std::string uniqueId1 = "uniqueId1";
1329 const std::string uniqueId2 = "uniqueId2";
1330 constexpr int32_t displayId1 = 1;
1331 constexpr int32_t displayId2 = 2;
1332 const uint8_t hdmi1 = 0;
1333 const uint8_t hdmi2 = 1;
1334 const uint8_t hdmi3 = 2;
1335
1336 mFakePolicy->clearViewports();
1337 // Add a viewport that's associated with some display port that's not of interest.
1338 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1339 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1340 // Add another viewport, connected to HDMI1 port
1341 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1342 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1343
1344 // Check that correct display viewport was returned by comparing the display ports.
1345 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1346 ASSERT_TRUE(hdmi1Viewport);
1347 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1348 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1349
1350 // Check that we can still get the same viewport using the uniqueId
1351 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1352 ASSERT_TRUE(hdmi1Viewport);
1353 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1354 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1355 ASSERT_EQ(type, hdmi1Viewport->type);
1356
1357 // Check that we cannot find a port with "HDMI2", because we never added one
1358 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1359 ASSERT_FALSE(hdmi2Viewport);
1360}
1361
Michael Wrightd02c5b62014-02-10 15:10:22 -08001362// --- InputReaderTest ---
1363
1364class InputReaderTest : public testing::Test {
1365protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001366 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001367 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001368 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001369 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001370
Prabir Pradhan28efc192019-11-05 01:10:04 +00001371 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001372 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001374 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001375
Prabir Pradhan28efc192019-11-05 01:10:04 +00001376 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1377 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001378 }
1379
Prabir Pradhan28efc192019-11-05 01:10:04 +00001380 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381 mFakeListener.clear();
1382 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001383 }
1384
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001385 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1386 const PropertyMap* configuration) {
1387 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001388
1389 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001390 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001391 }
1392 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001393 mReader->loopOnce();
1394 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001395 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1396 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001397 }
1398
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001399 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001400 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001401 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001402 }
1403
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001404 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001405 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001406 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001407 }
1408
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001409 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001410 const std::string& name, uint32_t classes,
1411 uint32_t sources,
1412 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001413 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1414 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001415 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001416 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001417 return mapper;
1418 }
1419};
1420
1421TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001422 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001423 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001424 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001425 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001426
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001427 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001428 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001429 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001430 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001431 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001432 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1433 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1434 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1435
1436 // Should also have received a notification describing the new input devices.
1437 inputDevices = mFakePolicy->getInputDevices();
1438 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001439 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001440 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001441 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1442 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1443 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1444}
1445
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001446TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001447 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001448 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001449 constexpr int32_t eventHubId = 1;
1450 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001451 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001452 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001453 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001454 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001455
Yi Kong9b14ac62018-07-17 13:48:38 -07001456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001457
1458 NotifyDeviceResetArgs resetArgs;
1459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001460 ASSERT_EQ(deviceId, resetArgs.deviceId);
1461
1462 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001463 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001464 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001465
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001467 ASSERT_EQ(deviceId, resetArgs.deviceId);
1468 ASSERT_EQ(device->isEnabled(), false);
1469
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001470 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001471 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001474 ASSERT_EQ(device->isEnabled(), false);
1475
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001476 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001477 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001479 ASSERT_EQ(deviceId, resetArgs.deviceId);
1480 ASSERT_EQ(device->isEnabled(), true);
1481}
1482
Michael Wrightd02c5b62014-02-10 15:10:22 -08001483TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001484 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1485 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1486 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001487 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001488 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001489 AINPUT_SOURCE_KEYBOARD, nullptr);
1490 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001491
1492 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1493 AINPUT_SOURCE_ANY, AKEYCODE_A))
1494 << "Should return unknown when the device id is >= 0 but unknown.";
1495
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001496 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1497 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1498 << "Should return unknown when the device id is valid but the sources are not "
1499 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001500
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001501 ASSERT_EQ(AKEY_STATE_DOWN,
1502 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1503 AKEYCODE_A))
1504 << "Should return value provided by mapper when device id is valid and the device "
1505 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001506
1507 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1508 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1509 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1510
1511 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1512 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1513 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1514}
1515
1516TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001517 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1518 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1519 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001520 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001521 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001522 AINPUT_SOURCE_KEYBOARD, nullptr);
1523 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524
1525 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1526 AINPUT_SOURCE_ANY, KEY_A))
1527 << "Should return unknown when the device id is >= 0 but unknown.";
1528
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001529 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1530 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1531 << "Should return unknown when the device id is valid but the sources are not "
1532 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001533
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001534 ASSERT_EQ(AKEY_STATE_DOWN,
1535 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1536 KEY_A))
1537 << "Should return value provided by mapper when device id is valid and the device "
1538 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001539
1540 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1541 AINPUT_SOURCE_TRACKBALL, KEY_A))
1542 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1543
1544 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1545 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1546 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1547}
1548
1549TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001550 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1551 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1552 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001553 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001554 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001555 AINPUT_SOURCE_KEYBOARD, nullptr);
1556 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001557
1558 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1559 AINPUT_SOURCE_ANY, SW_LID))
1560 << "Should return unknown when the device id is >= 0 but unknown.";
1561
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001562 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1563 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1564 << "Should return unknown when the device id is valid but the sources are not "
1565 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001567 ASSERT_EQ(AKEY_STATE_DOWN,
1568 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1569 SW_LID))
1570 << "Should return value provided by mapper when device id is valid and the device "
1571 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001572
1573 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1574 AINPUT_SOURCE_TRACKBALL, SW_LID))
1575 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1576
1577 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1578 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1579 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1580}
1581
1582TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001583 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1584 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1585 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001586 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001588 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001589
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001590 mapper.addSupportedKeyCode(AKEYCODE_A);
1591 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592
1593 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1594 uint8_t flags[4] = { 0, 0, 0, 1 };
1595
1596 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1597 << "Should return false when device id is >= 0 but unknown.";
1598 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1599
1600 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001601 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1602 << "Should return false when device id is valid but the sources are not supported by "
1603 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1605
1606 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001607 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1608 keyCodes, flags))
1609 << "Should return value provided by mapper when device id is valid and the device "
1610 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001611 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1612
1613 flags[3] = 1;
1614 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1615 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1616 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1617
1618 flags[3] = 1;
1619 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1620 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1621 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1622}
1623
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001624TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 constexpr int32_t eventHubId = 1;
1626 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001627
1628 NotifyConfigurationChangedArgs args;
1629
1630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1631 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1632}
1633
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001634TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001635 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1636 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1637 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001638 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001640 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001641
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001643 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001644 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1645
1646 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001647 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001649 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650 ASSERT_EQ(EV_KEY, event.type);
1651 ASSERT_EQ(KEY_A, event.code);
1652 ASSERT_EQ(1, event.value);
1653}
1654
Garfield Tan1c7bc862020-01-28 13:24:04 -08001655TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001656 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001657 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 constexpr int32_t eventHubId = 1;
1659 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001660 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001661 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001662 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001663 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001664
1665 NotifyDeviceResetArgs resetArgs;
1666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001667 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001668
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001669 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001670 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001672 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001673 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001674
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001675 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001676 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001678 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001679 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001680
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001681 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001682 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001684 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001685 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001686}
1687
Garfield Tan1c7bc862020-01-28 13:24:04 -08001688TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1689 constexpr int32_t deviceId = 1;
1690 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1691 constexpr int32_t eventHubId = 1;
1692 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1693 // Must add at least one mapper or the device will be ignored!
1694 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1695 mReader->setNextDevice(device);
1696 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1697
1698 NotifyDeviceResetArgs resetArgs;
1699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1700 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1701}
1702
Arthur Hungc23540e2018-11-29 20:42:11 +08001703TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001704 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001705 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001707 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1709 FakeInputMapper& mapper =
1710 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001711 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001712
1713 const uint8_t hdmi1 = 1;
1714
1715 // Associated touch screen with second display.
1716 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1717
1718 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001719 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001720 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1721 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1722 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1723 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1724 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001725 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001726
1727 // Add the device, and make sure all of the callbacks are triggered.
1728 // The device is added after the input port associations are processed since
1729 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001730 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001733 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001734
Arthur Hung2c9a3342019-07-23 14:18:59 +08001735 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001736 ASSERT_EQ(deviceId, device->getId());
1737 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1738 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001739
1740 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001741 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001742 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001743 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001744}
1745
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001746// --- InputReaderIntegrationTest ---
1747
1748// These tests create and interact with the InputReader only through its interface.
1749// The InputReader is started during SetUp(), which starts its processing in its own
1750// thread. The tests use linux uinput to emulate input devices.
1751// NOTE: Interacting with the physical device while these tests are running may cause
1752// the tests to fail.
1753class InputReaderIntegrationTest : public testing::Test {
1754protected:
1755 sp<TestInputListener> mTestListener;
1756 sp<FakeInputReaderPolicy> mFakePolicy;
1757 sp<InputReaderInterface> mReader;
1758
1759 virtual void SetUp() override {
1760 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001761 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1762 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001763
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001764 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001765 ASSERT_EQ(mReader->start(), OK);
1766
1767 // Since this test is run on a real device, all the input devices connected
1768 // to the test device will show up in mReader. We wait for those input devices to
1769 // show up before beginning the tests.
1770 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1771 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1772 }
1773
1774 virtual void TearDown() override {
1775 ASSERT_EQ(mReader->stop(), OK);
1776 mTestListener.clear();
1777 mFakePolicy.clear();
1778 }
1779};
1780
1781TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1782 // An invalid input device that is only used for this test.
1783 class InvalidUinputDevice : public UinputDevice {
1784 public:
1785 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1786
1787 private:
1788 void configureDevice(int fd, uinput_user_dev* device) override {}
1789 };
1790
1791 const size_t numDevices = mFakePolicy->getInputDevices().size();
1792
1793 // UinputDevice does not set any event or key bits, so InputReader should not
1794 // consider it as a valid device.
1795 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1796 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1797 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1798 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1799
1800 invalidDevice.reset();
1801 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1802 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1803 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1804}
1805
1806TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1807 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1808
1809 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1810 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1811 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1812 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1813
1814 // Find the test device by its name.
1815 std::vector<InputDeviceInfo> inputDevices;
1816 mReader->getInputDevices(inputDevices);
1817 InputDeviceInfo* keyboardInfo = nullptr;
1818 const char* keyboardName = keyboard->getName();
1819 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1820 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1821 keyboardInfo = &inputDevices[i];
1822 break;
1823 }
1824 }
1825 ASSERT_NE(keyboardInfo, nullptr);
1826 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1827 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1828 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1829
1830 keyboard.reset();
1831 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1832 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1833 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1834}
1835
1836TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1837 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1838 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1839
1840 NotifyConfigurationChangedArgs configChangedArgs;
1841 ASSERT_NO_FATAL_FAILURE(
1842 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001843 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001844 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1845
1846 NotifyKeyArgs keyArgs;
1847 keyboard->pressAndReleaseHomeKey();
1848 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1849 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001850 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001851 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001852 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1853 prevTimestamp = keyArgs.eventTime;
1854
1855 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1856 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001857 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001858 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1859}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001860
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001861/**
1862 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1863 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1864 * are passed to the listener.
1865 */
1866static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1867TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1868 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1869 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1870 NotifyKeyArgs keyArgs;
1871
1872 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1873 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1874 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1875 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1876
1877 controller->pressAndReleaseKey(BTN_GEAR_UP);
1878 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1879 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1880 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1881}
1882
Arthur Hungaab25622020-01-16 11:22:11 +08001883// --- TouchProcessTest ---
1884class TouchIntegrationTest : public InputReaderIntegrationTest {
1885protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001886 const std::string UNIQUE_ID = "local:0";
1887
1888 virtual void SetUp() override {
1889 InputReaderIntegrationTest::SetUp();
1890 // At least add an internal display.
1891 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1892 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
1893 ViewportType::VIEWPORT_INTERNAL);
1894
1895 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1896 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1897 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1898 }
1899
1900 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1901 int32_t orientation, const std::string& uniqueId,
1902 std::optional<uint8_t> physicalPort,
1903 ViewportType viewportType) {
1904 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1905 physicalPort, viewportType);
1906 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1907 }
1908
1909 std::unique_ptr<UinputTouchScreen> mDevice;
1910};
1911
1912TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1913 NotifyMotionArgs args;
1914 const Point centerPoint = mDevice->getCenterPoint();
1915
1916 // ACTION_DOWN
1917 mDevice->sendDown(centerPoint);
1918 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1919 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1920
1921 // ACTION_MOVE
1922 mDevice->sendMove(centerPoint + Point(1, 1));
1923 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1925
1926 // ACTION_UP
1927 mDevice->sendUp();
1928 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1929 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1930}
1931
1932TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1933 NotifyMotionArgs args;
1934 const Point centerPoint = mDevice->getCenterPoint();
1935
1936 // ACTION_DOWN
1937 mDevice->sendDown(centerPoint);
1938 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1939 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1940
1941 // ACTION_POINTER_DOWN (Second slot)
1942 const Point secondPoint = centerPoint + Point(100, 100);
1943 mDevice->sendSlot(SECOND_SLOT);
1944 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1945 mDevice->sendDown(secondPoint + Point(1, 1));
1946 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1947 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1948 args.action);
1949
1950 // ACTION_MOVE (Second slot)
1951 mDevice->sendMove(secondPoint);
1952 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1954
1955 // ACTION_POINTER_UP (Second slot)
arthurhung65600042020-04-30 17:55:40 +08001956 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08001957 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhung65600042020-04-30 17:55:40 +08001958 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08001959 args.action);
1960
1961 // ACTION_UP
1962 mDevice->sendSlot(FIRST_SLOT);
1963 mDevice->sendUp();
1964 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1965 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1966}
1967
1968TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1969 NotifyMotionArgs args;
1970 const Point centerPoint = mDevice->getCenterPoint();
1971
1972 // ACTION_DOWN
arthurhung65600042020-04-30 17:55:40 +08001973 mDevice->sendSlot(FIRST_SLOT);
1974 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001975 mDevice->sendDown(centerPoint);
1976 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1977 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1978
arthurhung65600042020-04-30 17:55:40 +08001979 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001980 const Point secondPoint = centerPoint + Point(100, 100);
1981 mDevice->sendSlot(SECOND_SLOT);
1982 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1983 mDevice->sendDown(secondPoint);
1984 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1985 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1986 args.action);
1987
arthurhung65600042020-04-30 17:55:40 +08001988 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001989 mDevice->sendMove(secondPoint + Point(1, 1));
1990 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1992
arthurhung65600042020-04-30 17:55:40 +08001993 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1994 // a palm event.
1995 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001996 mDevice->sendToolType(MT_TOOL_PALM);
1997 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhung65600042020-04-30 17:55:40 +08001998 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1999 args.action);
2000 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002001
arthurhung65600042020-04-30 17:55:40 +08002002 // Send up to second slot, expect first slot send moving.
2003 mDevice->sendPointerUp();
2004 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002006
arthurhung65600042020-04-30 17:55:40 +08002007 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002008 mDevice->sendSlot(FIRST_SLOT);
2009 mDevice->sendUp();
2010
arthurhung65600042020-04-30 17:55:40 +08002011 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2012 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002013}
2014
Michael Wrightd02c5b62014-02-10 15:10:22 -08002015// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002016class InputDeviceTest : public testing::Test {
2017protected:
2018 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002019 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002020 static const int32_t DEVICE_ID;
2021 static const int32_t DEVICE_GENERATION;
2022 static const int32_t DEVICE_CONTROLLER_NUMBER;
2023 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002024 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002025
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002026 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002027 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002028 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002029 FakeInputReaderContext* mFakeContext;
2030
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002031 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002032
Prabir Pradhan28efc192019-11-05 01:10:04 +00002033 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002034 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002035 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002036 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002037 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2038
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002039 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002040 InputDeviceIdentifier identifier;
2041 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002042 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002043 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2044 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002045 }
2046
Prabir Pradhan28efc192019-11-05 01:10:04 +00002047 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002048 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002049 delete mFakeContext;
2050 mFakeListener.clear();
2051 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002052 }
2053};
2054
2055const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002056const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002057const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002058const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2059const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
2060const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
2061 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002062const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002063
2064TEST_F(InputDeviceTest, ImmutableProperties) {
2065 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002066 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002067 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002068}
2069
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002070TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2071 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002072}
2073
Michael Wrightd02c5b62014-02-10 15:10:22 -08002074TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2075 // Configuration.
2076 InputReaderConfiguration config;
2077 mDevice->configure(ARBITRARY_TIME, &config, 0);
2078
2079 // Reset.
2080 mDevice->reset(ARBITRARY_TIME);
2081
2082 NotifyDeviceResetArgs resetArgs;
2083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2084 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2085 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2086
2087 // Metadata.
2088 ASSERT_TRUE(mDevice->isIgnored());
2089 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2090
2091 InputDeviceInfo info;
2092 mDevice->getDeviceInfo(&info);
2093 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002094 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002095 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2096 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2097
2098 // State queries.
2099 ASSERT_EQ(0, mDevice->getMetaState());
2100
2101 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2102 << "Ignored device should return unknown key code state.";
2103 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2104 << "Ignored device should return unknown scan code state.";
2105 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2106 << "Ignored device should return unknown switch state.";
2107
2108 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2109 uint8_t flags[2] = { 0, 1 };
2110 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2111 << "Ignored device should never mark any key codes.";
2112 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2113 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2114}
2115
2116TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2117 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002118 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002119
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002120 FakeInputMapper& mapper1 =
2121 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002122 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2123 mapper1.setMetaState(AMETA_ALT_ON);
2124 mapper1.addSupportedKeyCode(AKEYCODE_A);
2125 mapper1.addSupportedKeyCode(AKEYCODE_B);
2126 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2127 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2128 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2129 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2130 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002131
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002132 FakeInputMapper& mapper2 =
2133 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002134 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002135
2136 InputReaderConfiguration config;
2137 mDevice->configure(ARBITRARY_TIME, &config, 0);
2138
2139 String8 propertyValue;
2140 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2141 << "Device should have read configuration during configuration phase.";
2142 ASSERT_STREQ("value", propertyValue.string());
2143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002144 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2145 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002146
2147 // Reset
2148 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002149 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2150 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002151
2152 NotifyDeviceResetArgs resetArgs;
2153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2154 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2155 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2156
2157 // Metadata.
2158 ASSERT_FALSE(mDevice->isIgnored());
2159 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2160
2161 InputDeviceInfo info;
2162 mDevice->getDeviceInfo(&info);
2163 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002164 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002165 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2166 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2167
2168 // State queries.
2169 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2170 << "Should query mappers and combine meta states.";
2171
2172 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2173 << "Should return unknown key code state when source not supported.";
2174 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2175 << "Should return unknown scan code state when source not supported.";
2176 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2177 << "Should return unknown switch state when source not supported.";
2178
2179 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2180 << "Should query mapper when source is supported.";
2181 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2182 << "Should query mapper when source is supported.";
2183 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2184 << "Should query mapper when source is supported.";
2185
2186 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2187 uint8_t flags[4] = { 0, 0, 0, 1 };
2188 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2189 << "Should do nothing when source is unsupported.";
2190 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2191 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2192 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2193 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2194
2195 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2196 << "Should query mapper when source is supported.";
2197 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2198 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2199 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2200 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2201
2202 // Event handling.
2203 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002204 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002205 mDevice->process(&event, 1);
2206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002207 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2208 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002209}
2210
Arthur Hung2c9a3342019-07-23 14:18:59 +08002211// A single input device is associated with a specific display. Check that:
2212// 1. Device is disabled if the viewport corresponding to the associated display is not found
2213// 2. Device is disabled when setEnabled API is called
2214TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002215 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002216
2217 // First Configuration.
2218 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2219
2220 // Device should be enabled by default.
2221 ASSERT_TRUE(mDevice->isEnabled());
2222
2223 // Prepare associated info.
2224 constexpr uint8_t hdmi = 1;
2225 const std::string UNIQUE_ID = "local:1";
2226
2227 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2228 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2229 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2230 // Device should be disabled because it is associated with a specific display via
2231 // input port <-> display port association, but the corresponding display is not found
2232 ASSERT_FALSE(mDevice->isEnabled());
2233
2234 // Prepare displays.
2235 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2236 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2237 ViewportType::VIEWPORT_INTERNAL);
2238 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2239 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2240 ASSERT_TRUE(mDevice->isEnabled());
2241
2242 // Device should be disabled after set disable.
2243 mFakePolicy->addDisabledDevice(mDevice->getId());
2244 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2245 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2246 ASSERT_FALSE(mDevice->isEnabled());
2247
2248 // Device should still be disabled even found the associated display.
2249 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2250 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2251 ASSERT_FALSE(mDevice->isEnabled());
2252}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002253
2254// --- InputMapperTest ---
2255
2256class InputMapperTest : public testing::Test {
2257protected:
2258 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002259 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002260 static const int32_t DEVICE_ID;
2261 static const int32_t DEVICE_GENERATION;
2262 static const int32_t DEVICE_CONTROLLER_NUMBER;
2263 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002264 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002265
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002266 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002267 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002268 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002269 FakeInputReaderContext* mFakeContext;
2270 InputDevice* mDevice;
2271
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002272 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002273 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002275 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002276 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2277 InputDeviceIdentifier identifier;
2278 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002279 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002280 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002281
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002282 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002283 }
2284
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002285 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2286
Prabir Pradhan28efc192019-11-05 01:10:04 +00002287 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288 delete mDevice;
2289 delete mFakeContext;
2290 mFakeListener.clear();
2291 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002292 }
2293
2294 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002295 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002296 }
2297
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002298 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002299 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2300 mFakeContext->updatePointerDisplay();
2301 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002302 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2303 }
2304
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002305 template <class T, typename... Args>
2306 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002307 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002308 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002309 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002310 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002311 }
2312
2313 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002314 int32_t orientation, const std::string& uniqueId,
2315 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002316 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002317 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002318 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2319 }
2320
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002321 void clearViewports() {
2322 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323 }
2324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002325 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2326 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002327 RawEvent event;
2328 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002329 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002330 event.type = type;
2331 event.code = code;
2332 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002333 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002334 }
2335
2336 static void assertMotionRange(const InputDeviceInfo& info,
2337 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2338 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002339 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002340 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2341 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2342 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2343 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2344 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2345 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2346 }
2347
2348 static void assertPointerCoords(const PointerCoords& coords,
2349 float x, float y, float pressure, float size,
2350 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2351 float orientation, float distance) {
2352 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2353 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2354 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2355 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2356 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2357 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2358 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2359 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2360 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2361 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2362 }
2363
Michael Wright7a376672020-06-26 20:51:44 +01002364 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365 float actualX, actualY;
Michael Wright7a376672020-06-26 20:51:44 +01002366 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002367 ASSERT_NEAR(x, actualX, 1);
2368 ASSERT_NEAR(y, actualY, 1);
2369 }
2370};
2371
2372const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002373const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002374const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002375const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2376const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2377const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002378const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379
2380// --- SwitchInputMapperTest ---
2381
2382class SwitchInputMapperTest : public InputMapperTest {
2383protected:
2384};
2385
2386TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002387 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002388
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002389 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390}
2391
2392TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002393 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002395 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002396 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002397
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002398 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002399 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400}
2401
2402TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002403 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002405 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2406 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2407 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2408 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409
2410 NotifySwitchArgs args;
2411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2412 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002413 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2414 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415 args.switchMask);
2416 ASSERT_EQ(uint32_t(0), args.policyFlags);
2417}
2418
2419
2420// --- KeyboardInputMapperTest ---
2421
2422class KeyboardInputMapperTest : public InputMapperTest {
2423protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002424 const std::string UNIQUE_ID = "local:0";
2425
2426 void prepareDisplay(int32_t orientation);
2427
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002428 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002429 int32_t originalKeyCode, int32_t rotatedKeyCode,
2430 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002431};
2432
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002433/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2434 * orientation.
2435 */
2436void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2437 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002438 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002439}
2440
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002441void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002442 int32_t originalScanCode, int32_t originalKeyCode,
2443 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002444 NotifyKeyArgs args;
2445
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002446 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2448 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2449 ASSERT_EQ(originalScanCode, args.scanCode);
2450 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002451 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002453 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2455 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2456 ASSERT_EQ(originalScanCode, args.scanCode);
2457 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002458 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459}
2460
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002462 KeyboardInputMapper& mapper =
2463 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2464 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002466 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467}
2468
2469TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2470 const int32_t USAGE_A = 0x070004;
2471 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002472 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2473 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002475 KeyboardInputMapper& mapper =
2476 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2477 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002478
2479 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002480 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002481 NotifyKeyArgs args;
2482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2483 ASSERT_EQ(DEVICE_ID, args.deviceId);
2484 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2485 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2486 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2487 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2488 ASSERT_EQ(KEY_HOME, args.scanCode);
2489 ASSERT_EQ(AMETA_NONE, args.metaState);
2490 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2491 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2492 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2493
2494 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002495 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2497 ASSERT_EQ(DEVICE_ID, args.deviceId);
2498 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2499 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2500 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2501 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2502 ASSERT_EQ(KEY_HOME, args.scanCode);
2503 ASSERT_EQ(AMETA_NONE, args.metaState);
2504 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2505 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2506 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2507
2508 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002509 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2510 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2512 ASSERT_EQ(DEVICE_ID, args.deviceId);
2513 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2514 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2515 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2516 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2517 ASSERT_EQ(0, args.scanCode);
2518 ASSERT_EQ(AMETA_NONE, args.metaState);
2519 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2520 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2521 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2522
2523 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002524 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2525 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2527 ASSERT_EQ(DEVICE_ID, args.deviceId);
2528 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2529 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2530 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2531 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2532 ASSERT_EQ(0, args.scanCode);
2533 ASSERT_EQ(AMETA_NONE, args.metaState);
2534 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2535 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2536 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2537
2538 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002539 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2540 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2542 ASSERT_EQ(DEVICE_ID, args.deviceId);
2543 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2544 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2545 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2546 ASSERT_EQ(0, args.keyCode);
2547 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2548 ASSERT_EQ(AMETA_NONE, args.metaState);
2549 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2550 ASSERT_EQ(0U, args.policyFlags);
2551 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2552
2553 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002554 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2555 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2557 ASSERT_EQ(DEVICE_ID, args.deviceId);
2558 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2559 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2560 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2561 ASSERT_EQ(0, args.keyCode);
2562 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2563 ASSERT_EQ(AMETA_NONE, args.metaState);
2564 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2565 ASSERT_EQ(0U, args.policyFlags);
2566 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2567}
2568
2569TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002570 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2571 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002572
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002573 KeyboardInputMapper& mapper =
2574 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2575 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
2577 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002578 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579
2580 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002581 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002582 NotifyKeyArgs args;
2583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2584 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002585 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002586 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2587
2588 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002589 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2591 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002592 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002593
2594 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002595 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002598 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599
2600 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002601 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2603 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002604 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002605 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2606}
2607
2608TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002609 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2610 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2611 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2612 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002614 KeyboardInputMapper& mapper =
2615 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2616 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002618 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2620 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2622 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2624 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2626 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2627}
2628
2629TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002630 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2631 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2632 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2633 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002636 KeyboardInputMapper& mapper =
2637 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2638 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002640 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002641 ASSERT_NO_FATAL_FAILURE(
2642 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2644 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2646 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2647 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2648 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002650 clearViewports();
2651 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002652 ASSERT_NO_FATAL_FAILURE(
2653 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2654 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2655 AKEYCODE_DPAD_UP, DISPLAY_ID));
2656 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2657 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2658 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2659 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002661 clearViewports();
2662 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002663 ASSERT_NO_FATAL_FAILURE(
2664 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2665 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2666 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2667 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2668 AKEYCODE_DPAD_UP, DISPLAY_ID));
2669 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2670 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002672 clearViewports();
2673 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002674 ASSERT_NO_FATAL_FAILURE(
2675 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2676 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2677 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2678 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2679 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2680 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2681 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682
2683 // Special case: if orientation changes while key is down, we still emit the same keycode
2684 // in the key up as we did in the key down.
2685 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002686 clearViewports();
2687 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002688 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2690 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2691 ASSERT_EQ(KEY_UP, args.scanCode);
2692 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2693
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002694 clearViewports();
2695 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002696 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2698 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2699 ASSERT_EQ(KEY_UP, args.scanCode);
2700 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2701}
2702
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002703TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2704 // If the keyboard is not orientation aware,
2705 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002706 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002707
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002708 KeyboardInputMapper& mapper =
2709 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2710 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002711 NotifyKeyArgs args;
2712
2713 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002714 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002716 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2718 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2719
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002720 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002721 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002723 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2725 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2726}
2727
2728TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2729 // If the keyboard is orientation aware,
2730 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002731 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002732
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002733 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002734 KeyboardInputMapper& mapper =
2735 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2736 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002737 NotifyKeyArgs args;
2738
2739 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2740 // ^--- already checked by the previous test
2741
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002742 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002743 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002744 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002746 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2748 ASSERT_EQ(DISPLAY_ID, args.displayId);
2749
2750 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002751 clearViewports();
2752 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002753 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002754 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002756 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2758 ASSERT_EQ(newDisplayId, args.displayId);
2759}
2760
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002762 KeyboardInputMapper& mapper =
2763 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2764 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002766 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002769 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002770 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771}
2772
2773TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002774 KeyboardInputMapper& mapper =
2775 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2776 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002777
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002778 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002779 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002781 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002783}
2784
2785TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002786 KeyboardInputMapper& mapper =
2787 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2788 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002789
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002790 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791
2792 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2793 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002794 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 ASSERT_TRUE(flags[0]);
2796 ASSERT_FALSE(flags[1]);
2797}
2798
2799TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002800 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2801 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2802 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2803 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2804 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2805 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002807 KeyboardInputMapper& mapper =
2808 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2809 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810
2811 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002812 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2813 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2814 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815
2816 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002817 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2818 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002819 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2820 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2821 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002822 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002823
2824 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002825 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2826 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002827 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2828 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2829 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002830 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002831
2832 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002833 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2834 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002835 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2836 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2837 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002838 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839
2840 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002841 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2842 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002843 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2844 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2845 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002846 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002847
2848 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002849 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2850 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002851 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2852 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2853 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002854 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002855
2856 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002857 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2858 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002859 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2860 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2861 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002862 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863}
2864
Arthur Hung2c9a3342019-07-23 14:18:59 +08002865TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2866 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002867 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2868 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2869 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2870 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002871
2872 // keyboard 2.
2873 const std::string USB2 = "USB2";
2874 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002875 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002876 InputDeviceIdentifier identifier;
2877 identifier.name = "KEYBOARD2";
2878 identifier.location = USB2;
2879 std::unique_ptr<InputDevice> device2 =
2880 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002881 identifier);
2882 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2883 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2884 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2885 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2886 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002887
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002888 KeyboardInputMapper& mapper =
2889 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2890 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002892 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002893 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002894 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002895 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2896 device2->reset(ARBITRARY_TIME);
2897
2898 // Prepared displays and associated info.
2899 constexpr uint8_t hdmi1 = 0;
2900 constexpr uint8_t hdmi2 = 1;
2901 const std::string SECONDARY_UNIQUE_ID = "local:1";
2902
2903 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2904 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2905
2906 // No associated display viewport found, should disable the device.
2907 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2908 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2909 ASSERT_FALSE(device2->isEnabled());
2910
2911 // Prepare second display.
2912 constexpr int32_t newDisplayId = 2;
2913 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2914 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2915 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2916 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2917 // Default device will reconfigure above, need additional reconfiguration for another device.
2918 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2919 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2920
2921 // Device should be enabled after the associated display is found.
2922 ASSERT_TRUE(mDevice->isEnabled());
2923 ASSERT_TRUE(device2->isEnabled());
2924
2925 // Test pad key events
2926 ASSERT_NO_FATAL_FAILURE(
2927 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2928 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2929 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2930 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2931 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2932 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2933 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2934
2935 ASSERT_NO_FATAL_FAILURE(
2936 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2937 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2938 AKEYCODE_DPAD_RIGHT, newDisplayId));
2939 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2940 AKEYCODE_DPAD_DOWN, newDisplayId));
2941 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2942 AKEYCODE_DPAD_LEFT, newDisplayId));
2943}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002945// --- KeyboardInputMapperTest_ExternalDevice ---
2946
2947class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2948protected:
2949 virtual void SetUp() override {
2950 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2951 }
2952};
2953
2954TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002955 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2956 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002957
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002958 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2959 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2960 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2961 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002963 KeyboardInputMapper& mapper =
2964 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2965 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002966
2967 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2968 NotifyKeyArgs args;
2969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2970 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2971
2972 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2974 ASSERT_EQ(uint32_t(0), args.policyFlags);
2975
2976 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2978 ASSERT_EQ(uint32_t(0), args.policyFlags);
2979
2980 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2982 ASSERT_EQ(uint32_t(0), args.policyFlags);
2983
2984 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2986 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2987
2988 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2990 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2991}
2992
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002993TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002994 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002995
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2997 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2998 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002999
Powei Fengd041c5d2019-05-03 17:11:33 -07003000 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003001 KeyboardInputMapper& mapper =
3002 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3003 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003004
3005 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3006 NotifyKeyArgs args;
3007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3008 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3009
3010 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3012 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3013
3014 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3016 ASSERT_EQ(uint32_t(0), args.policyFlags);
3017
3018 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3020 ASSERT_EQ(uint32_t(0), args.policyFlags);
3021
3022 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3024 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3025
3026 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3028 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3029}
3030
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031// --- CursorInputMapperTest ---
3032
3033class CursorInputMapperTest : public InputMapperTest {
3034protected:
3035 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3036
Michael Wright7a376672020-06-26 20:51:44 +01003037 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003038
Prabir Pradhan28efc192019-11-05 01:10:04 +00003039 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040 InputMapperTest::SetUp();
3041
Michael Wright7a376672020-06-26 20:51:44 +01003042 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003043 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044 }
3045
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003046 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3047 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003048
3049 void prepareDisplay(int32_t orientation) {
3050 const std::string uniqueId = "local:0";
3051 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
3052 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3053 orientation, uniqueId, NO_PORT, viewportType);
3054 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055};
3056
3057const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3060 int32_t originalY, int32_t rotatedX,
3061 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062 NotifyMotionArgs args;
3063
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003064 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3065 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3066 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3070 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3071 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3072 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3073}
3074
3075TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003077 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003079 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080}
3081
3082TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003086 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087}
3088
3089TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092
3093 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003094 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095
3096 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003097 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3098 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3100 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3101
3102 // When the bounds are set, then there should be a valid motion range.
3103 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3104
3105 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003106 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107
3108 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3109 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3110 1, 800 - 1, 0.0f, 0.0f));
3111 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3112 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3113 2, 480 - 1, 0.0f, 0.0f));
3114 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3115 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3116 0.0f, 1.0f, 0.0f, 0.0f));
3117}
3118
3119TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003121 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122
3123 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003124 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125
3126 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3127 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3128 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3129 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3130 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3131 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3132 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3133 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3134 0.0f, 1.0f, 0.0f, 0.0f));
3135}
3136
3137TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003139 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140
3141 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3142
3143 NotifyMotionArgs args;
3144
3145 // Button press.
3146 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003147 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3148 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3150 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3151 ASSERT_EQ(DEVICE_ID, args.deviceId);
3152 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3153 ASSERT_EQ(uint32_t(0), args.policyFlags);
3154 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3155 ASSERT_EQ(0, args.flags);
3156 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3157 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3158 ASSERT_EQ(0, args.edgeFlags);
3159 ASSERT_EQ(uint32_t(1), args.pointerCount);
3160 ASSERT_EQ(0, args.pointerProperties[0].id);
3161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3163 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3164 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3165 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3166 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3167
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3169 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3170 ASSERT_EQ(DEVICE_ID, args.deviceId);
3171 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3172 ASSERT_EQ(uint32_t(0), args.policyFlags);
3173 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3174 ASSERT_EQ(0, args.flags);
3175 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3176 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3177 ASSERT_EQ(0, args.edgeFlags);
3178 ASSERT_EQ(uint32_t(1), args.pointerCount);
3179 ASSERT_EQ(0, args.pointerProperties[0].id);
3180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3182 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3183 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3184 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3185 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3186
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003188 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3189 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3191 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3192 ASSERT_EQ(DEVICE_ID, args.deviceId);
3193 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3194 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3196 ASSERT_EQ(0, args.flags);
3197 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3198 ASSERT_EQ(0, args.buttonState);
3199 ASSERT_EQ(0, args.edgeFlags);
3200 ASSERT_EQ(uint32_t(1), args.pointerCount);
3201 ASSERT_EQ(0, args.pointerProperties[0].id);
3202 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3204 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3205 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3206 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3207 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3208
3209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3210 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3211 ASSERT_EQ(DEVICE_ID, args.deviceId);
3212 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3213 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3215 ASSERT_EQ(0, args.flags);
3216 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3217 ASSERT_EQ(0, args.buttonState);
3218 ASSERT_EQ(0, args.edgeFlags);
3219 ASSERT_EQ(uint32_t(1), args.pointerCount);
3220 ASSERT_EQ(0, args.pointerProperties[0].id);
3221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3223 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3224 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3225 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3226 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3227}
3228
3229TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003231 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232
3233 NotifyMotionArgs args;
3234
3235 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003236 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3237 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3241 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3242
3243 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003244 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3245 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3249 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3250}
3251
3252TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003254 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255
3256 NotifyMotionArgs args;
3257
3258 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003259 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3260 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3264 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3265
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3269 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3270
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003272 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3273 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3277 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3278
3279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3282 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3283}
3284
3285TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003287 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288
3289 NotifyMotionArgs args;
3290
3291 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003292 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3293 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3294 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3295 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3297 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3299 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3300 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3301
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3305 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3306 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3307
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003309 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3310 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3311 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3315 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3316 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3317
3318 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003319 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3320 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3324 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3325
3326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3329 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3330}
3331
3332TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003334 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003336 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3338 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3339 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3340 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3341 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3345}
3346
3347TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 addConfigurationProperty("cursor.mode", "navigation");
3349 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003350 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003352 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3354 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3355 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3356 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3357 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3358 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3359 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3360 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3361
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003362 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3364 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3365 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3366 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3367 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3368 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3369 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3371
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003372 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3374 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3375 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3376 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3377 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3378 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3379 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3380 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3381
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003382 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3384 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3385 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3386 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3387 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3388 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3391}
3392
3393TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003395 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396
3397 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3398 mFakePointerController->setPosition(100, 200);
3399 mFakePointerController->setButtonState(0);
3400
3401 NotifyMotionArgs motionArgs;
3402 NotifyKeyArgs keyArgs;
3403
3404 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003405 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3406 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3408 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3409 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3410 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3412 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3413
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3415 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3416 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3417 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3419 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3420
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003421 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3422 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003424 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_EQ(0, motionArgs.buttonState);
3426 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3428 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3429
3430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003431 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 ASSERT_EQ(0, motionArgs.buttonState);
3433 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3435 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3436
3437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003439 ASSERT_EQ(0, motionArgs.buttonState);
3440 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3442 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3443
3444 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003445 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3446 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3447 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3449 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3450 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3451 motionArgs.buttonState);
3452 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3453 mFakePointerController->getButtonState());
3454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3455 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3456
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3459 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3460 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3461 mFakePointerController->getButtonState());
3462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3463 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3464
3465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3466 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3467 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3468 motionArgs.buttonState);
3469 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3470 mFakePointerController->getButtonState());
3471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3472 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3473
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003474 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3475 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3479 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3481 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3482
3483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003485 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3486 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3488 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3489
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003490 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3491 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3494 ASSERT_EQ(0, motionArgs.buttonState);
3495 ASSERT_EQ(0, mFakePointerController->getButtonState());
3496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3497 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 -08003498 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3499 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003500
3501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003502 ASSERT_EQ(0, motionArgs.buttonState);
3503 ASSERT_EQ(0, mFakePointerController->getButtonState());
3504 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3506 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 -08003507
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3509 ASSERT_EQ(0, motionArgs.buttonState);
3510 ASSERT_EQ(0, mFakePointerController->getButtonState());
3511 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3513 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3514
3515 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003516 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3517 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3519 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3520 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003521
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3525 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3527 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3528
3529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3531 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3532 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3534 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3535
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003536 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 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(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003539 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540 ASSERT_EQ(0, motionArgs.buttonState);
3541 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3543 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3544
3545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003547 ASSERT_EQ(0, motionArgs.buttonState);
3548 ASSERT_EQ(0, mFakePointerController->getButtonState());
3549
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3551 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3553 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3554 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3555
3556 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003557 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3558 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3560 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3561 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003562
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3566 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3568 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3569
3570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3571 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3572 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3573 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3575 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3576
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003577 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3578 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003580 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003581 ASSERT_EQ(0, motionArgs.buttonState);
3582 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3584 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 -08003585
3586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3587 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3588 ASSERT_EQ(0, motionArgs.buttonState);
3589 ASSERT_EQ(0, mFakePointerController->getButtonState());
3590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3591 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3592
Michael Wrightd02c5b62014-02-10 15:10:22 -08003593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3594 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3595 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3596
3597 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003598 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3599 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3601 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3602 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003603
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003605 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3607 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3609 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3610
3611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3613 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3614 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3616 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3617
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003618 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3619 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003621 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622 ASSERT_EQ(0, motionArgs.buttonState);
3623 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3625 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 -08003626
3627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3628 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3629 ASSERT_EQ(0, motionArgs.buttonState);
3630 ASSERT_EQ(0, mFakePointerController->getButtonState());
3631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3632 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3633
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3635 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3636 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3637
3638 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003639 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3640 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3642 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3643 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003644
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003646 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3648 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3650 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3651
3652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3653 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3654 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3655 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3657 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3658
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003659 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3660 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003662 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003663 ASSERT_EQ(0, motionArgs.buttonState);
3664 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3666 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 -08003667
3668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3670 ASSERT_EQ(0, motionArgs.buttonState);
3671 ASSERT_EQ(0, mFakePointerController->getButtonState());
3672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3673 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3674
Michael Wrightd02c5b62014-02-10 15:10:22 -08003675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3676 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3677 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3678}
3679
3680TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003682 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683
3684 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3685 mFakePointerController->setPosition(100, 200);
3686 mFakePointerController->setButtonState(0);
3687
3688 NotifyMotionArgs args;
3689
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003690 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3691 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3692 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003694 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3695 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3697 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright7a376672020-06-26 20:51:44 +01003698 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003699}
3700
3701TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003702 addConfigurationProperty("cursor.mode", "pointer");
3703 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003704 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003705
3706 NotifyDeviceResetArgs resetArgs;
3707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3708 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3709 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3710
3711 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3712 mFakePointerController->setPosition(100, 200);
3713 mFakePointerController->setButtonState(0);
3714
3715 NotifyMotionArgs args;
3716
3717 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003718 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3719 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3720 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3722 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3725 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright7a376672020-06-26 20:51:44 +01003726 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003727
3728 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003729 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3730 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3732 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3733 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3735 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3737 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3740 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3741
3742 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003743 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3744 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3746 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3749 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3751 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3752 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3754 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3755
3756 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003757 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3758 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3759 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3761 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3764 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright7a376672020-06-26 20:51:44 +01003765 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003766
3767 // Disable pointer capture and check that the device generation got bumped
3768 // and events are generated the usual way.
3769 const uint32_t generation = mFakeContext->getGeneration();
3770 mFakePolicy->setPointerCapture(false);
3771 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3772 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3773
3774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3775 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3776 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3777
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003778 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3779 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3780 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3782 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3785 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright7a376672020-06-26 20:51:44 +01003786 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003787}
3788
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003789TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003790 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003791
Garfield Tan888a6a42020-01-09 11:39:16 -08003792 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003793 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003794 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3795 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3796 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3797 ViewportType::VIEWPORT_EXTERNAL);
3798 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3799 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3800
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003801 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3802 mFakePointerController->setPosition(100, 200);
3803 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003804
3805 NotifyMotionArgs args;
3806 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3807 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3808 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3810 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3811 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3813 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright7a376672020-06-26 20:51:44 +01003814 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003815 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3816}
3817
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818// --- TouchInputMapperTest ---
3819
3820class TouchInputMapperTest : public InputMapperTest {
3821protected:
3822 static const int32_t RAW_X_MIN;
3823 static const int32_t RAW_X_MAX;
3824 static const int32_t RAW_Y_MIN;
3825 static const int32_t RAW_Y_MAX;
3826 static const int32_t RAW_TOUCH_MIN;
3827 static const int32_t RAW_TOUCH_MAX;
3828 static const int32_t RAW_TOOL_MIN;
3829 static const int32_t RAW_TOOL_MAX;
3830 static const int32_t RAW_PRESSURE_MIN;
3831 static const int32_t RAW_PRESSURE_MAX;
3832 static const int32_t RAW_ORIENTATION_MIN;
3833 static const int32_t RAW_ORIENTATION_MAX;
3834 static const int32_t RAW_DISTANCE_MIN;
3835 static const int32_t RAW_DISTANCE_MAX;
3836 static const int32_t RAW_TILT_MIN;
3837 static const int32_t RAW_TILT_MAX;
3838 static const int32_t RAW_ID_MIN;
3839 static const int32_t RAW_ID_MAX;
3840 static const int32_t RAW_SLOT_MIN;
3841 static const int32_t RAW_SLOT_MAX;
3842 static const float X_PRECISION;
3843 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003844 static const float X_PRECISION_VIRTUAL;
3845 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846
3847 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003848 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003849
3850 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3851
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003852 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003853 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003854
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855 enum Axes {
3856 POSITION = 1 << 0,
3857 TOUCH = 1 << 1,
3858 TOOL = 1 << 2,
3859 PRESSURE = 1 << 3,
3860 ORIENTATION = 1 << 4,
3861 MINOR = 1 << 5,
3862 ID = 1 << 6,
3863 DISTANCE = 1 << 7,
3864 TILT = 1 << 8,
3865 SLOT = 1 << 9,
3866 TOOL_TYPE = 1 << 10,
3867 };
3868
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003869 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3870 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003871 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003873 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874 int32_t toRawX(float displayX);
3875 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003876 float toCookedX(float rawX, float rawY);
3877 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003879 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003881 float toDisplayY(int32_t rawY, int32_t displayHeight);
3882
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883};
3884
3885const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3886const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3887const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3888const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3889const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3890const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3891const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3892const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003893const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3894const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3896const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3897const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3898const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3899const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3900const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3901const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3902const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3903const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3904const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3905const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3906const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003907const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3908 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3909const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3910 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003911const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3912 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913
3914const float TouchInputMapperTest::GEOMETRIC_SCALE =
3915 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3916 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3917
3918const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3919 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3920 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3921};
3922
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003923void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003924 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003925 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3926}
3927
3928void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3929 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3930 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931}
3932
Santos Cordonfa5cf462017-04-05 10:37:00 -07003933void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003934 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3935 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003936 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003937}
3938
Michael Wrightd02c5b62014-02-10 15:10:22 -08003939void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003940 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3941 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3942 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3943 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944}
3945
Jason Gerecke489fda82012-09-07 17:19:40 -07003946void TouchInputMapperTest::prepareLocationCalibration() {
3947 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3948}
3949
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950int32_t TouchInputMapperTest::toRawX(float displayX) {
3951 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3952}
3953
3954int32_t TouchInputMapperTest::toRawY(float displayY) {
3955 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3956}
3957
Jason Gerecke489fda82012-09-07 17:19:40 -07003958float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3959 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3960 return rawX;
3961}
3962
3963float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3964 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3965 return rawY;
3966}
3967
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003969 return toDisplayX(rawX, DISPLAY_WIDTH);
3970}
3971
3972float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3973 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974}
3975
3976float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003977 return toDisplayY(rawY, DISPLAY_HEIGHT);
3978}
3979
3980float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3981 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982}
3983
3984
3985// --- SingleTouchInputMapperTest ---
3986
3987class SingleTouchInputMapperTest : public TouchInputMapperTest {
3988protected:
3989 void prepareButtons();
3990 void prepareAxes(int axes);
3991
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003992 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3993 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3994 void processUp(SingleTouchInputMapper& mappery);
3995 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3996 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3997 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3998 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3999 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4000 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001};
4002
4003void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004004 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005}
4006
4007void SingleTouchInputMapperTest::prepareAxes(int axes) {
4008 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004009 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4010 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 }
4012 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004013 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4014 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015 }
4016 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004017 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4018 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 }
4020 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004021 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4022 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 }
4024 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004025 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4026 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027 }
4028}
4029
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004030void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004031 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4032 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4033 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034}
4035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004036void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004037 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4038 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039}
4040
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004041void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004042 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043}
4044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004045void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004046 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047}
4048
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004049void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4050 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004051 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052}
4053
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004054void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004055 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056}
4057
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004058void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4059 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004060 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4061 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062}
4063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004064void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4065 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004066 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067}
4068
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004069void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004070 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071}
4072
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074 prepareButtons();
4075 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004076 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004078 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079}
4080
4081TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004082 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4083 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 prepareButtons();
4085 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004086 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004088 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089}
4090
4091TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092 prepareButtons();
4093 prepareAxes(POSITION);
4094 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004095 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004097 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098}
4099
4100TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101 prepareButtons();
4102 prepareAxes(POSITION);
4103 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004104 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004106 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107}
4108
4109TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 addConfigurationProperty("touch.deviceType", "touchScreen");
4111 prepareDisplay(DISPLAY_ORIENTATION_0);
4112 prepareButtons();
4113 prepareAxes(POSITION);
4114 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004115 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116
4117 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004118 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119
4120 // Virtual key is down.
4121 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4122 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4123 processDown(mapper, x, y);
4124 processSync(mapper);
4125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004127 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004128
4129 // Virtual key is up.
4130 processUp(mapper);
4131 processSync(mapper);
4132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004134 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135}
4136
4137TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 addConfigurationProperty("touch.deviceType", "touchScreen");
4139 prepareDisplay(DISPLAY_ORIENTATION_0);
4140 prepareButtons();
4141 prepareAxes(POSITION);
4142 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004143 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144
4145 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004146 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147
4148 // Virtual key is down.
4149 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4150 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4151 processDown(mapper, x, y);
4152 processSync(mapper);
4153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004155 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156
4157 // Virtual key is up.
4158 processUp(mapper);
4159 processSync(mapper);
4160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004162 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163}
4164
4165TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 addConfigurationProperty("touch.deviceType", "touchScreen");
4167 prepareDisplay(DISPLAY_ORIENTATION_0);
4168 prepareButtons();
4169 prepareAxes(POSITION);
4170 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004171 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172
4173 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4174 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004175 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176 ASSERT_TRUE(flags[0]);
4177 ASSERT_FALSE(flags[1]);
4178}
4179
4180TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 addConfigurationProperty("touch.deviceType", "touchScreen");
4182 prepareDisplay(DISPLAY_ORIENTATION_0);
4183 prepareButtons();
4184 prepareAxes(POSITION);
4185 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004186 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187
4188 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4189
4190 NotifyKeyArgs args;
4191
4192 // Press virtual key.
4193 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4194 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4195 processDown(mapper, x, y);
4196 processSync(mapper);
4197
4198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4199 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4200 ASSERT_EQ(DEVICE_ID, args.deviceId);
4201 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4202 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4203 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4204 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4205 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4206 ASSERT_EQ(KEY_HOME, args.scanCode);
4207 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4208 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4209
4210 // Release virtual key.
4211 processUp(mapper);
4212 processSync(mapper);
4213
4214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4215 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4216 ASSERT_EQ(DEVICE_ID, args.deviceId);
4217 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4218 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4219 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4220 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4221 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4222 ASSERT_EQ(KEY_HOME, args.scanCode);
4223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4224 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4225
4226 // Should not have sent any motions.
4227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4228}
4229
4230TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 addConfigurationProperty("touch.deviceType", "touchScreen");
4232 prepareDisplay(DISPLAY_ORIENTATION_0);
4233 prepareButtons();
4234 prepareAxes(POSITION);
4235 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004236 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237
4238 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4239
4240 NotifyKeyArgs keyArgs;
4241
4242 // Press virtual key.
4243 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4244 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4245 processDown(mapper, x, y);
4246 processSync(mapper);
4247
4248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4249 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4250 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4251 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4252 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4253 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4254 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4255 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4256 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4257 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4258 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4259
4260 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4261 // into the display area.
4262 y -= 100;
4263 processMove(mapper, x, y);
4264 processSync(mapper);
4265
4266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4267 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4268 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4269 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4270 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4271 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4272 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4273 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4274 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4275 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4277 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4278
4279 NotifyMotionArgs motionArgs;
4280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4281 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4282 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4283 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4284 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4285 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4286 ASSERT_EQ(0, motionArgs.flags);
4287 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4288 ASSERT_EQ(0, motionArgs.buttonState);
4289 ASSERT_EQ(0, motionArgs.edgeFlags);
4290 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4291 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4292 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4294 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4295 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4296 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4297 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4298
4299 // Keep moving out of bounds. Should generate a pointer move.
4300 y -= 50;
4301 processMove(mapper, x, y);
4302 processSync(mapper);
4303
4304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4305 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4306 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4307 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4308 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4310 ASSERT_EQ(0, motionArgs.flags);
4311 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4312 ASSERT_EQ(0, motionArgs.buttonState);
4313 ASSERT_EQ(0, motionArgs.edgeFlags);
4314 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4315 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4318 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4319 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4320 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4322
4323 // Release out of bounds. Should generate a pointer up.
4324 processUp(mapper);
4325 processSync(mapper);
4326
4327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4328 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4329 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4330 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4331 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4332 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4333 ASSERT_EQ(0, motionArgs.flags);
4334 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4335 ASSERT_EQ(0, motionArgs.buttonState);
4336 ASSERT_EQ(0, motionArgs.edgeFlags);
4337 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4338 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4341 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4342 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4343 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4344 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4345
4346 // Should not have sent any more keys or motions.
4347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4349}
4350
4351TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 addConfigurationProperty("touch.deviceType", "touchScreen");
4353 prepareDisplay(DISPLAY_ORIENTATION_0);
4354 prepareButtons();
4355 prepareAxes(POSITION);
4356 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004357 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358
4359 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4360
4361 NotifyMotionArgs motionArgs;
4362
4363 // Initially go down out of bounds.
4364 int32_t x = -10;
4365 int32_t y = -10;
4366 processDown(mapper, x, y);
4367 processSync(mapper);
4368
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4370
4371 // Move into the display area. Should generate a pointer down.
4372 x = 50;
4373 y = 75;
4374 processMove(mapper, x, y);
4375 processSync(mapper);
4376
4377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4379 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4380 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4381 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4383 ASSERT_EQ(0, motionArgs.flags);
4384 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4385 ASSERT_EQ(0, motionArgs.buttonState);
4386 ASSERT_EQ(0, motionArgs.edgeFlags);
4387 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4388 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4391 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4392 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4393 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4395
4396 // Release. Should generate a pointer up.
4397 processUp(mapper);
4398 processSync(mapper);
4399
4400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4401 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4402 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4403 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4404 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4405 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4406 ASSERT_EQ(0, motionArgs.flags);
4407 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4408 ASSERT_EQ(0, motionArgs.buttonState);
4409 ASSERT_EQ(0, motionArgs.edgeFlags);
4410 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4411 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4414 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4415 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4416 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4417 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4418
4419 // Should not have sent any more keys or motions.
4420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4422}
4423
Santos Cordonfa5cf462017-04-05 10:37:00 -07004424TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004425 addConfigurationProperty("touch.deviceType", "touchScreen");
4426 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4427
4428 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4429 prepareButtons();
4430 prepareAxes(POSITION);
4431 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004432 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004433
4434 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4435
4436 NotifyMotionArgs motionArgs;
4437
4438 // Down.
4439 int32_t x = 100;
4440 int32_t y = 125;
4441 processDown(mapper, x, y);
4442 processSync(mapper);
4443
4444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4445 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4446 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4447 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4448 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4449 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4451 ASSERT_EQ(0, motionArgs.flags);
4452 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4453 ASSERT_EQ(0, motionArgs.buttonState);
4454 ASSERT_EQ(0, motionArgs.edgeFlags);
4455 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4456 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4459 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4460 1, 0, 0, 0, 0, 0, 0, 0));
4461 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4462 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4463 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4464
4465 // Move.
4466 x += 50;
4467 y += 75;
4468 processMove(mapper, x, y);
4469 processSync(mapper);
4470
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4472 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4473 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4474 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4475 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4476 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4478 ASSERT_EQ(0, motionArgs.flags);
4479 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4480 ASSERT_EQ(0, motionArgs.buttonState);
4481 ASSERT_EQ(0, motionArgs.edgeFlags);
4482 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4486 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4487 1, 0, 0, 0, 0, 0, 0, 0));
4488 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4489 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4490 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4491
4492 // Up.
4493 processUp(mapper);
4494 processSync(mapper);
4495
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4497 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4498 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4499 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4500 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4501 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4502 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4503 ASSERT_EQ(0, motionArgs.flags);
4504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4505 ASSERT_EQ(0, motionArgs.buttonState);
4506 ASSERT_EQ(0, motionArgs.edgeFlags);
4507 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4511 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4512 1, 0, 0, 0, 0, 0, 0, 0));
4513 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4514 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4515 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4516
4517 // Should not have sent any more keys or motions.
4518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4520}
4521
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 addConfigurationProperty("touch.deviceType", "touchScreen");
4524 prepareDisplay(DISPLAY_ORIENTATION_0);
4525 prepareButtons();
4526 prepareAxes(POSITION);
4527 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004528 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529
4530 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4531
4532 NotifyMotionArgs motionArgs;
4533
4534 // Down.
4535 int32_t x = 100;
4536 int32_t y = 125;
4537 processDown(mapper, x, y);
4538 processSync(mapper);
4539
4540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4541 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4542 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4543 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4544 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4546 ASSERT_EQ(0, motionArgs.flags);
4547 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4548 ASSERT_EQ(0, motionArgs.buttonState);
4549 ASSERT_EQ(0, motionArgs.edgeFlags);
4550 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4551 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4554 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4555 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4556 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4557 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4558
4559 // Move.
4560 x += 50;
4561 y += 75;
4562 processMove(mapper, x, y);
4563 processSync(mapper);
4564
4565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4566 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4567 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4568 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4569 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4571 ASSERT_EQ(0, motionArgs.flags);
4572 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4573 ASSERT_EQ(0, motionArgs.buttonState);
4574 ASSERT_EQ(0, motionArgs.edgeFlags);
4575 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4576 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4579 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4580 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4581 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4582 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4583
4584 // Up.
4585 processUp(mapper);
4586 processSync(mapper);
4587
4588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4589 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4590 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4591 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4592 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4593 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4594 ASSERT_EQ(0, motionArgs.flags);
4595 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4596 ASSERT_EQ(0, motionArgs.buttonState);
4597 ASSERT_EQ(0, motionArgs.edgeFlags);
4598 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4599 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4602 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4603 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4604 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4605 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4606
4607 // Should not have sent any more keys or motions.
4608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4610}
4611
4612TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 addConfigurationProperty("touch.deviceType", "touchScreen");
4614 prepareButtons();
4615 prepareAxes(POSITION);
4616 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004617 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618
4619 NotifyMotionArgs args;
4620
4621 // Rotation 90.
4622 prepareDisplay(DISPLAY_ORIENTATION_90);
4623 processDown(mapper, toRawX(50), toRawY(75));
4624 processSync(mapper);
4625
4626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4627 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4628 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4629
4630 processUp(mapper);
4631 processSync(mapper);
4632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4633}
4634
4635TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 addConfigurationProperty("touch.deviceType", "touchScreen");
4637 prepareButtons();
4638 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004639 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640
4641 NotifyMotionArgs args;
4642
4643 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004644 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 prepareDisplay(DISPLAY_ORIENTATION_0);
4646 processDown(mapper, toRawX(50), toRawY(75));
4647 processSync(mapper);
4648
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4650 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4651 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4652
4653 processUp(mapper);
4654 processSync(mapper);
4655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4656
4657 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004658 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659 prepareDisplay(DISPLAY_ORIENTATION_90);
4660 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4661 processSync(mapper);
4662
4663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4664 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4665 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4666
4667 processUp(mapper);
4668 processSync(mapper);
4669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4670
4671 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004672 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 prepareDisplay(DISPLAY_ORIENTATION_180);
4674 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4675 processSync(mapper);
4676
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4678 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4679 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4680
4681 processUp(mapper);
4682 processSync(mapper);
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4684
4685 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004686 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 prepareDisplay(DISPLAY_ORIENTATION_270);
4688 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4689 processSync(mapper);
4690
4691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4692 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4693 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4694
4695 processUp(mapper);
4696 processSync(mapper);
4697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4698}
4699
4700TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701 addConfigurationProperty("touch.deviceType", "touchScreen");
4702 prepareDisplay(DISPLAY_ORIENTATION_0);
4703 prepareButtons();
4704 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004705 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706
4707 // These calculations are based on the input device calibration documentation.
4708 int32_t rawX = 100;
4709 int32_t rawY = 200;
4710 int32_t rawPressure = 10;
4711 int32_t rawToolMajor = 12;
4712 int32_t rawDistance = 2;
4713 int32_t rawTiltX = 30;
4714 int32_t rawTiltY = 110;
4715
4716 float x = toDisplayX(rawX);
4717 float y = toDisplayY(rawY);
4718 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4719 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4720 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4721 float distance = float(rawDistance);
4722
4723 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4724 float tiltScale = M_PI / 180;
4725 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4726 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4727 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4728 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4729
4730 processDown(mapper, rawX, rawY);
4731 processPressure(mapper, rawPressure);
4732 processToolMajor(mapper, rawToolMajor);
4733 processDistance(mapper, rawDistance);
4734 processTilt(mapper, rawTiltX, rawTiltY);
4735 processSync(mapper);
4736
4737 NotifyMotionArgs args;
4738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4740 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4741 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4742}
4743
Jason Gerecke489fda82012-09-07 17:19:40 -07004744TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004745 addConfigurationProperty("touch.deviceType", "touchScreen");
4746 prepareDisplay(DISPLAY_ORIENTATION_0);
4747 prepareLocationCalibration();
4748 prepareButtons();
4749 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004750 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004751
4752 int32_t rawX = 100;
4753 int32_t rawY = 200;
4754
4755 float x = toDisplayX(toCookedX(rawX, rawY));
4756 float y = toDisplayY(toCookedY(rawX, rawY));
4757
4758 processDown(mapper, rawX, rawY);
4759 processSync(mapper);
4760
4761 NotifyMotionArgs args;
4762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4764 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4765}
4766
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768 addConfigurationProperty("touch.deviceType", "touchScreen");
4769 prepareDisplay(DISPLAY_ORIENTATION_0);
4770 prepareButtons();
4771 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004772 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773
4774 NotifyMotionArgs motionArgs;
4775 NotifyKeyArgs keyArgs;
4776
4777 processDown(mapper, 100, 200);
4778 processSync(mapper);
4779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4780 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4781 ASSERT_EQ(0, motionArgs.buttonState);
4782
4783 // press BTN_LEFT, release BTN_LEFT
4784 processKey(mapper, BTN_LEFT, 1);
4785 processSync(mapper);
4786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4788 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4789
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4791 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4792 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4793
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794 processKey(mapper, BTN_LEFT, 0);
4795 processSync(mapper);
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004797 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004799
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803
4804 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4805 processKey(mapper, BTN_RIGHT, 1);
4806 processKey(mapper, BTN_MIDDLE, 1);
4807 processSync(mapper);
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4810 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4811 motionArgs.buttonState);
4812
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4814 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4815 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4816
4817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4818 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4819 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4820 motionArgs.buttonState);
4821
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 processKey(mapper, BTN_RIGHT, 0);
4823 processSync(mapper);
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004825 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004827
4828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004830 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831
4832 processKey(mapper, BTN_MIDDLE, 0);
4833 processSync(mapper);
4834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004837
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004840 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841
4842 // press BTN_BACK, release BTN_BACK
4843 processKey(mapper, BTN_BACK, 1);
4844 processSync(mapper);
4845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4846 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4847 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004848
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004851 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4852
4853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4854 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4855 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856
4857 processKey(mapper, BTN_BACK, 0);
4858 processSync(mapper);
4859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004860 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004862
4863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004865 ASSERT_EQ(0, motionArgs.buttonState);
4866
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4868 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4869 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4870
4871 // press BTN_SIDE, release BTN_SIDE
4872 processKey(mapper, BTN_SIDE, 1);
4873 processSync(mapper);
4874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4875 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4876 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004877
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004880 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4881
4882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4883 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4884 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885
4886 processKey(mapper, BTN_SIDE, 0);
4887 processSync(mapper);
4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004891
4892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004894 ASSERT_EQ(0, motionArgs.buttonState);
4895
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4897 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4898 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4899
4900 // press BTN_FORWARD, release BTN_FORWARD
4901 processKey(mapper, BTN_FORWARD, 1);
4902 processSync(mapper);
4903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4904 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4905 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004906
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004909 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4910
4911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4913 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914
4915 processKey(mapper, BTN_FORWARD, 0);
4916 processSync(mapper);
4917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004918 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004920
4921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004923 ASSERT_EQ(0, motionArgs.buttonState);
4924
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4926 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4927 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4928
4929 // press BTN_EXTRA, release BTN_EXTRA
4930 processKey(mapper, BTN_EXTRA, 1);
4931 processSync(mapper);
4932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4933 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4934 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004935
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004938 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4939
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4942 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943
4944 processKey(mapper, BTN_EXTRA, 0);
4945 processSync(mapper);
4946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004949
4950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004952 ASSERT_EQ(0, motionArgs.buttonState);
4953
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4955 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4956 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4957
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4959
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960 // press BTN_STYLUS, release BTN_STYLUS
4961 processKey(mapper, BTN_STYLUS, 1);
4962 processSync(mapper);
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004965 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4966
4967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4969 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970
4971 processKey(mapper, BTN_STYLUS, 0);
4972 processSync(mapper);
4973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004974 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004976
4977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004979 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980
4981 // press BTN_STYLUS2, release BTN_STYLUS2
4982 processKey(mapper, BTN_STYLUS2, 1);
4983 processSync(mapper);
4984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004986 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4987
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4990 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991
4992 processKey(mapper, BTN_STYLUS2, 0);
4993 processSync(mapper);
4994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004997
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005000 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001
5002 // release touch
5003 processUp(mapper);
5004 processSync(mapper);
5005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5006 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5007 ASSERT_EQ(0, motionArgs.buttonState);
5008}
5009
5010TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011 addConfigurationProperty("touch.deviceType", "touchScreen");
5012 prepareDisplay(DISPLAY_ORIENTATION_0);
5013 prepareButtons();
5014 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005015 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016
5017 NotifyMotionArgs motionArgs;
5018
5019 // default tool type is finger
5020 processDown(mapper, 100, 200);
5021 processSync(mapper);
5022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5023 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5024 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5025
5026 // eraser
5027 processKey(mapper, BTN_TOOL_RUBBER, 1);
5028 processSync(mapper);
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5032
5033 // stylus
5034 processKey(mapper, BTN_TOOL_RUBBER, 0);
5035 processKey(mapper, BTN_TOOL_PEN, 1);
5036 processSync(mapper);
5037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5039 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5040
5041 // brush
5042 processKey(mapper, BTN_TOOL_PEN, 0);
5043 processKey(mapper, BTN_TOOL_BRUSH, 1);
5044 processSync(mapper);
5045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5048
5049 // pencil
5050 processKey(mapper, BTN_TOOL_BRUSH, 0);
5051 processKey(mapper, BTN_TOOL_PENCIL, 1);
5052 processSync(mapper);
5053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5056
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005057 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058 processKey(mapper, BTN_TOOL_PENCIL, 0);
5059 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5060 processSync(mapper);
5061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5064
5065 // mouse
5066 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5067 processKey(mapper, BTN_TOOL_MOUSE, 1);
5068 processSync(mapper);
5069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5072
5073 // lens
5074 processKey(mapper, BTN_TOOL_MOUSE, 0);
5075 processKey(mapper, BTN_TOOL_LENS, 1);
5076 processSync(mapper);
5077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5080
5081 // double-tap
5082 processKey(mapper, BTN_TOOL_LENS, 0);
5083 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5084 processSync(mapper);
5085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5088
5089 // triple-tap
5090 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5091 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5092 processSync(mapper);
5093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5095 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5096
5097 // quad-tap
5098 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5099 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5100 processSync(mapper);
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5104
5105 // finger
5106 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5107 processKey(mapper, BTN_TOOL_FINGER, 1);
5108 processSync(mapper);
5109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5112
5113 // stylus trumps finger
5114 processKey(mapper, BTN_TOOL_PEN, 1);
5115 processSync(mapper);
5116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5119
5120 // eraser trumps stylus
5121 processKey(mapper, BTN_TOOL_RUBBER, 1);
5122 processSync(mapper);
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5126
5127 // mouse trumps eraser
5128 processKey(mapper, BTN_TOOL_MOUSE, 1);
5129 processSync(mapper);
5130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5133
5134 // back to default tool type
5135 processKey(mapper, BTN_TOOL_MOUSE, 0);
5136 processKey(mapper, BTN_TOOL_RUBBER, 0);
5137 processKey(mapper, BTN_TOOL_PEN, 0);
5138 processKey(mapper, BTN_TOOL_FINGER, 0);
5139 processSync(mapper);
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5143}
5144
5145TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146 addConfigurationProperty("touch.deviceType", "touchScreen");
5147 prepareDisplay(DISPLAY_ORIENTATION_0);
5148 prepareButtons();
5149 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005150 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005151 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152
5153 NotifyMotionArgs motionArgs;
5154
5155 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5156 processKey(mapper, BTN_TOOL_FINGER, 1);
5157 processMove(mapper, 100, 200);
5158 processSync(mapper);
5159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5160 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5162 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5163
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5167 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5168
5169 // move a little
5170 processMove(mapper, 150, 250);
5171 processSync(mapper);
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5173 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5175 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5176
5177 // down when BTN_TOUCH is pressed, pressure defaults to 1
5178 processKey(mapper, BTN_TOUCH, 1);
5179 processSync(mapper);
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5181 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5183 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5184
5185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5186 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5188 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5189
5190 // up when BTN_TOUCH is released, hover restored
5191 processKey(mapper, BTN_TOUCH, 0);
5192 processSync(mapper);
5193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5194 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5196 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5197
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5199 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5201 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5202
5203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5204 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5206 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5207
5208 // exit hover when pointer goes away
5209 processKey(mapper, BTN_TOOL_FINGER, 0);
5210 processSync(mapper);
5211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5212 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5214 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5215}
5216
5217TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218 addConfigurationProperty("touch.deviceType", "touchScreen");
5219 prepareDisplay(DISPLAY_ORIENTATION_0);
5220 prepareButtons();
5221 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005222 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223
5224 NotifyMotionArgs motionArgs;
5225
5226 // initially hovering because pressure is 0
5227 processDown(mapper, 100, 200);
5228 processPressure(mapper, 0);
5229 processSync(mapper);
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5231 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5233 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5234
5235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5238 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5239
5240 // move a little
5241 processMove(mapper, 150, 250);
5242 processSync(mapper);
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5244 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5246 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5247
5248 // down when pressure is non-zero
5249 processPressure(mapper, RAW_PRESSURE_MAX);
5250 processSync(mapper);
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5254 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5255
5256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5257 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5259 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5260
5261 // up when pressure becomes 0, hover restored
5262 processPressure(mapper, 0);
5263 processSync(mapper);
5264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5265 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5267 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5268
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5270 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5272 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5273
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5275 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5277 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5278
5279 // exit hover when pointer goes away
5280 processUp(mapper);
5281 processSync(mapper);
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5283 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5285 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5286}
5287
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288// --- MultiTouchInputMapperTest ---
5289
5290class MultiTouchInputMapperTest : public TouchInputMapperTest {
5291protected:
5292 void prepareAxes(int axes);
5293
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005294 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5295 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5296 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5297 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5298 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5299 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5300 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5301 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5302 void processId(MultiTouchInputMapper& mapper, int32_t id);
5303 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5304 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5305 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5306 void processMTSync(MultiTouchInputMapper& mapper);
5307 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308};
5309
5310void MultiTouchInputMapperTest::prepareAxes(int axes) {
5311 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005312 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5313 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314 }
5315 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005316 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5317 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005319 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5320 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321 }
5322 }
5323 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005324 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5325 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005327 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5328 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005329 }
5330 }
5331 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005332 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5333 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334 }
5335 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005336 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5337 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338 }
5339 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005340 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5341 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342 }
5343 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005344 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5345 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346 }
5347 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005348 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5349 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350 }
5351 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005352 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353 }
5354}
5355
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005356void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5357 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005358 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5359 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360}
5361
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005362void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5363 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005364 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365}
5366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005367void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5368 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005369 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370}
5371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005372void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005373 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374}
5375
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005376void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005377 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005378}
5379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005380void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5381 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005382 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383}
5384
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005385void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005386 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387}
5388
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005389void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005390 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391}
5392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005393void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005394 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395}
5396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005397void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005398 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399}
5400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005401void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005402 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403}
5404
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005405void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5406 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005407 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408}
5409
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005410void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005411 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412}
5413
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005414void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005415 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416}
5417
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419 addConfigurationProperty("touch.deviceType", "touchScreen");
5420 prepareDisplay(DISPLAY_ORIENTATION_0);
5421 prepareAxes(POSITION);
5422 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005423 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424
5425 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5426
5427 NotifyMotionArgs motionArgs;
5428
5429 // Two fingers down at once.
5430 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5431 processPosition(mapper, x1, y1);
5432 processMTSync(mapper);
5433 processPosition(mapper, x2, y2);
5434 processMTSync(mapper);
5435 processSync(mapper);
5436
5437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5438 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5439 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5440 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5441 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5442 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5443 ASSERT_EQ(0, motionArgs.flags);
5444 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5445 ASSERT_EQ(0, motionArgs.buttonState);
5446 ASSERT_EQ(0, motionArgs.edgeFlags);
5447 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5448 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5451 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5452 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5453 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5454 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5455
5456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5458 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5459 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5460 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5461 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5462 motionArgs.action);
5463 ASSERT_EQ(0, motionArgs.flags);
5464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5465 ASSERT_EQ(0, motionArgs.buttonState);
5466 ASSERT_EQ(0, motionArgs.edgeFlags);
5467 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5468 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5470 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5473 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5475 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5476 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5477 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5478 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5479
5480 // Move.
5481 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5482 processPosition(mapper, x1, y1);
5483 processMTSync(mapper);
5484 processPosition(mapper, x2, y2);
5485 processMTSync(mapper);
5486 processSync(mapper);
5487
5488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5489 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5490 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5491 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5492 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5494 ASSERT_EQ(0, motionArgs.flags);
5495 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5496 ASSERT_EQ(0, motionArgs.buttonState);
5497 ASSERT_EQ(0, motionArgs.edgeFlags);
5498 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5499 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5501 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5504 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5506 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5507 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5508 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5509 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5510
5511 // First finger up.
5512 x2 += 15; y2 -= 20;
5513 processPosition(mapper, x2, y2);
5514 processMTSync(mapper);
5515 processSync(mapper);
5516
5517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5518 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5519 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5520 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5521 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5522 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5523 motionArgs.action);
5524 ASSERT_EQ(0, motionArgs.flags);
5525 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5526 ASSERT_EQ(0, motionArgs.buttonState);
5527 ASSERT_EQ(0, motionArgs.edgeFlags);
5528 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5529 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5531 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5534 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5536 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5537 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5538 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5539 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5540
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5542 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5543 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5544 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5545 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5547 ASSERT_EQ(0, motionArgs.flags);
5548 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5549 ASSERT_EQ(0, motionArgs.buttonState);
5550 ASSERT_EQ(0, motionArgs.edgeFlags);
5551 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5552 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5555 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5556 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5557 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5558 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5559
5560 // Move.
5561 x2 += 20; y2 -= 25;
5562 processPosition(mapper, x2, y2);
5563 processMTSync(mapper);
5564 processSync(mapper);
5565
5566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5567 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5568 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5569 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5570 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5572 ASSERT_EQ(0, motionArgs.flags);
5573 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5574 ASSERT_EQ(0, motionArgs.buttonState);
5575 ASSERT_EQ(0, motionArgs.edgeFlags);
5576 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5577 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5580 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5581 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5582 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5583 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5584
5585 // New finger down.
5586 int32_t x3 = 700, y3 = 300;
5587 processPosition(mapper, x2, y2);
5588 processMTSync(mapper);
5589 processPosition(mapper, x3, y3);
5590 processMTSync(mapper);
5591 processSync(mapper);
5592
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5594 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5595 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5596 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5597 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5598 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5599 motionArgs.action);
5600 ASSERT_EQ(0, motionArgs.flags);
5601 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5602 ASSERT_EQ(0, motionArgs.buttonState);
5603 ASSERT_EQ(0, motionArgs.edgeFlags);
5604 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5605 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5607 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5610 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5612 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5613 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5614 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5615 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5616
5617 // Second finger up.
5618 x3 += 30; y3 -= 20;
5619 processPosition(mapper, x3, y3);
5620 processMTSync(mapper);
5621 processSync(mapper);
5622
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5624 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5625 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5626 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5627 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5628 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5629 motionArgs.action);
5630 ASSERT_EQ(0, motionArgs.flags);
5631 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5632 ASSERT_EQ(0, motionArgs.buttonState);
5633 ASSERT_EQ(0, motionArgs.edgeFlags);
5634 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5637 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5640 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5642 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5643 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5644 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5645 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5646
5647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5648 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5649 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5650 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5651 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5653 ASSERT_EQ(0, motionArgs.flags);
5654 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5655 ASSERT_EQ(0, motionArgs.buttonState);
5656 ASSERT_EQ(0, motionArgs.edgeFlags);
5657 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5658 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5661 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5662 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5663 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5664 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5665
5666 // Last finger up.
5667 processMTSync(mapper);
5668 processSync(mapper);
5669
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5671 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5672 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5673 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5674 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5675 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5676 ASSERT_EQ(0, motionArgs.flags);
5677 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5678 ASSERT_EQ(0, motionArgs.buttonState);
5679 ASSERT_EQ(0, motionArgs.edgeFlags);
5680 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5681 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5684 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5685 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5686 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5687 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5688
5689 // Should not have sent any more keys or motions.
5690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5692}
5693
5694TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695 addConfigurationProperty("touch.deviceType", "touchScreen");
5696 prepareDisplay(DISPLAY_ORIENTATION_0);
5697 prepareAxes(POSITION | ID);
5698 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005699 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700
5701 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5702
5703 NotifyMotionArgs motionArgs;
5704
5705 // Two fingers down at once.
5706 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5707 processPosition(mapper, x1, y1);
5708 processId(mapper, 1);
5709 processMTSync(mapper);
5710 processPosition(mapper, x2, y2);
5711 processId(mapper, 2);
5712 processMTSync(mapper);
5713 processSync(mapper);
5714
5715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5716 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5717 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5721 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5722
5723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5724 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5725 motionArgs.action);
5726 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5727 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5729 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5732 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5734 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5735
5736 // Move.
5737 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5738 processPosition(mapper, x1, y1);
5739 processId(mapper, 1);
5740 processMTSync(mapper);
5741 processPosition(mapper, x2, y2);
5742 processId(mapper, 2);
5743 processMTSync(mapper);
5744 processSync(mapper);
5745
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5748 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5749 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5751 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5754 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5756 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5757
5758 // First finger up.
5759 x2 += 15; y2 -= 20;
5760 processPosition(mapper, x2, y2);
5761 processId(mapper, 2);
5762 processMTSync(mapper);
5763 processSync(mapper);
5764
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5766 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5767 motionArgs.action);
5768 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5769 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5770 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5771 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5774 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5776 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5777
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5780 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5781 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5784 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5785
5786 // Move.
5787 x2 += 20; y2 -= 25;
5788 processPosition(mapper, x2, y2);
5789 processId(mapper, 2);
5790 processMTSync(mapper);
5791 processSync(mapper);
5792
5793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5795 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5796 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5799 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5800
5801 // New finger down.
5802 int32_t x3 = 700, y3 = 300;
5803 processPosition(mapper, x2, y2);
5804 processId(mapper, 2);
5805 processMTSync(mapper);
5806 processPosition(mapper, x3, y3);
5807 processId(mapper, 3);
5808 processMTSync(mapper);
5809 processSync(mapper);
5810
5811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5812 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5813 motionArgs.action);
5814 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5815 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5817 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5820 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5822 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5823
5824 // Second finger up.
5825 x3 += 30; y3 -= 20;
5826 processPosition(mapper, x3, y3);
5827 processId(mapper, 3);
5828 processMTSync(mapper);
5829 processSync(mapper);
5830
5831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5832 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5833 motionArgs.action);
5834 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5835 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5836 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5837 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5840 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5842 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5843
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5846 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5847 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5850 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5851
5852 // Last finger up.
5853 processMTSync(mapper);
5854 processSync(mapper);
5855
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5858 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5859 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5860 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5862 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5863
5864 // Should not have sent any more keys or motions.
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5867}
5868
5869TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 addConfigurationProperty("touch.deviceType", "touchScreen");
5871 prepareDisplay(DISPLAY_ORIENTATION_0);
5872 prepareAxes(POSITION | ID | SLOT);
5873 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005874 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875
5876 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5877
5878 NotifyMotionArgs motionArgs;
5879
5880 // Two fingers down at once.
5881 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5882 processPosition(mapper, x1, y1);
5883 processId(mapper, 1);
5884 processSlot(mapper, 1);
5885 processPosition(mapper, x2, y2);
5886 processId(mapper, 2);
5887 processSync(mapper);
5888
5889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5890 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5891 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5892 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5895 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5896
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5898 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5899 motionArgs.action);
5900 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5901 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5903 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5906 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5908 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5909
5910 // Move.
5911 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5912 processSlot(mapper, 0);
5913 processPosition(mapper, x1, y1);
5914 processSlot(mapper, 1);
5915 processPosition(mapper, x2, y2);
5916 processSync(mapper);
5917
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5920 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5923 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5926 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5928 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5929
5930 // First finger up.
5931 x2 += 15; y2 -= 20;
5932 processSlot(mapper, 0);
5933 processId(mapper, -1);
5934 processSlot(mapper, 1);
5935 processPosition(mapper, x2, y2);
5936 processSync(mapper);
5937
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5940 motionArgs.action);
5941 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5942 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5944 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5947 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5949 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5950
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5953 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5954 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5955 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5957 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5958
5959 // Move.
5960 x2 += 20; y2 -= 25;
5961 processPosition(mapper, x2, y2);
5962 processSync(mapper);
5963
5964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5966 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5967 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5968 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5970 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5971
5972 // New finger down.
5973 int32_t x3 = 700, y3 = 300;
5974 processPosition(mapper, x2, y2);
5975 processSlot(mapper, 0);
5976 processId(mapper, 3);
5977 processPosition(mapper, x3, y3);
5978 processSync(mapper);
5979
5980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5981 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5982 motionArgs.action);
5983 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5984 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5985 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5986 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5987 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5989 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5991 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5992
5993 // Second finger up.
5994 x3 += 30; y3 -= 20;
5995 processSlot(mapper, 1);
5996 processId(mapper, -1);
5997 processSlot(mapper, 0);
5998 processPosition(mapper, x3, y3);
5999 processSync(mapper);
6000
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6003 motionArgs.action);
6004 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6005 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6006 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6007 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6010 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6012 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6013
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6016 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6017 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6020 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6021
6022 // Last finger up.
6023 processId(mapper, -1);
6024 processSync(mapper);
6025
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6028 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6029 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6032 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6033
6034 // Should not have sent any more keys or motions.
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6037}
6038
6039TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040 addConfigurationProperty("touch.deviceType", "touchScreen");
6041 prepareDisplay(DISPLAY_ORIENTATION_0);
6042 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006043 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044
6045 // These calculations are based on the input device calibration documentation.
6046 int32_t rawX = 100;
6047 int32_t rawY = 200;
6048 int32_t rawTouchMajor = 7;
6049 int32_t rawTouchMinor = 6;
6050 int32_t rawToolMajor = 9;
6051 int32_t rawToolMinor = 8;
6052 int32_t rawPressure = 11;
6053 int32_t rawDistance = 0;
6054 int32_t rawOrientation = 3;
6055 int32_t id = 5;
6056
6057 float x = toDisplayX(rawX);
6058 float y = toDisplayY(rawY);
6059 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6060 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6061 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6062 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6063 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6064 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6065 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6066 float distance = float(rawDistance);
6067
6068 processPosition(mapper, rawX, rawY);
6069 processTouchMajor(mapper, rawTouchMajor);
6070 processTouchMinor(mapper, rawTouchMinor);
6071 processToolMajor(mapper, rawToolMajor);
6072 processToolMinor(mapper, rawToolMinor);
6073 processPressure(mapper, rawPressure);
6074 processOrientation(mapper, rawOrientation);
6075 processDistance(mapper, rawDistance);
6076 processId(mapper, id);
6077 processMTSync(mapper);
6078 processSync(mapper);
6079
6080 NotifyMotionArgs args;
6081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6082 ASSERT_EQ(0, args.pointerProperties[0].id);
6083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6084 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6085 orientation, distance));
6086}
6087
6088TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006089 addConfigurationProperty("touch.deviceType", "touchScreen");
6090 prepareDisplay(DISPLAY_ORIENTATION_0);
6091 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6092 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006093 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094
6095 // These calculations are based on the input device calibration documentation.
6096 int32_t rawX = 100;
6097 int32_t rawY = 200;
6098 int32_t rawTouchMajor = 140;
6099 int32_t rawTouchMinor = 120;
6100 int32_t rawToolMajor = 180;
6101 int32_t rawToolMinor = 160;
6102
6103 float x = toDisplayX(rawX);
6104 float y = toDisplayY(rawY);
6105 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6106 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6107 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6108 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6109 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6110
6111 processPosition(mapper, rawX, rawY);
6112 processTouchMajor(mapper, rawTouchMajor);
6113 processTouchMinor(mapper, rawTouchMinor);
6114 processToolMajor(mapper, rawToolMajor);
6115 processToolMinor(mapper, rawToolMinor);
6116 processMTSync(mapper);
6117 processSync(mapper);
6118
6119 NotifyMotionArgs args;
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6122 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6123}
6124
6125TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 addConfigurationProperty("touch.deviceType", "touchScreen");
6127 prepareDisplay(DISPLAY_ORIENTATION_0);
6128 prepareAxes(POSITION | TOUCH | TOOL);
6129 addConfigurationProperty("touch.size.calibration", "diameter");
6130 addConfigurationProperty("touch.size.scale", "10");
6131 addConfigurationProperty("touch.size.bias", "160");
6132 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006133 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134
6135 // These calculations are based on the input device calibration documentation.
6136 // Note: We only provide a single common touch/tool value because the device is assumed
6137 // not to emit separate values for each pointer (isSummed = 1).
6138 int32_t rawX = 100;
6139 int32_t rawY = 200;
6140 int32_t rawX2 = 150;
6141 int32_t rawY2 = 250;
6142 int32_t rawTouchMajor = 5;
6143 int32_t rawToolMajor = 8;
6144
6145 float x = toDisplayX(rawX);
6146 float y = toDisplayY(rawY);
6147 float x2 = toDisplayX(rawX2);
6148 float y2 = toDisplayY(rawY2);
6149 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6150 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6151 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6152
6153 processPosition(mapper, rawX, rawY);
6154 processTouchMajor(mapper, rawTouchMajor);
6155 processToolMajor(mapper, rawToolMajor);
6156 processMTSync(mapper);
6157 processPosition(mapper, rawX2, rawY2);
6158 processTouchMajor(mapper, rawTouchMajor);
6159 processToolMajor(mapper, rawToolMajor);
6160 processMTSync(mapper);
6161 processSync(mapper);
6162
6163 NotifyMotionArgs args;
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6165 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6166
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6168 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6169 args.action);
6170 ASSERT_EQ(size_t(2), args.pointerCount);
6171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6172 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6174 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6175}
6176
6177TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 addConfigurationProperty("touch.deviceType", "touchScreen");
6179 prepareDisplay(DISPLAY_ORIENTATION_0);
6180 prepareAxes(POSITION | TOUCH | TOOL);
6181 addConfigurationProperty("touch.size.calibration", "area");
6182 addConfigurationProperty("touch.size.scale", "43");
6183 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006184 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185
6186 // These calculations are based on the input device calibration documentation.
6187 int32_t rawX = 100;
6188 int32_t rawY = 200;
6189 int32_t rawTouchMajor = 5;
6190 int32_t rawToolMajor = 8;
6191
6192 float x = toDisplayX(rawX);
6193 float y = toDisplayY(rawY);
6194 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6195 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6196 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6197
6198 processPosition(mapper, rawX, rawY);
6199 processTouchMajor(mapper, rawTouchMajor);
6200 processToolMajor(mapper, rawToolMajor);
6201 processMTSync(mapper);
6202 processSync(mapper);
6203
6204 NotifyMotionArgs args;
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6207 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6208}
6209
6210TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211 addConfigurationProperty("touch.deviceType", "touchScreen");
6212 prepareDisplay(DISPLAY_ORIENTATION_0);
6213 prepareAxes(POSITION | PRESSURE);
6214 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6215 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006216 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217
Michael Wrightaa449c92017-12-13 21:21:43 +00006218 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006219 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006220 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6221 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6222 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6223
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 // These calculations are based on the input device calibration documentation.
6225 int32_t rawX = 100;
6226 int32_t rawY = 200;
6227 int32_t rawPressure = 60;
6228
6229 float x = toDisplayX(rawX);
6230 float y = toDisplayY(rawY);
6231 float pressure = float(rawPressure) * 0.01f;
6232
6233 processPosition(mapper, rawX, rawY);
6234 processPressure(mapper, rawPressure);
6235 processMTSync(mapper);
6236 processSync(mapper);
6237
6238 NotifyMotionArgs args;
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6241 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6242}
6243
6244TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006245 addConfigurationProperty("touch.deviceType", "touchScreen");
6246 prepareDisplay(DISPLAY_ORIENTATION_0);
6247 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006248 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249
6250 NotifyMotionArgs motionArgs;
6251 NotifyKeyArgs keyArgs;
6252
6253 processId(mapper, 1);
6254 processPosition(mapper, 100, 200);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6257 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6258 ASSERT_EQ(0, motionArgs.buttonState);
6259
6260 // press BTN_LEFT, release BTN_LEFT
6261 processKey(mapper, BTN_LEFT, 1);
6262 processSync(mapper);
6263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6265 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6266
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6269 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6270
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271 processKey(mapper, BTN_LEFT, 0);
6272 processSync(mapper);
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006274 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006276
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280
6281 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6282 processKey(mapper, BTN_RIGHT, 1);
6283 processKey(mapper, BTN_MIDDLE, 1);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6287 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6288 motionArgs.buttonState);
6289
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6292 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6293
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6296 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6297 motionArgs.buttonState);
6298
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299 processKey(mapper, BTN_RIGHT, 0);
6300 processSync(mapper);
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006307 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308
6309 processKey(mapper, BTN_MIDDLE, 0);
6310 processSync(mapper);
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006314
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318
6319 // press BTN_BACK, release BTN_BACK
6320 processKey(mapper, BTN_BACK, 1);
6321 processSync(mapper);
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6323 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6324 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006325
Michael Wrightd02c5b62014-02-10 15:10:22 -08006326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006328 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6329
6330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6332 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333
6334 processKey(mapper, BTN_BACK, 0);
6335 processSync(mapper);
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006339
6340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342 ASSERT_EQ(0, motionArgs.buttonState);
6343
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6345 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6346 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6347
6348 // press BTN_SIDE, release BTN_SIDE
6349 processKey(mapper, BTN_SIDE, 1);
6350 processSync(mapper);
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6352 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6353 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006354
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006357 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6358
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6361 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362
6363 processKey(mapper, BTN_SIDE, 0);
6364 processSync(mapper);
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006368
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006371 ASSERT_EQ(0, motionArgs.buttonState);
6372
Michael Wrightd02c5b62014-02-10 15:10:22 -08006373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6374 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6375 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6376
6377 // press BTN_FORWARD, release BTN_FORWARD
6378 processKey(mapper, BTN_FORWARD, 1);
6379 processSync(mapper);
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6381 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6382 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006383
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006386 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6387
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6390 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391
6392 processKey(mapper, BTN_FORWARD, 0);
6393 processSync(mapper);
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006397
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400 ASSERT_EQ(0, motionArgs.buttonState);
6401
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6403 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6404 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6405
6406 // press BTN_EXTRA, release BTN_EXTRA
6407 processKey(mapper, BTN_EXTRA, 1);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6410 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6411 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006412
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006415 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6416
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6419 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006420
6421 processKey(mapper, BTN_EXTRA, 0);
6422 processSync(mapper);
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006424 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006426
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429 ASSERT_EQ(0, motionArgs.buttonState);
6430
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6432 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6433 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6434
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6436
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437 // press BTN_STYLUS, release BTN_STYLUS
6438 processKey(mapper, BTN_STYLUS, 1);
6439 processSync(mapper);
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006442 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6443
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6446 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006447
6448 processKey(mapper, BTN_STYLUS, 0);
6449 processSync(mapper);
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006451 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006452 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006453
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457
6458 // press BTN_STYLUS2, release BTN_STYLUS2
6459 processKey(mapper, BTN_STYLUS2, 1);
6460 processSync(mapper);
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006463 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6464
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6467 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006468
6469 processKey(mapper, BTN_STYLUS2, 0);
6470 processSync(mapper);
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006472 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006474
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006477 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478
6479 // release touch
6480 processId(mapper, -1);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6483 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6484 ASSERT_EQ(0, motionArgs.buttonState);
6485}
6486
6487TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006488 addConfigurationProperty("touch.deviceType", "touchScreen");
6489 prepareDisplay(DISPLAY_ORIENTATION_0);
6490 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006491 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492
6493 NotifyMotionArgs motionArgs;
6494
6495 // default tool type is finger
6496 processId(mapper, 1);
6497 processPosition(mapper, 100, 200);
6498 processSync(mapper);
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6500 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6501 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6502
6503 // eraser
6504 processKey(mapper, BTN_TOOL_RUBBER, 1);
6505 processSync(mapper);
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6509
6510 // stylus
6511 processKey(mapper, BTN_TOOL_RUBBER, 0);
6512 processKey(mapper, BTN_TOOL_PEN, 1);
6513 processSync(mapper);
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6517
6518 // brush
6519 processKey(mapper, BTN_TOOL_PEN, 0);
6520 processKey(mapper, BTN_TOOL_BRUSH, 1);
6521 processSync(mapper);
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6525
6526 // pencil
6527 processKey(mapper, BTN_TOOL_BRUSH, 0);
6528 processKey(mapper, BTN_TOOL_PENCIL, 1);
6529 processSync(mapper);
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6533
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006534 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006535 processKey(mapper, BTN_TOOL_PENCIL, 0);
6536 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6537 processSync(mapper);
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6541
6542 // mouse
6543 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6544 processKey(mapper, BTN_TOOL_MOUSE, 1);
6545 processSync(mapper);
6546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6549
6550 // lens
6551 processKey(mapper, BTN_TOOL_MOUSE, 0);
6552 processKey(mapper, BTN_TOOL_LENS, 1);
6553 processSync(mapper);
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6557
6558 // double-tap
6559 processKey(mapper, BTN_TOOL_LENS, 0);
6560 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6561 processSync(mapper);
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6565
6566 // triple-tap
6567 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6568 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6569 processSync(mapper);
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6573
6574 // quad-tap
6575 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6576 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6577 processSync(mapper);
6578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6581
6582 // finger
6583 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6584 processKey(mapper, BTN_TOOL_FINGER, 1);
6585 processSync(mapper);
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6587 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6589
6590 // stylus trumps finger
6591 processKey(mapper, BTN_TOOL_PEN, 1);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6596
6597 // eraser trumps stylus
6598 processKey(mapper, BTN_TOOL_RUBBER, 1);
6599 processSync(mapper);
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6603
6604 // mouse trumps eraser
6605 processKey(mapper, BTN_TOOL_MOUSE, 1);
6606 processSync(mapper);
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6610
6611 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6612 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6613 processSync(mapper);
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6617
6618 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6619 processToolType(mapper, MT_TOOL_PEN);
6620 processSync(mapper);
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6624
6625 // back to default tool type
6626 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6627 processKey(mapper, BTN_TOOL_MOUSE, 0);
6628 processKey(mapper, BTN_TOOL_RUBBER, 0);
6629 processKey(mapper, BTN_TOOL_PEN, 0);
6630 processKey(mapper, BTN_TOOL_FINGER, 0);
6631 processSync(mapper);
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6635}
6636
6637TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006638 addConfigurationProperty("touch.deviceType", "touchScreen");
6639 prepareDisplay(DISPLAY_ORIENTATION_0);
6640 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006641 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006642 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006643
6644 NotifyMotionArgs motionArgs;
6645
6646 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6647 processId(mapper, 1);
6648 processPosition(mapper, 100, 200);
6649 processSync(mapper);
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6653 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6654
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6656 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6658 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6659
6660 // move a little
6661 processPosition(mapper, 150, 250);
6662 processSync(mapper);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6664 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6666 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6667
6668 // down when BTN_TOUCH is pressed, pressure defaults to 1
6669 processKey(mapper, BTN_TOUCH, 1);
6670 processSync(mapper);
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6672 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6674 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6675
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6679 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6680
6681 // up when BTN_TOUCH is released, hover restored
6682 processKey(mapper, BTN_TOUCH, 0);
6683 processSync(mapper);
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6685 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6687 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6688
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6692 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6693
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6697 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6698
6699 // exit hover when pointer goes away
6700 processId(mapper, -1);
6701 processSync(mapper);
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6703 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6705 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6706}
6707
6708TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006709 addConfigurationProperty("touch.deviceType", "touchScreen");
6710 prepareDisplay(DISPLAY_ORIENTATION_0);
6711 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006712 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006713
6714 NotifyMotionArgs motionArgs;
6715
6716 // initially hovering because pressure is 0
6717 processId(mapper, 1);
6718 processPosition(mapper, 100, 200);
6719 processPressure(mapper, 0);
6720 processSync(mapper);
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6722 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6724 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6725
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6727 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6729 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6730
6731 // move a little
6732 processPosition(mapper, 150, 250);
6733 processSync(mapper);
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6737 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6738
6739 // down when pressure becomes non-zero
6740 processPressure(mapper, RAW_PRESSURE_MAX);
6741 processSync(mapper);
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6743 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6745 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6746
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6751
6752 // up when pressure becomes 0, hover restored
6753 processPressure(mapper, 0);
6754 processSync(mapper);
6755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6756 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6758 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6759
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6763 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6764
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6768 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6769
6770 // exit hover when pointer goes away
6771 processId(mapper, -1);
6772 processSync(mapper);
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6776 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6777}
6778
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006779/**
6780 * Set the input device port <--> display port associations, and check that the
6781 * events are routed to the display that matches the display port.
6782 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6783 */
6784TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006785 const std::string usb2 = "USB2";
6786 const uint8_t hdmi1 = 0;
6787 const uint8_t hdmi2 = 1;
6788 const std::string secondaryUniqueId = "uniqueId2";
6789 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6790
6791 addConfigurationProperty("touch.deviceType", "touchScreen");
6792 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006793 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006794
6795 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6796 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6797
6798 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6799 // for this input device is specified, and the matching viewport is not present,
6800 // the input device should be disabled (at the mapper level).
6801
6802 // Add viewport for display 2 on hdmi2
6803 prepareSecondaryDisplay(type, hdmi2);
6804 // Send a touch event
6805 processPosition(mapper, 100, 100);
6806 processSync(mapper);
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6808
6809 // Add viewport for display 1 on hdmi1
6810 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6811 // Send a touch event again
6812 processPosition(mapper, 100, 100);
6813 processSync(mapper);
6814
6815 NotifyMotionArgs args;
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6817 ASSERT_EQ(DISPLAY_ID, args.displayId);
6818}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006819
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006820TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006821 // Setup for second display.
Michael Wright7a376672020-06-26 20:51:44 +01006822 std::shared_ptr<FakePointerController> fakePointerController =
6823 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006824 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006825 fakePointerController->setPosition(100, 200);
6826 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006827 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6828
Garfield Tan888a6a42020-01-09 11:39:16 -08006829 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6830 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6831
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006832 prepareDisplay(DISPLAY_ORIENTATION_0);
6833 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006834 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006835
6836 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006837 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006838
6839 NotifyMotionArgs motionArgs;
6840 processPosition(mapper, 100, 100);
6841 processSync(mapper);
6842
6843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6844 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6845 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6846}
6847
Arthur Hung7c645402019-01-25 17:45:42 +08006848TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6849 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006850 prepareAxes(POSITION | ID | SLOT);
6851 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006852 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006853
6854 // Create the second touch screen device, and enable multi fingers.
6855 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006856 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006857 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006858 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006859 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006860 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006861 std::unique_ptr<InputDevice> device2 =
6862 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006863 identifier);
6864 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6865 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6866 0 /*flat*/, 0 /*fuzz*/);
6867 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6868 0 /*flat*/, 0 /*fuzz*/);
6869 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6870 0 /*flat*/, 0 /*fuzz*/);
6871 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6872 0 /*flat*/, 0 /*fuzz*/);
6873 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6874 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6875 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006876
6877 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006878 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006879 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6880 device2->reset(ARBITRARY_TIME);
6881
6882 // Setup PointerController.
Michael Wright7a376672020-06-26 20:51:44 +01006883 std::shared_ptr<FakePointerController> fakePointerController =
6884 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006885 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6886 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6887
6888 // Setup policy for associated displays and show touches.
6889 const uint8_t hdmi1 = 0;
6890 const uint8_t hdmi2 = 1;
6891 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6892 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6893 mFakePolicy->setShowTouches(true);
6894
6895 // Create displays.
6896 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6897 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6898
6899 // Default device will reconfigure above, need additional reconfiguration for another device.
6900 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6901 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6902
6903 // Two fingers down at default display.
6904 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6905 processPosition(mapper, x1, y1);
6906 processId(mapper, 1);
6907 processSlot(mapper, 1);
6908 processPosition(mapper, x2, y2);
6909 processId(mapper, 2);
6910 processSync(mapper);
6911
6912 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6913 fakePointerController->getSpots().find(DISPLAY_ID);
6914 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6915 ASSERT_EQ(size_t(2), iter->second.size());
6916
6917 // Two fingers down at second display.
6918 processPosition(mapper2, x1, y1);
6919 processId(mapper2, 1);
6920 processSlot(mapper2, 1);
6921 processPosition(mapper2, x2, y2);
6922 processId(mapper2, 2);
6923 processSync(mapper2);
6924
6925 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6926 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6927 ASSERT_EQ(size_t(2), iter->second.size());
6928}
6929
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006930TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006931 prepareAxes(POSITION);
6932 addConfigurationProperty("touch.deviceType", "touchScreen");
6933 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006934 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006935
6936 NotifyMotionArgs motionArgs;
6937 // Unrotated video frame
6938 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6939 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006940 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006941 processPosition(mapper, 100, 200);
6942 processSync(mapper);
6943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6944 ASSERT_EQ(frames, motionArgs.videoFrames);
6945
6946 // Subsequent touch events should not have any videoframes
6947 // This is implemented separately in FakeEventHub,
6948 // but that should match the behaviour of TouchVideoDevice.
6949 processPosition(mapper, 200, 200);
6950 processSync(mapper);
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6952 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6953}
6954
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006955TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006956 prepareAxes(POSITION);
6957 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006958 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006959 // Unrotated video frame
6960 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6961 NotifyMotionArgs motionArgs;
6962
6963 // Test all 4 orientations
6964 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6965 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6966 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6967 clearViewports();
6968 prepareDisplay(orientation);
6969 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006970 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006971 processPosition(mapper, 100, 200);
6972 processSync(mapper);
6973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6974 frames[0].rotate(orientation);
6975 ASSERT_EQ(frames, motionArgs.videoFrames);
6976 }
6977}
6978
6979TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006980 prepareAxes(POSITION);
6981 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006982 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006983 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6984 // so mix these.
6985 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6986 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6987 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6988 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6989 NotifyMotionArgs motionArgs;
6990
6991 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006992 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006993 processPosition(mapper, 100, 200);
6994 processSync(mapper);
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6996 std::for_each(frames.begin(), frames.end(),
6997 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6998 ASSERT_EQ(frames, motionArgs.videoFrames);
6999}
7000
Arthur Hung9da14732019-09-02 16:16:58 +08007001/**
7002 * If we had defined port associations, but the viewport is not ready, the touch device would be
7003 * expected to be disabled, and it should be enabled after the viewport has found.
7004 */
7005TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007006 constexpr uint8_t hdmi2 = 1;
7007 const std::string secondaryUniqueId = "uniqueId2";
7008 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
7009
7010 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7011
7012 addConfigurationProperty("touch.deviceType", "touchScreen");
7013 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007014 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007015
7016 ASSERT_EQ(mDevice->isEnabled(), false);
7017
7018 // Add display on hdmi2, the device should be enabled and can receive touch event.
7019 prepareSecondaryDisplay(type, hdmi2);
7020 ASSERT_EQ(mDevice->isEnabled(), true);
7021
7022 // Send a touch event.
7023 processPosition(mapper, 100, 100);
7024 processSync(mapper);
7025
7026 NotifyMotionArgs args;
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7028 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7029}
7030
Arthur Hung6cd19a42019-08-30 19:04:12 +08007031
Arthur Hung6cd19a42019-08-30 19:04:12 +08007032
Arthur Hung421eb1c2020-01-16 00:09:42 +08007033TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007034 addConfigurationProperty("touch.deviceType", "touchScreen");
7035 prepareDisplay(DISPLAY_ORIENTATION_0);
7036 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007037 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007038
7039 NotifyMotionArgs motionArgs;
7040
7041 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7042 // finger down
7043 processId(mapper, 1);
7044 processPosition(mapper, x1, y1);
7045 processSync(mapper);
7046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7048 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7049
7050 // finger move
7051 processId(mapper, 1);
7052 processPosition(mapper, x2, y2);
7053 processSync(mapper);
7054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7057
7058 // finger up.
7059 processId(mapper, -1);
7060 processSync(mapper);
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7062 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7064
7065 // new finger down
7066 processId(mapper, 1);
7067 processPosition(mapper, x3, y3);
7068 processSync(mapper);
7069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7070 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7072}
7073
7074/**
arthurhung65600042020-04-30 17:55:40 +08007075 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7076 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007077 */
arthurhung65600042020-04-30 17:55:40 +08007078TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007079 addConfigurationProperty("touch.deviceType", "touchScreen");
7080 prepareDisplay(DISPLAY_ORIENTATION_0);
7081 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007082 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007083
7084 NotifyMotionArgs motionArgs;
7085
7086 // default tool type is finger
7087 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhung65600042020-04-30 17:55:40 +08007088 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007089 processPosition(mapper, x1, y1);
7090 processSync(mapper);
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7092 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7094
7095 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7096 processToolType(mapper, MT_TOOL_PALM);
7097 processSync(mapper);
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7099 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7100
7101 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhung65600042020-04-30 17:55:40 +08007102 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007103 processPosition(mapper, x2, y2);
7104 processSync(mapper);
7105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7106
7107 // finger up.
arthurhung65600042020-04-30 17:55:40 +08007108 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007109 processSync(mapper);
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7111
7112 // new finger down
arthurhung65600042020-04-30 17:55:40 +08007113 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007114 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007115 processPosition(mapper, x3, y3);
7116 processSync(mapper);
7117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7118 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7120}
7121
arthurhungbf89a482020-04-17 17:37:55 +08007122/**
arthurhung65600042020-04-30 17:55:40 +08007123 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7124 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007125 */
arthurhung65600042020-04-30 17:55:40 +08007126TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007127 addConfigurationProperty("touch.deviceType", "touchScreen");
7128 prepareDisplay(DISPLAY_ORIENTATION_0);
7129 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7130 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7131
7132 NotifyMotionArgs motionArgs;
7133
7134 // default tool type is finger
arthurhung65600042020-04-30 17:55:40 +08007135 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7136 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007137 processPosition(mapper, x1, y1);
7138 processSync(mapper);
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7140 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7142
7143 // Second finger down.
arthurhung65600042020-04-30 17:55:40 +08007144 processSlot(mapper, SECOND_SLOT);
7145 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007146 processPosition(mapper, x2, y2);
arthurhung65600042020-04-30 17:55:40 +08007147 processSync(mapper);
7148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7149 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7150 motionArgs.action);
7151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7152
7153 // If the tool type of the first finger changes to MT_TOOL_PALM,
7154 // we expect to receive ACTION_POINTER_UP with cancel flag.
7155 processSlot(mapper, FIRST_SLOT);
7156 processId(mapper, FIRST_TRACKING_ID);
7157 processToolType(mapper, MT_TOOL_PALM);
7158 processSync(mapper);
7159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7160 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7161 motionArgs.action);
7162 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7163
7164 // The following MOVE events of second finger should be processed.
7165 processSlot(mapper, SECOND_SLOT);
7166 processId(mapper, SECOND_TRACKING_ID);
7167 processPosition(mapper, x2 + 1, y2 + 1);
7168 processSync(mapper);
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7171 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7172
7173 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7174 // it. Second finger receive move.
7175 processSlot(mapper, FIRST_SLOT);
7176 processId(mapper, INVALID_TRACKING_ID);
7177 processSync(mapper);
7178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7180 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7181
7182 // Second finger keeps moving.
7183 processSlot(mapper, SECOND_SLOT);
7184 processId(mapper, SECOND_TRACKING_ID);
7185 processPosition(mapper, x2 + 2, y2 + 2);
7186 processSync(mapper);
7187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7189 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7190
7191 // Second finger up.
7192 processId(mapper, INVALID_TRACKING_ID);
7193 processSync(mapper);
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7195 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7196 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7197}
7198
7199/**
7200 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7201 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7202 */
7203TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7204 addConfigurationProperty("touch.deviceType", "touchScreen");
7205 prepareDisplay(DISPLAY_ORIENTATION_0);
7206 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7207 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7208
7209 NotifyMotionArgs motionArgs;
7210
7211 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7212 // First finger down.
7213 processId(mapper, FIRST_TRACKING_ID);
7214 processPosition(mapper, x1, y1);
7215 processSync(mapper);
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7217 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7219
7220 // Second finger down.
7221 processSlot(mapper, SECOND_SLOT);
7222 processId(mapper, SECOND_TRACKING_ID);
7223 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007224 processSync(mapper);
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7226 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7227 motionArgs.action);
7228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7229
arthurhung65600042020-04-30 17:55:40 +08007230 // If the tool type of the first finger changes to MT_TOOL_PALM,
7231 // we expect to receive ACTION_POINTER_UP with cancel flag.
7232 processSlot(mapper, FIRST_SLOT);
7233 processId(mapper, FIRST_TRACKING_ID);
7234 processToolType(mapper, MT_TOOL_PALM);
7235 processSync(mapper);
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7237 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7238 motionArgs.action);
7239 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7240
7241 // Second finger keeps moving.
7242 processSlot(mapper, SECOND_SLOT);
7243 processId(mapper, SECOND_TRACKING_ID);
7244 processPosition(mapper, x2 + 1, y2 + 1);
7245 processSync(mapper);
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7248
7249 // second finger becomes palm, receive cancel due to only 1 finger is active.
7250 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007251 processToolType(mapper, MT_TOOL_PALM);
7252 processSync(mapper);
7253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7254 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7255
arthurhung65600042020-04-30 17:55:40 +08007256 // third finger down.
7257 processSlot(mapper, THIRD_SLOT);
7258 processId(mapper, THIRD_TRACKING_ID);
7259 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007260 processPosition(mapper, x3, y3);
7261 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7263 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhung65600042020-04-30 17:55:40 +08007265 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7266
7267 // third finger move
7268 processId(mapper, THIRD_TRACKING_ID);
7269 processPosition(mapper, x3 + 1, y3 + 1);
7270 processSync(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7273
7274 // first finger up, third finger receive move.
7275 processSlot(mapper, FIRST_SLOT);
7276 processId(mapper, INVALID_TRACKING_ID);
7277 processSync(mapper);
7278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7280 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7281
7282 // second finger up, third finger receive move.
7283 processSlot(mapper, SECOND_SLOT);
7284 processId(mapper, INVALID_TRACKING_ID);
7285 processSync(mapper);
7286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7288 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7289
7290 // third finger up.
7291 processSlot(mapper, THIRD_SLOT);
7292 processId(mapper, INVALID_TRACKING_ID);
7293 processSync(mapper);
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7295 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7296 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7297}
7298
7299/**
7300 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7301 * and the active finger could still be allowed to receive the events
7302 */
7303TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7304 addConfigurationProperty("touch.deviceType", "touchScreen");
7305 prepareDisplay(DISPLAY_ORIENTATION_0);
7306 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7307 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7308
7309 NotifyMotionArgs motionArgs;
7310
7311 // default tool type is finger
7312 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7313 processId(mapper, FIRST_TRACKING_ID);
7314 processPosition(mapper, x1, y1);
7315 processSync(mapper);
7316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7319
7320 // Second finger down.
7321 processSlot(mapper, SECOND_SLOT);
7322 processId(mapper, SECOND_TRACKING_ID);
7323 processPosition(mapper, x2, y2);
7324 processSync(mapper);
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7326 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7327 motionArgs.action);
7328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7329
7330 // If the tool type of the second finger changes to MT_TOOL_PALM,
7331 // we expect to receive ACTION_POINTER_UP with cancel flag.
7332 processId(mapper, SECOND_TRACKING_ID);
7333 processToolType(mapper, MT_TOOL_PALM);
7334 processSync(mapper);
7335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7336 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7337 motionArgs.action);
7338 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7339
7340 // The following MOVE event should be processed.
7341 processSlot(mapper, FIRST_SLOT);
7342 processId(mapper, FIRST_TRACKING_ID);
7343 processPosition(mapper, x1 + 1, y1 + 1);
7344 processSync(mapper);
7345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7347 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7348
7349 // second finger up.
7350 processSlot(mapper, SECOND_SLOT);
7351 processId(mapper, INVALID_TRACKING_ID);
7352 processSync(mapper);
7353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7355
7356 // first finger keep moving
7357 processSlot(mapper, FIRST_SLOT);
7358 processId(mapper, FIRST_TRACKING_ID);
7359 processPosition(mapper, x1 + 2, y1 + 2);
7360 processSync(mapper);
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7363
7364 // first finger up.
7365 processId(mapper, INVALID_TRACKING_ID);
7366 processSync(mapper);
7367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7368 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7369 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007370}
7371
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007372// --- MultiTouchInputMapperTest_ExternalDevice ---
7373
7374class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7375protected:
7376 virtual void SetUp() override {
7377 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
7378 }
7379};
7380
7381/**
7382 * Expect fallback to internal viewport if device is external and external viewport is not present.
7383 */
7384TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7385 prepareAxes(POSITION);
7386 addConfigurationProperty("touch.deviceType", "touchScreen");
7387 prepareDisplay(DISPLAY_ORIENTATION_0);
7388 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7389
7390 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7391
7392 NotifyMotionArgs motionArgs;
7393
7394 // Expect the event to be sent to the internal viewport,
7395 // because an external viewport is not present.
7396 processPosition(mapper, 100, 100);
7397 processSync(mapper);
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7399 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7400
7401 // Expect the event to be sent to the external viewport if it is present.
7402 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
7403 processPosition(mapper, 100, 100);
7404 processSync(mapper);
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7406 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7407}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007408
7409/**
7410 * Test touch should not work if outside of surface.
7411 */
7412class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7413protected:
7414 void halfDisplayToCenterHorizontal(int32_t orientation) {
7415 std::optional<DisplayViewport> internalViewport =
7416 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
7417
7418 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7419 internalViewport->orientation = orientation;
7420 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7421 internalViewport->logicalLeft = 0;
7422 internalViewport->logicalTop = 0;
7423 internalViewport->logicalRight = DISPLAY_HEIGHT;
7424 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7425
7426 internalViewport->physicalLeft = 0;
7427 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7428 internalViewport->physicalRight = DISPLAY_HEIGHT;
7429 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7430
7431 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7432 internalViewport->deviceHeight = DISPLAY_WIDTH;
7433 } else {
7434 internalViewport->logicalLeft = 0;
7435 internalViewport->logicalTop = 0;
7436 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7437 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7438
7439 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7440 internalViewport->physicalTop = 0;
7441 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7442 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7443
7444 internalViewport->deviceWidth = DISPLAY_WIDTH;
7445 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7446 }
7447
7448 mFakePolicy->updateViewport(internalViewport.value());
7449 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7450 }
7451
arthurhung10052f62020-12-29 20:28:15 +08007452 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
7453 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08007454 int32_t yExpected) {
7455 // touch on outside area should not work.
7456 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7457 processSync(mapper);
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7459
7460 // touch on inside area should receive the event.
7461 NotifyMotionArgs args;
7462 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7463 processSync(mapper);
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7465 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7466 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7467
7468 // Reset.
7469 mapper.reset(ARBITRARY_TIME);
7470 }
7471};
7472
7473TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7474 addConfigurationProperty("touch.deviceType", "touchScreen");
7475 prepareDisplay(DISPLAY_ORIENTATION_0);
7476 prepareAxes(POSITION);
7477 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7478
7479 // Touch on center of normal display should work.
7480 const int32_t x = DISPLAY_WIDTH / 4;
7481 const int32_t y = DISPLAY_HEIGHT / 2;
7482 processPosition(mapper, toRawX(x), toRawY(y));
7483 processSync(mapper);
7484 NotifyMotionArgs args;
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7487 0.0f, 0.0f, 0.0f, 0.0f));
7488 // Reset.
7489 mapper.reset(ARBITRARY_TIME);
7490
7491 // Let physical display be different to device, and make surface and physical could be 1:1.
7492 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7493
7494 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7495 const int32_t yExpected = y;
7496 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7497}
7498
7499TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7500 addConfigurationProperty("touch.deviceType", "touchScreen");
7501 prepareDisplay(DISPLAY_ORIENTATION_0);
7502 prepareAxes(POSITION);
7503 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7504
7505 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7506 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7507
7508 const int32_t x = DISPLAY_WIDTH / 4;
7509 const int32_t y = DISPLAY_HEIGHT / 2;
7510
7511 // expect x/y = swap x/y then reverse y.
7512 const int32_t xExpected = y;
7513 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7514 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7515}
7516
7517TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7518 addConfigurationProperty("touch.deviceType", "touchScreen");
7519 prepareDisplay(DISPLAY_ORIENTATION_0);
7520 prepareAxes(POSITION);
7521 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7522
7523 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7524 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7525
7526 const int32_t x = DISPLAY_WIDTH / 4;
7527 const int32_t y = DISPLAY_HEIGHT / 2;
7528
7529 // expect x/y = swap x/y then reverse x.
7530 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7531 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7532 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7533}
Nathaniel R. Lewiseba157b2018-02-22 13:31:42 -08007534
arthurhung10052f62020-12-29 20:28:15 +08007535TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
7536 addConfigurationProperty("touch.deviceType", "touchScreen");
7537 prepareDisplay(DISPLAY_ORIENTATION_0);
7538 prepareAxes(POSITION);
7539 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7540
7541 const int32_t x = 0;
7542 const int32_t y = 0;
7543
7544 const int32_t xExpected = x;
7545 const int32_t yExpected = y;
7546 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
7547
7548 clearViewports();
7549 prepareDisplay(DISPLAY_ORIENTATION_90);
7550 // expect x/y = swap x/y then reverse y.
7551 const int32_t xExpected90 = y;
7552 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
7553 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
7554
7555 clearViewports();
7556 prepareDisplay(DISPLAY_ORIENTATION_270);
7557 // expect x/y = swap x/y then reverse x.
7558 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
7559 const int32_t yExpected270 = x;
7560 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
7561}
7562
Nathaniel R. Lewiseba157b2018-02-22 13:31:42 -08007563TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7564 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7565 std::shared_ptr<FakePointerController> fakePointerController =
7566 std::make_shared<FakePointerController>();
7567 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7568 fakePointerController->setPosition(0, 0);
7569 fakePointerController->setButtonState(0);
7570
7571 // prepare device and capture
7572 prepareDisplay(DISPLAY_ORIENTATION_0);
7573 prepareAxes(POSITION | ID | SLOT);
7574 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7575 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7576 mFakePolicy->setPointerCapture(true);
7577 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7578 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7579
7580 // captured touchpad should be a touchpad source
7581 NotifyDeviceResetArgs resetArgs;
7582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7583 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7584
Chris Ye1fb45302020-09-02 22:41:50 -07007585 InputDeviceInfo deviceInfo;
7586 mDevice->getDeviceInfo(&deviceInfo);
7587
7588 const InputDeviceInfo::MotionRange* relRangeX =
7589 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
7590 ASSERT_NE(relRangeX, nullptr);
7591 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
7592 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
7593 const InputDeviceInfo::MotionRange* relRangeY =
7594 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
7595 ASSERT_NE(relRangeY, nullptr);
7596 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
7597 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
7598
Nathaniel R. Lewiseba157b2018-02-22 13:31:42 -08007599 // run captured pointer tests - note that this is unscaled, so input listener events should be
7600 // identical to what the hardware sends (accounting for any
7601 // calibration).
7602 // FINGER 0 DOWN
Chris Yedca44af2020-08-05 15:07:56 -07007603 processSlot(mapper, 0);
Nathaniel R. Lewiseba157b2018-02-22 13:31:42 -08007604 processId(mapper, 1);
7605 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7606 processKey(mapper, BTN_TOUCH, 1);
7607 processSync(mapper);
7608
7609 // expect coord[0] to contain initial location of touch 0
7610 NotifyMotionArgs args;
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7612 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7613 ASSERT_EQ(1U, args.pointerCount);
7614 ASSERT_EQ(0, args.pointerProperties[0].id);
7615 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7616 ASSERT_NO_FATAL_FAILURE(
7617 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7618
7619 // FINGER 1 DOWN
7620 processSlot(mapper, 1);
7621 processId(mapper, 2);
7622 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7623 processSync(mapper);
7624
7625 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Yedca44af2020-08-05 15:07:56 -07007627 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7628 args.action);
Nathaniel R. Lewiseba157b2018-02-22 13:31:42 -08007629 ASSERT_EQ(2U, args.pointerCount);
7630 ASSERT_EQ(0, args.pointerProperties[0].id);
7631 ASSERT_EQ(1, args.pointerProperties[1].id);
7632 ASSERT_NO_FATAL_FAILURE(
7633 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7634 ASSERT_NO_FATAL_FAILURE(
7635 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7636
7637 // FINGER 1 MOVE
7638 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7639 processSync(mapper);
7640
7641 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7642 // from move
7643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7645 ASSERT_NO_FATAL_FAILURE(
7646 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7647 ASSERT_NO_FATAL_FAILURE(
7648 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7649
7650 // FINGER 0 MOVE
7651 processSlot(mapper, 0);
7652 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7653 processSync(mapper);
7654
7655 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7658 ASSERT_NO_FATAL_FAILURE(
7659 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7660 ASSERT_NO_FATAL_FAILURE(
7661 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7662
7663 // BUTTON DOWN
7664 processKey(mapper, BTN_LEFT, 1);
7665 processSync(mapper);
7666
7667 // touchinputmapper design sends a move before button press
7668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7672
7673 // BUTTON UP
7674 processKey(mapper, BTN_LEFT, 0);
7675 processSync(mapper);
7676
7677 // touchinputmapper design sends a move after button release
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7682
7683 // FINGER 0 UP
7684 processId(mapper, -1);
7685 processSync(mapper);
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7687 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7688
7689 // FINGER 1 MOVE
7690 processSlot(mapper, 1);
7691 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7692 processSync(mapper);
7693
7694 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7697 ASSERT_EQ(1U, args.pointerCount);
7698 ASSERT_EQ(1, args.pointerProperties[0].id);
7699 ASSERT_NO_FATAL_FAILURE(
7700 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7701
7702 // FINGER 1 UP
7703 processId(mapper, -1);
7704 processKey(mapper, BTN_TOUCH, 0);
7705 processSync(mapper);
7706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7707 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7708
7709 // non captured touchpad should be a mouse source
7710 mFakePolicy->setPointerCapture(false);
7711 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7713 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7714}
7715
7716TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7717 std::shared_ptr<FakePointerController> fakePointerController =
7718 std::make_shared<FakePointerController>();
7719 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7720 fakePointerController->setPosition(0, 0);
7721 fakePointerController->setButtonState(0);
7722
7723 // prepare device and capture
7724 prepareDisplay(DISPLAY_ORIENTATION_0);
7725 prepareAxes(POSITION | ID | SLOT);
7726 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7727 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7728 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7729 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7730 // run uncaptured pointer tests - pushes out generic events
7731 // FINGER 0 DOWN
7732 processId(mapper, 3);
7733 processPosition(mapper, 100, 100);
7734 processKey(mapper, BTN_TOUCH, 1);
7735 processSync(mapper);
7736
7737 // start at (100,100), cursor should be at (0,0) * scale
7738 NotifyMotionArgs args;
7739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7741 ASSERT_NO_FATAL_FAILURE(
7742 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7743
7744 // FINGER 0 MOVE
7745 processPosition(mapper, 200, 200);
7746 processSync(mapper);
7747
7748 // compute scaling to help with touch position checking
7749 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7750 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7751 float scale =
7752 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7753
7754 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7758 0, 0, 0, 0, 0, 0, 0));
7759}
7760
7761TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7762 std::shared_ptr<FakePointerController> fakePointerController =
7763 std::make_shared<FakePointerController>();
7764
7765 prepareDisplay(DISPLAY_ORIENTATION_0);
7766 prepareAxes(POSITION | ID | SLOT);
7767 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7768 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7769 mFakePolicy->setPointerCapture(false);
7770 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7771
7772 // uncaptured touchpad should be a pointer device
7773 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7774
7775 // captured touchpad should be a touchpad device
7776 mFakePolicy->setPointerCapture(true);
7777 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7778 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7779}
7780
Michael Wrightd02c5b62014-02-10 15:10:22 -08007781} // namespace android