blob: 8ca7e4a8939ae47238cbea8222170225542dd9dd [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>
21#include <KeyboardInputMapper.h>
22#include <MultiTouchInputMapper.h>
23#include <SingleTouchInputMapper.h>
24#include <SwitchInputMapper.h>
25#include <TestInputListener.h>
26#include <TouchInputMapper.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070028#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080030#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <math.h>
32
33namespace android {
34
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070035using std::chrono_literals::operator""ms;
36
37// Timeout for waiting for an expected event
38static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
39
Michael Wrightd02c5b62014-02-10 15:10:22 -080040// An arbitrary time value.
41static const nsecs_t ARBITRARY_TIME = 1234;
42
43// Arbitrary display properties.
44static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070045static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080046static const int32_t DISPLAY_WIDTH = 480;
47static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070048static const int32_t VIRTUAL_DISPLAY_ID = 1;
49static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
50static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070051static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070052static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
54// Error tolerance for floating point assertions.
55static const float EPSILON = 0.001f;
56
57template<typename T>
58static inline T min(T a, T b) {
59 return a < b ? a : b;
60}
61
62static inline float avg(float x, float y) {
63 return (x + y) / 2;
64}
65
66
67// --- FakePointerController ---
68
69class FakePointerController : public PointerControllerInterface {
70 bool mHaveBounds;
71 float mMinX, mMinY, mMaxX, mMaxY;
72 float mX, mY;
73 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080074 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080075
76protected:
77 virtual ~FakePointerController() { }
78
79public:
80 FakePointerController() :
81 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080082 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080083 }
84
85 void setBounds(float minX, float minY, float maxX, float maxY) {
86 mHaveBounds = true;
87 mMinX = minX;
88 mMinY = minY;
89 mMaxX = maxX;
90 mMaxY = maxY;
91 }
92
93 virtual void setPosition(float x, float y) {
94 mX = x;
95 mY = y;
96 }
97
98 virtual void setButtonState(int32_t buttonState) {
99 mButtonState = buttonState;
100 }
101
102 virtual int32_t getButtonState() const {
103 return mButtonState;
104 }
105
106 virtual void getPosition(float* outX, float* outY) const {
107 *outX = mX;
108 *outY = mY;
109 }
110
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800111 virtual int32_t getDisplayId() const {
112 return mDisplayId;
113 }
114
Garfield Tan888a6a42020-01-09 11:39:16 -0800115 virtual void setDisplayViewport(const DisplayViewport& viewport) {
116 mDisplayId = viewport.displayId;
117 }
118
Arthur Hung7c645402019-01-25 17:45:42 +0800119 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
120 return mSpotsByDisplay;
121 }
122
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123private:
124 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
125 *outMinX = mMinX;
126 *outMinY = mMinY;
127 *outMaxX = mMaxX;
128 *outMaxY = mMaxY;
129 return mHaveBounds;
130 }
131
132 virtual void move(float deltaX, float deltaY) {
133 mX += deltaX;
134 if (mX < mMinX) mX = mMinX;
135 if (mX > mMaxX) mX = mMaxX;
136 mY += deltaY;
137 if (mY < mMinY) mY = mMinY;
138 if (mY > mMaxY) mY = mMaxY;
139 }
140
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100141 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142 }
143
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100144 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 }
146
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100147 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148 }
149
Arthur Hung7c645402019-01-25 17:45:42 +0800150 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
151 int32_t displayId) {
152 std::vector<int32_t> newSpots;
153 // Add spots for fingers that are down.
154 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
155 uint32_t id = idBits.clearFirstMarkedBit();
156 newSpots.push_back(id);
157 }
158
159 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160 }
161
162 virtual void clearSpots() {
163 }
Arthur Hung7c645402019-01-25 17:45:42 +0800164
165 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166};
167
168
169// --- FakeInputReaderPolicy ---
170
171class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700172 std::mutex mLock;
173 std::condition_variable mDevicesChangedCondition;
174
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175 InputReaderConfiguration mConfig;
176 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700177 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
178 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100179 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700180 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181
182protected:
183 virtual ~FakeInputReaderPolicy() { }
184
185public:
186 FakeInputReaderPolicy() {
187 }
188
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700189 void assertInputDevicesChanged() {
190 std::unique_lock<std::mutex> lock(mLock);
191 base::ScopedLockAssertion assumeLocked(mLock);
192
193 const bool devicesChanged =
194 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
195 return mInputDevicesChanged;
196 });
197 if (!devicesChanged) {
198 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
199 }
200 mInputDevicesChanged = false;
201 }
202
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700203 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100204 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100205 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700206 }
207
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700208 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
209 return mConfig.getDisplayViewportByUniqueId(uniqueId);
210 }
211 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
212 return mConfig.getDisplayViewportByType(type);
213 }
214
215 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
216 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700217 }
218
219 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700220 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
221 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700222 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700223 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700224 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100225 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226 }
227
Arthur Hung6cd19a42019-08-30 19:04:12 +0800228 bool updateViewport(const DisplayViewport& viewport) {
229 size_t count = mViewports.size();
230 for (size_t i = 0; i < count; i++) {
231 const DisplayViewport& currentViewport = mViewports[i];
232 if (currentViewport.displayId == viewport.displayId) {
233 mViewports[i] = viewport;
234 mConfig.setDisplayViewports(mViewports);
235 return true;
236 }
237 }
238 // no viewport found.
239 return false;
240 }
241
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100242 void addExcludedDeviceName(const std::string& deviceName) {
243 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244 }
245
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700246 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
247 mConfig.portAssociations.insert({inputPort, displayPort});
248 }
249
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000250 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700251
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000252 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700253
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
255 mPointerControllers.add(deviceId, controller);
256 }
257
258 const InputReaderConfiguration* getReaderConfiguration() const {
259 return &mConfig;
260 }
261
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800262 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263 return mInputDevices;
264 }
265
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100266 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700267 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700268 return transform;
269 }
270
271 void setTouchAffineTransformation(const TouchAffineTransformation t) {
272 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800273 }
274
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800275 void setPointerCapture(bool enabled) {
276 mConfig.pointerCapture = enabled;
277 }
278
Arthur Hung7c645402019-01-25 17:45:42 +0800279 void setShowTouches(bool enabled) {
280 mConfig.showTouches = enabled;
281 }
282
Garfield Tan888a6a42020-01-09 11:39:16 -0800283 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
284 mConfig.defaultPointerDisplayId = pointerDisplayId;
285 }
286
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700288 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700289 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
290 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700291 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
292 || orientation == DISPLAY_ORIENTATION_270);
293 DisplayViewport v;
294 v.displayId = displayId;
295 v.orientation = orientation;
296 v.logicalLeft = 0;
297 v.logicalTop = 0;
298 v.logicalRight = isRotated ? height : width;
299 v.logicalBottom = isRotated ? width : height;
300 v.physicalLeft = 0;
301 v.physicalTop = 0;
302 v.physicalRight = isRotated ? height : width;
303 v.physicalBottom = isRotated ? width : height;
304 v.deviceWidth = isRotated ? height : width;
305 v.deviceHeight = isRotated ? width : height;
306 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700307 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100308 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700309 return v;
310 }
311
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
313 *outConfig = mConfig;
314 }
315
316 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
317 return mPointerControllers.valueFor(deviceId);
318 }
319
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800320 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700321 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700323 mInputDevicesChanged = true;
324 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 }
326
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100327 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700328 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 }
330
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100331 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
332 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 }
334};
335
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336// --- FakeEventHub ---
337
338class FakeEventHub : public EventHubInterface {
339 struct KeyInfo {
340 int32_t keyCode;
341 uint32_t flags;
342 };
343
344 struct Device {
345 InputDeviceIdentifier identifier;
346 uint32_t classes;
347 PropertyMap configuration;
348 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
349 KeyedVector<int, bool> relativeAxes;
350 KeyedVector<int32_t, int32_t> keyCodeStates;
351 KeyedVector<int32_t, int32_t> scanCodeStates;
352 KeyedVector<int32_t, int32_t> switchStates;
353 KeyedVector<int32_t, int32_t> absoluteAxisValue;
354 KeyedVector<int32_t, KeyInfo> keysByScanCode;
355 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
356 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800357 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700358 bool enabled;
359
360 status_t enable() {
361 enabled = true;
362 return OK;
363 }
364
365 status_t disable() {
366 enabled = false;
367 return OK;
368 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700370 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700371 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372 }
373 };
374
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700375 std::mutex mLock;
376 std::condition_variable mEventsCondition;
377
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100379 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700380 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600381 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700383public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800384 virtual ~FakeEventHub() {
385 for (size_t i = 0; i < mDevices.size(); i++) {
386 delete mDevices.valueAt(i);
387 }
388 }
389
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390 FakeEventHub() { }
391
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100392 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393 Device* device = new Device(classes);
394 device->identifier.name = name;
395 mDevices.add(deviceId, device);
396
397 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
398 }
399
400 void removeDevice(int32_t deviceId) {
401 delete mDevices.valueFor(deviceId);
402 mDevices.removeItem(deviceId);
403
404 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
405 }
406
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700407 bool isDeviceEnabled(int32_t deviceId) {
408 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700409 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700410 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
411 return false;
412 }
413 return device->enabled;
414 }
415
416 status_t enableDevice(int32_t deviceId) {
417 status_t result;
418 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700419 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700420 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
421 return BAD_VALUE;
422 }
423 if (device->enabled) {
424 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
425 return OK;
426 }
427 result = device->enable();
428 return result;
429 }
430
431 status_t disableDevice(int32_t deviceId) {
432 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700433 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700434 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
435 return BAD_VALUE;
436 }
437 if (!device->enabled) {
438 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
439 return OK;
440 }
441 return device->disable();
442 }
443
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444 void finishDeviceScan() {
445 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
446 }
447
448 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
449 Device* device = getDevice(deviceId);
450 device->configuration.addProperty(key, value);
451 }
452
453 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
454 Device* device = getDevice(deviceId);
455 device->configuration.addAll(configuration);
456 }
457
458 void addAbsoluteAxis(int32_t deviceId, int axis,
459 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
460 Device* device = getDevice(deviceId);
461
462 RawAbsoluteAxisInfo info;
463 info.valid = true;
464 info.minValue = minValue;
465 info.maxValue = maxValue;
466 info.flat = flat;
467 info.fuzz = fuzz;
468 info.resolution = resolution;
469 device->absoluteAxes.add(axis, info);
470 }
471
472 void addRelativeAxis(int32_t deviceId, int32_t axis) {
473 Device* device = getDevice(deviceId);
474 device->relativeAxes.add(axis, true);
475 }
476
477 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
478 Device* device = getDevice(deviceId);
479 device->keyCodeStates.replaceValueFor(keyCode, state);
480 }
481
482 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
483 Device* device = getDevice(deviceId);
484 device->scanCodeStates.replaceValueFor(scanCode, state);
485 }
486
487 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
488 Device* device = getDevice(deviceId);
489 device->switchStates.replaceValueFor(switchCode, state);
490 }
491
492 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
493 Device* device = getDevice(deviceId);
494 device->absoluteAxisValue.replaceValueFor(axis, value);
495 }
496
497 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
498 int32_t keyCode, uint32_t flags) {
499 Device* device = getDevice(deviceId);
500 KeyInfo info;
501 info.keyCode = keyCode;
502 info.flags = flags;
503 if (scanCode) {
504 device->keysByScanCode.add(scanCode, info);
505 }
506 if (usageCode) {
507 device->keysByUsageCode.add(usageCode, info);
508 }
509 }
510
511 void addLed(int32_t deviceId, int32_t led, bool initialState) {
512 Device* device = getDevice(deviceId);
513 device->leds.add(led, initialState);
514 }
515
516 bool getLedState(int32_t deviceId, int32_t led) {
517 Device* device = getDevice(deviceId);
518 return device->leds.valueFor(led);
519 }
520
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100521 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522 return mExcludedDevices;
523 }
524
525 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
526 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800527 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528 }
529
530 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
531 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700532 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533 RawEvent event;
534 event.when = when;
535 event.deviceId = deviceId;
536 event.type = type;
537 event.code = code;
538 event.value = value;
539 mEvents.push_back(event);
540
541 if (type == EV_ABS) {
542 setAbsoluteAxisValue(deviceId, code, value);
543 }
544 }
545
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600546 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
547 std::vector<TouchVideoFrame>> videoFrames) {
548 mVideoFrames = std::move(videoFrames);
549 }
550
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700552 std::unique_lock<std::mutex> lock(mLock);
553 base::ScopedLockAssertion assumeLocked(mLock);
554 const bool queueIsEmpty =
555 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
556 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
557 if (!queueIsEmpty) {
558 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
559 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560 }
561
562private:
563 Device* getDevice(int32_t deviceId) const {
564 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100565 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 }
567
568 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
569 Device* device = getDevice(deviceId);
570 return device ? device->classes : 0;
571 }
572
573 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
574 Device* device = getDevice(deviceId);
575 return device ? device->identifier : InputDeviceIdentifier();
576 }
577
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100578 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 return 0;
580 }
581
582 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
583 Device* device = getDevice(deviceId);
584 if (device) {
585 *outConfiguration = device->configuration;
586 }
587 }
588
589 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
590 RawAbsoluteAxisInfo* outAxisInfo) const {
591 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800592 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 ssize_t index = device->absoluteAxes.indexOfKey(axis);
594 if (index >= 0) {
595 *outAxisInfo = device->absoluteAxes.valueAt(index);
596 return OK;
597 }
598 }
599 outAxisInfo->clear();
600 return -1;
601 }
602
603 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
604 Device* device = getDevice(deviceId);
605 if (device) {
606 return device->relativeAxes.indexOfKey(axis) >= 0;
607 }
608 return false;
609 }
610
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100611 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 return false;
613 }
614
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700615 virtual status_t mapKey(int32_t deviceId,
616 int32_t scanCode, int32_t usageCode, int32_t metaState,
617 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 Device* device = getDevice(deviceId);
619 if (device) {
620 const KeyInfo* key = getKey(device, scanCode, usageCode);
621 if (key) {
622 if (outKeycode) {
623 *outKeycode = key->keyCode;
624 }
625 if (outFlags) {
626 *outFlags = key->flags;
627 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700628 if (outMetaState) {
629 *outMetaState = metaState;
630 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800631 return OK;
632 }
633 }
634 return NAME_NOT_FOUND;
635 }
636
637 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
638 if (usageCode) {
639 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
640 if (index >= 0) {
641 return &device->keysByUsageCode.valueAt(index);
642 }
643 }
644 if (scanCode) {
645 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
646 if (index >= 0) {
647 return &device->keysByScanCode.valueAt(index);
648 }
649 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700650 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 }
652
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100653 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654 return NAME_NOT_FOUND;
655 }
656
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100657 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 mExcludedDevices = devices;
659 }
660
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100661 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700662 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 if (mEvents.empty()) {
664 return 0;
665 }
666
667 *buffer = *mEvents.begin();
668 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700669 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 return 1;
671 }
672
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800673 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600674 auto it = mVideoFrames.find(deviceId);
675 if (it != mVideoFrames.end()) {
676 std::vector<TouchVideoFrame> frames = std::move(it->second);
677 mVideoFrames.erase(deviceId);
678 return frames;
679 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800680 return {};
681 }
682
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
684 Device* device = getDevice(deviceId);
685 if (device) {
686 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
687 if (index >= 0) {
688 return device->scanCodeStates.valueAt(index);
689 }
690 }
691 return AKEY_STATE_UNKNOWN;
692 }
693
694 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
695 Device* device = getDevice(deviceId);
696 if (device) {
697 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
698 if (index >= 0) {
699 return device->keyCodeStates.valueAt(index);
700 }
701 }
702 return AKEY_STATE_UNKNOWN;
703 }
704
705 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
706 Device* device = getDevice(deviceId);
707 if (device) {
708 ssize_t index = device->switchStates.indexOfKey(sw);
709 if (index >= 0) {
710 return device->switchStates.valueAt(index);
711 }
712 }
713 return AKEY_STATE_UNKNOWN;
714 }
715
716 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
717 int32_t* outValue) const {
718 Device* device = getDevice(deviceId);
719 if (device) {
720 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
721 if (index >= 0) {
722 *outValue = device->absoluteAxisValue.valueAt(index);
723 return OK;
724 }
725 }
726 *outValue = 0;
727 return -1;
728 }
729
730 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
731 uint8_t* outFlags) const {
732 bool result = false;
733 Device* device = getDevice(deviceId);
734 if (device) {
735 for (size_t i = 0; i < numCodes; i++) {
736 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
737 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
738 outFlags[i] = 1;
739 result = true;
740 }
741 }
742 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
743 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
744 outFlags[i] = 1;
745 result = true;
746 }
747 }
748 }
749 }
750 return result;
751 }
752
753 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
754 Device* device = getDevice(deviceId);
755 if (device) {
756 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
757 return index >= 0;
758 }
759 return false;
760 }
761
762 virtual bool hasLed(int32_t deviceId, int32_t led) const {
763 Device* device = getDevice(deviceId);
764 return device && device->leds.indexOfKey(led) >= 0;
765 }
766
767 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
768 Device* device = getDevice(deviceId);
769 if (device) {
770 ssize_t index = device->leds.indexOfKey(led);
771 if (index >= 0) {
772 device->leds.replaceValueAt(led, on);
773 } else {
774 ADD_FAILURE()
775 << "Attempted to set the state of an LED that the EventHub declared "
776 "was not present. led=" << led;
777 }
778 }
779 }
780
781 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800782 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 outVirtualKeys.clear();
784
785 Device* device = getDevice(deviceId);
786 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800787 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788 }
789 }
790
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100791 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700792 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793 }
794
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100795 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796 return false;
797 }
798
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100799 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 }
801
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100802 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803 }
804
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100805 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806 return false;
807 }
808
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800809 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811
812 virtual void monitor() {
813 }
814
815 virtual void requestReopenDevices() {
816 }
817
818 virtual void wake() {
819 }
820};
821
822
823// --- FakeInputReaderContext ---
824
825class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700826 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 sp<InputReaderPolicyInterface> mPolicy;
828 sp<InputListenerInterface> mListener;
829 int32_t mGlobalMetaState;
830 bool mUpdateGlobalMetaStateWasCalled;
831 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800832 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833
834public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700835 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
836 const sp<InputReaderPolicyInterface>& policy,
837 const sp<InputListenerInterface>& listener)
838 : mEventHub(eventHub),
839 mPolicy(policy),
840 mListener(listener),
841 mGlobalMetaState(0),
842 mNextSequenceNum(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843
844 virtual ~FakeInputReaderContext() { }
845
846 void assertUpdateGlobalMetaStateWasCalled() {
847 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
848 << "Expected updateGlobalMetaState() to have been called.";
849 mUpdateGlobalMetaStateWasCalled = false;
850 }
851
852 void setGlobalMetaState(int32_t state) {
853 mGlobalMetaState = state;
854 }
855
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800856 uint32_t getGeneration() {
857 return mGeneration;
858 }
859
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860private:
861 virtual void updateGlobalMetaState() {
862 mUpdateGlobalMetaStateWasCalled = true;
863 }
864
865 virtual int32_t getGlobalMetaState() {
866 return mGlobalMetaState;
867 }
868
869 virtual EventHubInterface* getEventHub() {
870 return mEventHub.get();
871 }
872
873 virtual InputReaderPolicyInterface* getPolicy() {
874 return mPolicy.get();
875 }
876
877 virtual InputListenerInterface* getListener() {
878 return mListener.get();
879 }
880
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100881 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882 }
883
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100884 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885 return false;
886 }
887
888 virtual void fadePointer() {
889 }
890
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100891 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 }
893
894 virtual int32_t bumpGeneration() {
895 return ++mGeneration;
896 }
Michael Wright842500e2015-03-13 17:32:02 -0700897
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800898 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700899
900 }
901
902 virtual void dispatchExternalStylusState(const StylusState&) {
903
904 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800905
906 virtual uint32_t getNextSequenceNum() {
907 return mNextSequenceNum++;
908 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909};
910
911
912// --- FakeInputMapper ---
913
914class FakeInputMapper : public InputMapper {
915 uint32_t mSources;
916 int32_t mKeyboardType;
917 int32_t mMetaState;
918 KeyedVector<int32_t, int32_t> mKeyCodeStates;
919 KeyedVector<int32_t, int32_t> mScanCodeStates;
920 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800921 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700923 std::mutex mLock;
924 std::condition_variable mStateChangedCondition;
925 bool mConfigureWasCalled GUARDED_BY(mLock);
926 bool mResetWasCalled GUARDED_BY(mLock);
927 bool mProcessWasCalled GUARDED_BY(mLock);
928 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
Arthur Hungc23540e2018-11-29 20:42:11 +0800930 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931public:
932 FakeInputMapper(InputDevice* device, uint32_t sources) :
933 InputMapper(device),
934 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
935 mMetaState(0),
936 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
937 }
938
939 virtual ~FakeInputMapper() { }
940
941 void setKeyboardType(int32_t keyboardType) {
942 mKeyboardType = keyboardType;
943 }
944
945 void setMetaState(int32_t metaState) {
946 mMetaState = metaState;
947 }
948
949 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700950 std::unique_lock<std::mutex> lock(mLock);
951 base::ScopedLockAssertion assumeLocked(mLock);
952 const bool configureCalled =
953 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
954 return mConfigureWasCalled;
955 });
956 if (!configureCalled) {
957 FAIL() << "Expected configure() to have been called.";
958 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 mConfigureWasCalled = false;
960 }
961
962 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700963 std::unique_lock<std::mutex> lock(mLock);
964 base::ScopedLockAssertion assumeLocked(mLock);
965 const bool resetCalled =
966 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
967 return mResetWasCalled;
968 });
969 if (!resetCalled) {
970 FAIL() << "Expected reset() to have been called.";
971 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972 mResetWasCalled = false;
973 }
974
Yi Kong9b14ac62018-07-17 13:48:38 -0700975 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700976 std::unique_lock<std::mutex> lock(mLock);
977 base::ScopedLockAssertion assumeLocked(mLock);
978 const bool processCalled =
979 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
980 return mProcessWasCalled;
981 });
982 if (!processCalled) {
983 FAIL() << "Expected process() to have been called.";
984 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 if (outLastEvent) {
986 *outLastEvent = mLastEvent;
987 }
988 mProcessWasCalled = false;
989 }
990
991 void setKeyCodeState(int32_t keyCode, int32_t state) {
992 mKeyCodeStates.replaceValueFor(keyCode, state);
993 }
994
995 void setScanCodeState(int32_t scanCode, int32_t state) {
996 mScanCodeStates.replaceValueFor(scanCode, state);
997 }
998
999 void setSwitchState(int32_t switchCode, int32_t state) {
1000 mSwitchStates.replaceValueFor(switchCode, state);
1001 }
1002
1003 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001004 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005 }
1006
1007private:
1008 virtual uint32_t getSources() {
1009 return mSources;
1010 }
1011
1012 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1013 InputMapper::populateDeviceInfo(deviceInfo);
1014
1015 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1016 deviceInfo->setKeyboardType(mKeyboardType);
1017 }
1018 }
1019
Arthur Hungc23540e2018-11-29 20:42:11 +08001020 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001021 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001023
1024 // Find the associated viewport if exist.
1025 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
1026 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1027 mViewport = config->getDisplayViewportByPort(*displayPort);
1028 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001029
1030 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 }
1032
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001033 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001034 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001035 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001036 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001037 }
1038
1039 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001040 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001041 mLastEvent = *rawEvent;
1042 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001043 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001044 }
1045
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001046 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1048 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1049 }
1050
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001051 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1053 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1054 }
1055
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001056 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001057 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1058 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1059 }
1060
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001061 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062 const int32_t* keyCodes, uint8_t* outFlags) {
1063 bool result = false;
1064 for (size_t i = 0; i < numCodes; i++) {
1065 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1066 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1067 outFlags[i] = 1;
1068 result = true;
1069 }
1070 }
1071 }
1072 return result;
1073 }
1074
1075 virtual int32_t getMetaState() {
1076 return mMetaState;
1077 }
1078
1079 virtual void fadePointer() {
1080 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001081
1082 virtual std::optional<int32_t> getAssociatedDisplay() {
1083 if (mViewport) {
1084 return std::make_optional(mViewport->displayId);
1085 }
1086 return std::nullopt;
1087 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001088};
1089
1090
1091// --- InstrumentedInputReader ---
1092
1093class InstrumentedInputReader : public InputReader {
1094 InputDevice* mNextDevice;
1095
1096public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001097 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1098 const sp<InputReaderPolicyInterface>& policy,
1099 const sp<InputListenerInterface>& listener)
1100 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101
1102 virtual ~InstrumentedInputReader() {
1103 if (mNextDevice) {
1104 delete mNextDevice;
1105 }
1106 }
1107
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001108 void setNextDevice(InputDevice* device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001110 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001111 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112 InputDeviceIdentifier identifier;
1113 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001114 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115 int32_t generation = deviceId + 1;
1116 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001117 classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118 }
1119
Prabir Pradhan28efc192019-11-05 01:10:04 +00001120 // Make the protected loopOnce method accessible to tests.
1121 using InputReader::loopOnce;
1122
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123protected:
1124 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001125 const InputDeviceIdentifier& identifier,
1126 uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127 if (mNextDevice) {
1128 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001129 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 return device;
1131 }
1132 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1133 }
1134
1135 friend class InputReaderTest;
1136};
1137
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001138// --- InputReaderPolicyTest ---
1139class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001140protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001141 sp<FakeInputReaderPolicy> mFakePolicy;
1142
Prabir Pradhan28efc192019-11-05 01:10:04 +00001143 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1144 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001145};
1146
1147/**
1148 * Check that empty set of viewports is an acceptable configuration.
1149 * Also try to get internal viewport two different ways - by type and by uniqueId.
1150 *
1151 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1152 * Such configuration is not currently allowed.
1153 */
1154TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001155 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001156
1157 // We didn't add any viewports yet, so there shouldn't be any.
1158 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001159 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001160 ASSERT_FALSE(internalViewport);
1161
1162 // Add an internal viewport, then clear it
1163 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001164 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001165
1166 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001167 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001168 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001169 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001170
1171 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001172 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001173 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001174 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001175
1176 mFakePolicy->clearViewports();
1177 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001178 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001179 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001180 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001181 ASSERT_FALSE(internalViewport);
1182}
1183
1184TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1185 const std::string internalUniqueId = "local:0";
1186 const std::string externalUniqueId = "local:1";
1187 const std::string virtualUniqueId1 = "virtual:2";
1188 const std::string virtualUniqueId2 = "virtual:3";
1189 constexpr int32_t virtualDisplayId1 = 2;
1190 constexpr int32_t virtualDisplayId2 = 3;
1191
1192 // Add an internal viewport
1193 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001194 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001195 // Add an external viewport
1196 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001197 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001198 // Add an virtual viewport
1199 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001200 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001201 // Add another virtual viewport
1202 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001203 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001204
1205 // Check matching by type for internal
1206 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001207 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001208 ASSERT_TRUE(internalViewport);
1209 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1210
1211 // Check matching by type for external
1212 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001213 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001214 ASSERT_TRUE(externalViewport);
1215 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1216
1217 // Check matching by uniqueId for virtual viewport #1
1218 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001219 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001220 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001221 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001222 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1223 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1224
1225 // Check matching by uniqueId for virtual viewport #2
1226 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001227 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001228 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001229 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001230 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1231 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1232}
1233
1234
1235/**
1236 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1237 * that lookup works by checking display id.
1238 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1239 */
1240TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1241 const std::string uniqueId1 = "uniqueId1";
1242 const std::string uniqueId2 = "uniqueId2";
1243 constexpr int32_t displayId1 = 2;
1244 constexpr int32_t displayId2 = 3;
1245
1246 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1247 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1248 for (const ViewportType& type : types) {
1249 mFakePolicy->clearViewports();
1250 // Add a viewport
1251 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001252 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001253 // Add another viewport
1254 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001255 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001256
1257 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001258 std::optional<DisplayViewport> viewport1 =
1259 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001260 ASSERT_TRUE(viewport1);
1261 ASSERT_EQ(displayId1, viewport1->displayId);
1262 ASSERT_EQ(type, viewport1->type);
1263
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001264 std::optional<DisplayViewport> viewport2 =
1265 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001266 ASSERT_TRUE(viewport2);
1267 ASSERT_EQ(displayId2, viewport2->displayId);
1268 ASSERT_EQ(type, viewport2->type);
1269
1270 // When there are multiple viewports of the same kind, and uniqueId is not specified
1271 // in the call to getDisplayViewport, then that situation is not supported.
1272 // The viewports can be stored in any order, so we cannot rely on the order, since that
1273 // is just implementation detail.
1274 // However, we can check that it still returns *a* viewport, we just cannot assert
1275 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001276 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001277 ASSERT_TRUE(someViewport);
1278 }
1279}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001281/**
1282 * Check getDisplayViewportByPort
1283 */
1284TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1285 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1286 const std::string uniqueId1 = "uniqueId1";
1287 const std::string uniqueId2 = "uniqueId2";
1288 constexpr int32_t displayId1 = 1;
1289 constexpr int32_t displayId2 = 2;
1290 const uint8_t hdmi1 = 0;
1291 const uint8_t hdmi2 = 1;
1292 const uint8_t hdmi3 = 2;
1293
1294 mFakePolicy->clearViewports();
1295 // Add a viewport that's associated with some display port that's not of interest.
1296 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1297 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1298 // Add another viewport, connected to HDMI1 port
1299 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1300 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1301
1302 // Check that correct display viewport was returned by comparing the display ports.
1303 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1304 ASSERT_TRUE(hdmi1Viewport);
1305 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1306 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1307
1308 // Check that we can still get the same viewport using the uniqueId
1309 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1310 ASSERT_TRUE(hdmi1Viewport);
1311 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1312 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1313 ASSERT_EQ(type, hdmi1Viewport->type);
1314
1315 // Check that we cannot find a port with "HDMI2", because we never added one
1316 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1317 ASSERT_FALSE(hdmi2Viewport);
1318}
1319
Michael Wrightd02c5b62014-02-10 15:10:22 -08001320// --- InputReaderTest ---
1321
1322class InputReaderTest : public testing::Test {
1323protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001324 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001326 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001327 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328
Prabir Pradhan28efc192019-11-05 01:10:04 +00001329 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001330 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001331 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001332 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001333
Prabir Pradhan28efc192019-11-05 01:10:04 +00001334 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1335 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001336 }
1337
Prabir Pradhan28efc192019-11-05 01:10:04 +00001338 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001339 mFakeListener.clear();
1340 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001341 }
1342
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001343 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001344 const PropertyMap* configuration) {
1345 mFakeEventHub->addDevice(deviceId, name, classes);
1346
1347 if (configuration) {
1348 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1349 }
1350 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001351 mReader->loopOnce();
1352 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001353 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1354 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001355 }
1356
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001357 void disableDevice(int32_t deviceId, InputDevice* device) {
1358 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001359 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001360 }
1361
1362 void enableDevice(int32_t deviceId, InputDevice* device) {
1363 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001364 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001365 }
1366
Michael Wrightd02c5b62014-02-10 15:10:22 -08001367 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001368 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001369 const PropertyMap* configuration) {
1370 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1371 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1372 device->addMapper(mapper);
1373 mReader->setNextDevice(device);
1374 addDevice(deviceId, name, classes, configuration);
1375 return mapper;
1376 }
1377};
1378
1379TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001380 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001381 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001382 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001383 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001385 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001387 ASSERT_EQ(1U, inputDevices.size());
1388 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001389 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001390 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1391 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1392 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1393
1394 // Should also have received a notification describing the new input devices.
1395 inputDevices = mFakePolicy->getInputDevices();
1396 ASSERT_EQ(1U, inputDevices.size());
1397 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001398 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1400 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1401 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1402}
1403
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001404TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1405 constexpr int32_t deviceId = 1;
1406 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001407 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001408 // Must add at least one mapper or the device will be ignored!
1409 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1410 device->addMapper(mapper);
1411 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001412 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001413
Yi Kong9b14ac62018-07-17 13:48:38 -07001414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001415
1416 NotifyDeviceResetArgs resetArgs;
1417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001418 ASSERT_EQ(deviceId, resetArgs.deviceId);
1419
1420 ASSERT_EQ(device->isEnabled(), true);
1421 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001422 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001423
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001425 ASSERT_EQ(deviceId, resetArgs.deviceId);
1426 ASSERT_EQ(device->isEnabled(), false);
1427
1428 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001429 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001432 ASSERT_EQ(device->isEnabled(), false);
1433
1434 enableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001435 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001437 ASSERT_EQ(deviceId, resetArgs.deviceId);
1438 ASSERT_EQ(device->isEnabled(), true);
1439}
1440
Michael Wrightd02c5b62014-02-10 15:10:22 -08001441TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001442 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001443 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001444 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001445 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1446
1447 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1448 AINPUT_SOURCE_ANY, AKEYCODE_A))
1449 << "Should return unknown when the device id is >= 0 but unknown.";
1450
1451 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1452 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1453 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1454
1455 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1456 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1457 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1458
1459 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1460 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1461 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1462
1463 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1464 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1465 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1466}
1467
1468TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001469 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001470 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001471 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001472 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1473
1474 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1475 AINPUT_SOURCE_ANY, KEY_A))
1476 << "Should return unknown when the device id is >= 0 but unknown.";
1477
1478 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1479 AINPUT_SOURCE_TRACKBALL, KEY_A))
1480 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1481
1482 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1483 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1484 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1485
1486 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1487 AINPUT_SOURCE_TRACKBALL, KEY_A))
1488 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1489
1490 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1491 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1492 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1493}
1494
1495TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001496 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001497 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001498 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001499 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1500
1501 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1502 AINPUT_SOURCE_ANY, SW_LID))
1503 << "Should return unknown when the device id is >= 0 but unknown.";
1504
1505 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1506 AINPUT_SOURCE_TRACKBALL, SW_LID))
1507 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1508
1509 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1510 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1511 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1512
1513 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1514 AINPUT_SOURCE_TRACKBALL, SW_LID))
1515 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1516
1517 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1518 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1519 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1520}
1521
1522TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001523 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001524 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001525 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001526
Michael Wrightd02c5b62014-02-10 15:10:22 -08001527 mapper->addSupportedKeyCode(AKEYCODE_A);
1528 mapper->addSupportedKeyCode(AKEYCODE_B);
1529
1530 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1531 uint8_t flags[4] = { 0, 0, 0, 1 };
1532
1533 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1534 << "Should return false when device id is >= 0 but unknown.";
1535 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1536
1537 flags[3] = 1;
1538 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1539 << "Should return false when device id is valid but the sources are not supported by the device.";
1540 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1541
1542 flags[3] = 1;
1543 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1544 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1545 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1546
1547 flags[3] = 1;
1548 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1549 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1550 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1551
1552 flags[3] = 1;
1553 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1554 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1555 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1556}
1557
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001558TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001559 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001560
1561 NotifyConfigurationChangedArgs args;
1562
1563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1564 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1565}
1566
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001567TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001568 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001569 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001570 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571
1572 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001573 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001574 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1575
1576 RawEvent event;
1577 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1578 ASSERT_EQ(0, event.when);
1579 ASSERT_EQ(1, event.deviceId);
1580 ASSERT_EQ(EV_KEY, event.type);
1581 ASSERT_EQ(KEY_A, event.code);
1582 ASSERT_EQ(1, event.value);
1583}
1584
Prabir Pradhan42611e02018-11-27 14:04:02 -08001585TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1586 constexpr int32_t deviceId = 1;
1587 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001588 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001589 // Must add at least one mapper or the device will be ignored!
1590 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1591 device->addMapper(mapper);
1592 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001593 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001594
1595 NotifyDeviceResetArgs resetArgs;
1596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1597 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1598
1599 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001600 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001602 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1603 prevSequenceNum = resetArgs.sequenceNum;
1604
1605 enableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001606 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001608 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1609 prevSequenceNum = resetArgs.sequenceNum;
1610
1611 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001612 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001614 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1615 prevSequenceNum = resetArgs.sequenceNum;
1616}
1617
Arthur Hungc23540e2018-11-29 20:42:11 +08001618TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1619 constexpr int32_t deviceId = 1;
1620 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1621 const char* DEVICE_LOCATION = "USB1";
1622 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1623 DEVICE_LOCATION);
1624 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1625 device->addMapper(mapper);
1626 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001627
1628 const uint8_t hdmi1 = 1;
1629
1630 // Associated touch screen with second display.
1631 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1632
1633 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001634 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001635 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1636 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1637 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1638 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1639 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001640 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001641
1642 // Add the device, and make sure all of the callbacks are triggered.
1643 // The device is added after the input port associations are processed since
1644 // we do not yet support dynamic device-to-display associations.
1645 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
1648 ASSERT_NO_FATAL_FAILURE(mapper->assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001649
Arthur Hung2c9a3342019-07-23 14:18:59 +08001650 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001651 ASSERT_EQ(deviceId, device->getId());
1652 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1653 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001654
1655 // Can't dispatch event from a disabled device.
1656 disableDevice(deviceId, device);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001657 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001658 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001659}
1660
Michael Wrightd02c5b62014-02-10 15:10:22 -08001661
1662// --- InputDeviceTest ---
1663
1664class InputDeviceTest : public testing::Test {
1665protected:
1666 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001667 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001668 static const int32_t DEVICE_ID;
1669 static const int32_t DEVICE_GENERATION;
1670 static const int32_t DEVICE_CONTROLLER_NUMBER;
1671 static const uint32_t DEVICE_CLASSES;
1672
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001673 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001674 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001675 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001676 FakeInputReaderContext* mFakeContext;
1677
1678 InputDevice* mDevice;
1679
Prabir Pradhan28efc192019-11-05 01:10:04 +00001680 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001681 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001683 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001684 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1685
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001686 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001687 InputDeviceIdentifier identifier;
1688 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001689 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001690 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1691 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1692 }
1693
Prabir Pradhan28efc192019-11-05 01:10:04 +00001694 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001695 delete mDevice;
1696
1697 delete mFakeContext;
1698 mFakeListener.clear();
1699 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001700 }
1701};
1702
1703const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001704const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001705const int32_t InputDeviceTest::DEVICE_ID = 1;
1706const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1707const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1708const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1709 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1710
1711TEST_F(InputDeviceTest, ImmutableProperties) {
1712 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001713 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1715}
1716
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001717TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1718 ASSERT_EQ(mDevice->isEnabled(), true);
1719}
1720
Michael Wrightd02c5b62014-02-10 15:10:22 -08001721TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1722 // Configuration.
1723 InputReaderConfiguration config;
1724 mDevice->configure(ARBITRARY_TIME, &config, 0);
1725
1726 // Reset.
1727 mDevice->reset(ARBITRARY_TIME);
1728
1729 NotifyDeviceResetArgs resetArgs;
1730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1731 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1732 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1733
1734 // Metadata.
1735 ASSERT_TRUE(mDevice->isIgnored());
1736 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1737
1738 InputDeviceInfo info;
1739 mDevice->getDeviceInfo(&info);
1740 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001741 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001742 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1743 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1744
1745 // State queries.
1746 ASSERT_EQ(0, mDevice->getMetaState());
1747
1748 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1749 << "Ignored device should return unknown key code state.";
1750 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1751 << "Ignored device should return unknown scan code state.";
1752 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1753 << "Ignored device should return unknown switch state.";
1754
1755 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1756 uint8_t flags[2] = { 0, 1 };
1757 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1758 << "Ignored device should never mark any key codes.";
1759 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1760 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1761}
1762
1763TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1764 // Configuration.
1765 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1766
1767 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1768 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1769 mapper1->setMetaState(AMETA_ALT_ON);
1770 mapper1->addSupportedKeyCode(AKEYCODE_A);
1771 mapper1->addSupportedKeyCode(AKEYCODE_B);
1772 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1773 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1774 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1775 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1776 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1777 mDevice->addMapper(mapper1);
1778
1779 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1780 mapper2->setMetaState(AMETA_SHIFT_ON);
1781 mDevice->addMapper(mapper2);
1782
1783 InputReaderConfiguration config;
1784 mDevice->configure(ARBITRARY_TIME, &config, 0);
1785
1786 String8 propertyValue;
1787 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1788 << "Device should have read configuration during configuration phase.";
1789 ASSERT_STREQ("value", propertyValue.string());
1790
1791 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1792 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1793
1794 // Reset
1795 mDevice->reset(ARBITRARY_TIME);
1796 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1797 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1798
1799 NotifyDeviceResetArgs resetArgs;
1800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1801 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1802 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1803
1804 // Metadata.
1805 ASSERT_FALSE(mDevice->isIgnored());
1806 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1807
1808 InputDeviceInfo info;
1809 mDevice->getDeviceInfo(&info);
1810 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001811 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001812 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1813 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1814
1815 // State queries.
1816 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1817 << "Should query mappers and combine meta states.";
1818
1819 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1820 << "Should return unknown key code state when source not supported.";
1821 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1822 << "Should return unknown scan code state when source not supported.";
1823 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1824 << "Should return unknown switch state when source not supported.";
1825
1826 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1827 << "Should query mapper when source is supported.";
1828 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1829 << "Should query mapper when source is supported.";
1830 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1831 << "Should query mapper when source is supported.";
1832
1833 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1834 uint8_t flags[4] = { 0, 0, 0, 1 };
1835 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1836 << "Should do nothing when source is unsupported.";
1837 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1838 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1839 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1840 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1841
1842 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1843 << "Should query mapper when source is supported.";
1844 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1845 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1846 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1847 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1848
1849 // Event handling.
1850 RawEvent event;
1851 mDevice->process(&event, 1);
1852
1853 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1854 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1855}
1856
Arthur Hung2c9a3342019-07-23 14:18:59 +08001857// A single input device is associated with a specific display. Check that:
1858// 1. Device is disabled if the viewport corresponding to the associated display is not found
1859// 2. Device is disabled when setEnabled API is called
1860TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
1861 FakeInputMapper* mapper = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1862 mDevice->addMapper(mapper);
1863
1864 // First Configuration.
1865 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1866
1867 // Device should be enabled by default.
1868 ASSERT_TRUE(mDevice->isEnabled());
1869
1870 // Prepare associated info.
1871 constexpr uint8_t hdmi = 1;
1872 const std::string UNIQUE_ID = "local:1";
1873
1874 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1875 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1876 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1877 // Device should be disabled because it is associated with a specific display via
1878 // input port <-> display port association, but the corresponding display is not found
1879 ASSERT_FALSE(mDevice->isEnabled());
1880
1881 // Prepare displays.
1882 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1883 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1884 ViewportType::VIEWPORT_INTERNAL);
1885 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1886 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1887 ASSERT_TRUE(mDevice->isEnabled());
1888
1889 // Device should be disabled after set disable.
1890 mFakePolicy->addDisabledDevice(mDevice->getId());
1891 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1892 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1893 ASSERT_FALSE(mDevice->isEnabled());
1894
1895 // Device should still be disabled even found the associated display.
1896 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1897 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1898 ASSERT_FALSE(mDevice->isEnabled());
1899}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001900
1901// --- InputMapperTest ---
1902
1903class InputMapperTest : public testing::Test {
1904protected:
1905 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001906 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001907 static const int32_t DEVICE_ID;
1908 static const int32_t DEVICE_GENERATION;
1909 static const int32_t DEVICE_CONTROLLER_NUMBER;
1910 static const uint32_t DEVICE_CLASSES;
1911
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001912 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001913 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001914 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915 FakeInputReaderContext* mFakeContext;
1916 InputDevice* mDevice;
1917
Prabir Pradhan28efc192019-11-05 01:10:04 +00001918 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001919 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001920 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001921 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001922 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1923 InputDeviceIdentifier identifier;
1924 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001925 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001926 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1927 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1928
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001929 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930 }
1931
Prabir Pradhan28efc192019-11-05 01:10:04 +00001932 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001933 delete mDevice;
1934 delete mFakeContext;
1935 mFakeListener.clear();
1936 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001937 }
1938
1939 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001940 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001941 }
1942
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001943 void configureDevice(uint32_t changes) {
1944 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1945 }
1946
Michael Wrightd02c5b62014-02-10 15:10:22 -08001947 void addMapperAndConfigure(InputMapper* mapper) {
1948 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001949 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001950 mDevice->reset(ARBITRARY_TIME);
1951 }
1952
1953 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001954 int32_t orientation, const std::string& uniqueId,
1955 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001956 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001957 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001958 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1959 }
1960
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001961 void clearViewports() {
1962 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001963 }
1964
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001965 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001966 int32_t code, int32_t value) {
1967 RawEvent event;
1968 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001969 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001970 event.type = type;
1971 event.code = code;
1972 event.value = value;
1973 mapper->process(&event);
1974 }
1975
1976 static void assertMotionRange(const InputDeviceInfo& info,
1977 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1978 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001979 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001980 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1981 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1982 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1983 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1984 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1985 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1986 }
1987
1988 static void assertPointerCoords(const PointerCoords& coords,
1989 float x, float y, float pressure, float size,
1990 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1991 float orientation, float distance) {
1992 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1993 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1994 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1995 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1996 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1997 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1998 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1999 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2000 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2001 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2002 }
2003
2004 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2005 float actualX, actualY;
2006 controller->getPosition(&actualX, &actualY);
2007 ASSERT_NEAR(x, actualX, 1);
2008 ASSERT_NEAR(y, actualY, 1);
2009 }
2010};
2011
2012const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002013const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002014const int32_t InputMapperTest::DEVICE_ID = 1;
2015const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2016const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2017const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
2018
2019
2020// --- SwitchInputMapperTest ---
2021
2022class SwitchInputMapperTest : public InputMapperTest {
2023protected:
2024};
2025
2026TEST_F(SwitchInputMapperTest, GetSources) {
2027 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2028 addMapperAndConfigure(mapper);
2029
2030 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
2031}
2032
2033TEST_F(SwitchInputMapperTest, GetSwitchState) {
2034 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2035 addMapperAndConfigure(mapper);
2036
2037 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
2038 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2039
2040 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
2041 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2042}
2043
2044TEST_F(SwitchInputMapperTest, Process) {
2045 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2046 addMapperAndConfigure(mapper);
2047
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002048 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2049 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2050 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2051 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002052
2053 NotifySwitchArgs args;
2054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2055 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002056 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2057 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002058 args.switchMask);
2059 ASSERT_EQ(uint32_t(0), args.policyFlags);
2060}
2061
2062
2063// --- KeyboardInputMapperTest ---
2064
2065class KeyboardInputMapperTest : public InputMapperTest {
2066protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002067 const std::string UNIQUE_ID = "local:0";
2068
2069 void prepareDisplay(int32_t orientation);
2070
Arthur Hung2c9a3342019-07-23 14:18:59 +08002071 void testDPadKeyRotation(KeyboardInputMapper* mapper, int32_t originalScanCode,
2072 int32_t originalKeyCode, int32_t rotatedKeyCode,
2073 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002074};
2075
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002076/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2077 * orientation.
2078 */
2079void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2080 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002081 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002082}
2083
Michael Wrightd02c5b62014-02-10 15:10:22 -08002084void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002085 int32_t originalScanCode, int32_t originalKeyCode,
2086 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002087 NotifyKeyArgs args;
2088
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002089 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2091 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2092 ASSERT_EQ(originalScanCode, args.scanCode);
2093 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002094 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002095
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002096 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2098 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2099 ASSERT_EQ(originalScanCode, args.scanCode);
2100 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002101 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002102}
2103
Michael Wrightd02c5b62014-02-10 15:10:22 -08002104TEST_F(KeyboardInputMapperTest, GetSources) {
2105 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2106 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2107 addMapperAndConfigure(mapper);
2108
2109 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
2110}
2111
2112TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2113 const int32_t USAGE_A = 0x070004;
2114 const int32_t USAGE_UNKNOWN = 0x07ffff;
2115 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2116 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2117
2118 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2119 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2120 addMapperAndConfigure(mapper);
2121
2122 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002123 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002124 NotifyKeyArgs args;
2125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2126 ASSERT_EQ(DEVICE_ID, args.deviceId);
2127 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2128 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2129 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2130 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2131 ASSERT_EQ(KEY_HOME, args.scanCode);
2132 ASSERT_EQ(AMETA_NONE, args.metaState);
2133 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2134 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2135 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2136
2137 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002138 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2140 ASSERT_EQ(DEVICE_ID, args.deviceId);
2141 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2142 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2143 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2144 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2145 ASSERT_EQ(KEY_HOME, args.scanCode);
2146 ASSERT_EQ(AMETA_NONE, args.metaState);
2147 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2148 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2149 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2150
2151 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002152 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2153 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2155 ASSERT_EQ(DEVICE_ID, args.deviceId);
2156 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2157 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2158 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2159 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2160 ASSERT_EQ(0, args.scanCode);
2161 ASSERT_EQ(AMETA_NONE, args.metaState);
2162 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2163 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2164 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2165
2166 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002167 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2168 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2170 ASSERT_EQ(DEVICE_ID, args.deviceId);
2171 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2172 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2173 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2174 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2175 ASSERT_EQ(0, args.scanCode);
2176 ASSERT_EQ(AMETA_NONE, args.metaState);
2177 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2178 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2179 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2180
2181 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002182 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2183 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2185 ASSERT_EQ(DEVICE_ID, args.deviceId);
2186 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2187 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2188 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2189 ASSERT_EQ(0, args.keyCode);
2190 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2191 ASSERT_EQ(AMETA_NONE, args.metaState);
2192 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2193 ASSERT_EQ(0U, args.policyFlags);
2194 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2195
2196 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002197 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2198 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2200 ASSERT_EQ(DEVICE_ID, args.deviceId);
2201 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2202 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2203 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2204 ASSERT_EQ(0, args.keyCode);
2205 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2206 ASSERT_EQ(AMETA_NONE, args.metaState);
2207 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2208 ASSERT_EQ(0U, args.policyFlags);
2209 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2210}
2211
2212TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2213 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2214 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2215
2216 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2217 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2218 addMapperAndConfigure(mapper);
2219
2220 // Initial metastate.
2221 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2222
2223 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002224 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002225 NotifyKeyArgs args;
2226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2227 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2228 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2229 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2230
2231 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002232 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2234 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2235 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2236
2237 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002238 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2241 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2242
2243 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002244 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2246 ASSERT_EQ(AMETA_NONE, args.metaState);
2247 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2248 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2249}
2250
2251TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2252 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2253 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2254 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2255 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2256
2257 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2258 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2259 addMapperAndConfigure(mapper);
2260
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002261 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2263 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2264 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2265 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2266 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2267 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2268 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2269 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2270}
2271
2272TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2273 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2274 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2275 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2276 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2277
2278 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2279 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2280 addConfigurationProperty("keyboard.orientationAware", "1");
2281 addMapperAndConfigure(mapper);
2282
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002283 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002284 ASSERT_NO_FATAL_FAILURE(
2285 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2286 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2287 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2288 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2289 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2290 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2291 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002292
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002293 clearViewports();
2294 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002295 ASSERT_NO_FATAL_FAILURE(
2296 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2297 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2298 AKEYCODE_DPAD_UP, DISPLAY_ID));
2299 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2300 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2301 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2302 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002304 clearViewports();
2305 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002306 ASSERT_NO_FATAL_FAILURE(
2307 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2308 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2309 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2310 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2311 AKEYCODE_DPAD_UP, DISPLAY_ID));
2312 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2313 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002314
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002315 clearViewports();
2316 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002317 ASSERT_NO_FATAL_FAILURE(
2318 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2319 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2320 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2321 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2322 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2323 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2324 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325
2326 // Special case: if orientation changes while key is down, we still emit the same keycode
2327 // in the key up as we did in the key down.
2328 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002329 clearViewports();
2330 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002331 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2333 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2334 ASSERT_EQ(KEY_UP, args.scanCode);
2335 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2336
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002337 clearViewports();
2338 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002339 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2341 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2342 ASSERT_EQ(KEY_UP, args.scanCode);
2343 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2344}
2345
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002346TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2347 // If the keyboard is not orientation aware,
2348 // key events should not be associated with a specific display id
2349 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2350
2351 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2352 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2353 addMapperAndConfigure(mapper);
2354 NotifyKeyArgs args;
2355
2356 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002357 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002359 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2361 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2362
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002363 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002364 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002366 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2368 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2369}
2370
2371TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2372 // If the keyboard is orientation aware,
2373 // key events should be associated with the internal viewport
2374 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2375
2376 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2377 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2378 addConfigurationProperty("keyboard.orientationAware", "1");
2379 addMapperAndConfigure(mapper);
2380 NotifyKeyArgs args;
2381
2382 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2383 // ^--- already checked by the previous test
2384
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002385 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002386 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002387 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002389 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2391 ASSERT_EQ(DISPLAY_ID, args.displayId);
2392
2393 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002394 clearViewports();
2395 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002396 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002397 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002399 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2401 ASSERT_EQ(newDisplayId, args.displayId);
2402}
2403
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2405 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2406 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2407 addMapperAndConfigure(mapper);
2408
2409 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2410 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2411
2412 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2413 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2414}
2415
2416TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2417 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2418 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2419 addMapperAndConfigure(mapper);
2420
2421 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2422 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2423
2424 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2425 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2426}
2427
2428TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2429 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2430 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2431 addMapperAndConfigure(mapper);
2432
2433 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2434
2435 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2436 uint8_t flags[2] = { 0, 0 };
2437 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2438 ASSERT_TRUE(flags[0]);
2439 ASSERT_FALSE(flags[1]);
2440}
2441
2442TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2443 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2444 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2445 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2446 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2447 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2448 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2449
2450 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2451 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2452 addMapperAndConfigure(mapper);
2453
2454 // Initialization should have turned all of the lights off.
2455 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2456 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2457 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2458
2459 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002460 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2461 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002462 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2463 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2464 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2465 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2466
2467 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002468 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2469 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2471 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2472 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2473 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2474
2475 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002476 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2477 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002478 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2479 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2480 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2481 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2482
2483 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002484 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2485 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002486 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2487 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2488 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2489 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2490
2491 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002492 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2493 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2495 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2496 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2497 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2498
2499 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002500 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2501 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002502 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2503 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2504 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2505 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2506}
2507
Arthur Hung2c9a3342019-07-23 14:18:59 +08002508TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2509 // keyboard 1.
2510 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2511 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2512 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2513 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2514
2515 // keyboard 2.
2516 const std::string USB2 = "USB2";
2517 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2518 InputDeviceIdentifier identifier;
2519 identifier.name = "KEYBOARD2";
2520 identifier.location = USB2;
2521 std::unique_ptr<InputDevice> device2 =
2522 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2523 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2524 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2525 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2526 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2527 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2528 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2529
2530 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2531 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2532 addMapperAndConfigure(mapper);
2533
2534 KeyboardInputMapper* mapper2 = new KeyboardInputMapper(device2.get(), AINPUT_SOURCE_KEYBOARD,
2535 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2536 device2->addMapper(mapper2);
2537 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2538 device2->reset(ARBITRARY_TIME);
2539
2540 // Prepared displays and associated info.
2541 constexpr uint8_t hdmi1 = 0;
2542 constexpr uint8_t hdmi2 = 1;
2543 const std::string SECONDARY_UNIQUE_ID = "local:1";
2544
2545 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2546 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2547
2548 // No associated display viewport found, should disable the device.
2549 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2550 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2551 ASSERT_FALSE(device2->isEnabled());
2552
2553 // Prepare second display.
2554 constexpr int32_t newDisplayId = 2;
2555 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2556 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2557 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2558 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2559 // Default device will reconfigure above, need additional reconfiguration for another device.
2560 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2561 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2562
2563 // Device should be enabled after the associated display is found.
2564 ASSERT_TRUE(mDevice->isEnabled());
2565 ASSERT_TRUE(device2->isEnabled());
2566
2567 // Test pad key events
2568 ASSERT_NO_FATAL_FAILURE(
2569 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2570 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2571 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2572 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2573 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2575 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2576
2577 ASSERT_NO_FATAL_FAILURE(
2578 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2579 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2580 AKEYCODE_DPAD_RIGHT, newDisplayId));
2581 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2582 AKEYCODE_DPAD_DOWN, newDisplayId));
2583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2584 AKEYCODE_DPAD_LEFT, newDisplayId));
2585}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002586
Powei Fengd041c5d2019-05-03 17:11:33 -07002587TEST_F(KeyboardInputMapperTest, ExternalDevice_WakeBehavior) {
2588 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2589 // marked as WAKE in the keylayout file to trigger wake.
2590 mDevice->setExternal(true);
2591
2592 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2593 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2594 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE, POLICY_FLAG_WAKE);
2595
2596 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2597 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2598 addMapperAndConfigure(mapper);
2599
2600 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2601 NotifyKeyArgs args;
2602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2603 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2604
2605 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2607 ASSERT_EQ(uint32_t(0), args.policyFlags);
2608
2609 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2611 ASSERT_EQ(uint32_t(0), args.policyFlags);
2612
2613 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2615 ASSERT_EQ(uint32_t(0), args.policyFlags);
2616
2617 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2619 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2620
2621 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2623 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2624}
2625
2626TEST_F(KeyboardInputMapperTest, ExternalDevice_DoNotWakeByDefaultBehavior) {
2627 // Tv Remote key's wake behavior is prescribed by the keylayout file.
2628 mDevice->setExternal(true);
2629
2630 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2631 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2632 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
2633
2634 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2635 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2636 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
2637 addMapperAndConfigure(mapper);
2638
2639 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2640 NotifyKeyArgs args;
2641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2642 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2643
2644 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2646 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2647
2648 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2650 ASSERT_EQ(uint32_t(0), args.policyFlags);
2651
2652 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2654 ASSERT_EQ(uint32_t(0), args.policyFlags);
2655
2656 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2658 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2659
2660 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2662 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2663}
2664
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665// --- CursorInputMapperTest ---
2666
2667class CursorInputMapperTest : public InputMapperTest {
2668protected:
2669 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2670
2671 sp<FakePointerController> mFakePointerController;
2672
Prabir Pradhan28efc192019-11-05 01:10:04 +00002673 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 InputMapperTest::SetUp();
2675
2676 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002677 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678 }
2679
2680 void testMotionRotation(CursorInputMapper* mapper,
2681 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002682
2683 void prepareDisplay(int32_t orientation) {
2684 const std::string uniqueId = "local:0";
2685 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2686 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2687 orientation, uniqueId, NO_PORT, viewportType);
2688 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689};
2690
2691const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2692
2693void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2694 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2695 NotifyMotionArgs args;
2696
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002697 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2698 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2699 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2703 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2704 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2705 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2706}
2707
2708TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2709 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2710 addConfigurationProperty("cursor.mode", "pointer");
2711 addMapperAndConfigure(mapper);
2712
2713 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2714}
2715
2716TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2717 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2718 addConfigurationProperty("cursor.mode", "navigation");
2719 addMapperAndConfigure(mapper);
2720
2721 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2722}
2723
2724TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2725 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2726 addConfigurationProperty("cursor.mode", "pointer");
2727 addMapperAndConfigure(mapper);
2728
2729 InputDeviceInfo info;
2730 mapper->populateDeviceInfo(&info);
2731
2732 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002733 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2734 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002735 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2736 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2737
2738 // When the bounds are set, then there should be a valid motion range.
2739 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2740
2741 InputDeviceInfo info2;
2742 mapper->populateDeviceInfo(&info2);
2743
2744 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2745 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2746 1, 800 - 1, 0.0f, 0.0f));
2747 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2748 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2749 2, 480 - 1, 0.0f, 0.0f));
2750 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2751 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2752 0.0f, 1.0f, 0.0f, 0.0f));
2753}
2754
2755TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2756 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2757 addConfigurationProperty("cursor.mode", "navigation");
2758 addMapperAndConfigure(mapper);
2759
2760 InputDeviceInfo info;
2761 mapper->populateDeviceInfo(&info);
2762
2763 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2764 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2765 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2766 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2767 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2768 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2769 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2770 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2771 0.0f, 1.0f, 0.0f, 0.0f));
2772}
2773
2774TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2775 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2776 addConfigurationProperty("cursor.mode", "navigation");
2777 addMapperAndConfigure(mapper);
2778
2779 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2780
2781 NotifyMotionArgs args;
2782
2783 // Button press.
2784 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002785 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2786 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2788 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2789 ASSERT_EQ(DEVICE_ID, args.deviceId);
2790 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2791 ASSERT_EQ(uint32_t(0), args.policyFlags);
2792 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2793 ASSERT_EQ(0, args.flags);
2794 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2795 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2796 ASSERT_EQ(0, args.edgeFlags);
2797 ASSERT_EQ(uint32_t(1), args.pointerCount);
2798 ASSERT_EQ(0, args.pointerProperties[0].id);
2799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2801 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2802 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2803 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2804 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2805
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2807 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2808 ASSERT_EQ(DEVICE_ID, args.deviceId);
2809 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2810 ASSERT_EQ(uint32_t(0), args.policyFlags);
2811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2812 ASSERT_EQ(0, args.flags);
2813 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2814 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2815 ASSERT_EQ(0, args.edgeFlags);
2816 ASSERT_EQ(uint32_t(1), args.pointerCount);
2817 ASSERT_EQ(0, args.pointerProperties[0].id);
2818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2820 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2821 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2822 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2823 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2824
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002826 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2827 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2829 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2830 ASSERT_EQ(DEVICE_ID, args.deviceId);
2831 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2832 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2834 ASSERT_EQ(0, args.flags);
2835 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2836 ASSERT_EQ(0, args.buttonState);
2837 ASSERT_EQ(0, args.edgeFlags);
2838 ASSERT_EQ(uint32_t(1), args.pointerCount);
2839 ASSERT_EQ(0, args.pointerProperties[0].id);
2840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2842 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2843 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2844 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2845 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2846
2847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2848 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2849 ASSERT_EQ(DEVICE_ID, args.deviceId);
2850 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2851 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2853 ASSERT_EQ(0, args.flags);
2854 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2855 ASSERT_EQ(0, args.buttonState);
2856 ASSERT_EQ(0, args.edgeFlags);
2857 ASSERT_EQ(uint32_t(1), args.pointerCount);
2858 ASSERT_EQ(0, args.pointerProperties[0].id);
2859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2861 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2862 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2863 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2864 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2865}
2866
2867TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2868 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2869 addConfigurationProperty("cursor.mode", "navigation");
2870 addMapperAndConfigure(mapper);
2871
2872 NotifyMotionArgs args;
2873
2874 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002875 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2876 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2880 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2881
2882 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002883 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2884 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2888 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2889}
2890
2891TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2892 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2893 addConfigurationProperty("cursor.mode", "navigation");
2894 addMapperAndConfigure(mapper);
2895
2896 NotifyMotionArgs args;
2897
2898 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002899 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2900 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2902 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2904 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2905
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2907 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2909 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2910
Michael Wrightd02c5b62014-02-10 15:10:22 -08002911 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002912 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2913 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002915 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2917 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2918
2919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002920 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2922 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2923}
2924
2925TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2926 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2927 addConfigurationProperty("cursor.mode", "navigation");
2928 addMapperAndConfigure(mapper);
2929
2930 NotifyMotionArgs args;
2931
2932 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002933 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2934 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2935 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2936 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2938 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2940 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2941 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2942
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2946 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2947 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2948
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002950 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2951 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2952 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2956 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2957 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2958
2959 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002960 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2961 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2965 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2966
2967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002968 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2970 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2971}
2972
2973TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2974 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2975 addConfigurationProperty("cursor.mode", "navigation");
2976 addMapperAndConfigure(mapper);
2977
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002978 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2980 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2981 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2982 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2983 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2984 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2985 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2986 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2987}
2988
2989TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2990 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2991 addConfigurationProperty("cursor.mode", "navigation");
2992 addConfigurationProperty("cursor.orientationAware", "1");
2993 addMapperAndConfigure(mapper);
2994
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002995 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2997 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2998 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2999 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3000 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3001 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3002 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3003 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3004
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003005 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003006 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3007 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3008 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3009 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3010 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3011 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3012 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3013 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3014
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003015 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3017 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3018 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3019 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3020 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3021 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3022 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3023 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3024
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003025 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3027 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3028 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3029 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3030 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3031 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3032 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3033 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3034}
3035
3036TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
3037 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3038 addConfigurationProperty("cursor.mode", "pointer");
3039 addMapperAndConfigure(mapper);
3040
3041 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3042 mFakePointerController->setPosition(100, 200);
3043 mFakePointerController->setButtonState(0);
3044
3045 NotifyMotionArgs motionArgs;
3046 NotifyKeyArgs keyArgs;
3047
3048 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003049 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3050 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3052 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3053 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3054 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3056 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3057
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3059 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3060 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3061 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3063 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3064
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003065 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3066 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069 ASSERT_EQ(0, motionArgs.buttonState);
3070 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3072 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3073
3074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003075 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076 ASSERT_EQ(0, motionArgs.buttonState);
3077 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3079 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3080
3081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003083 ASSERT_EQ(0, motionArgs.buttonState);
3084 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3086 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3087
3088 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003089 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3090 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3091 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3093 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3094 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3095 motionArgs.buttonState);
3096 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3097 mFakePointerController->getButtonState());
3098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3099 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3100
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3102 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3103 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3104 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3105 mFakePointerController->getButtonState());
3106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3107 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3108
3109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3110 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3111 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3112 motionArgs.buttonState);
3113 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3114 mFakePointerController->getButtonState());
3115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3116 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3117
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003118 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3119 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3123 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3125 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3126
3127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003129 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3130 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3132 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3133
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003134 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3135 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003137 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3138 ASSERT_EQ(0, motionArgs.buttonState);
3139 ASSERT_EQ(0, mFakePointerController->getButtonState());
3140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3141 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 -08003142 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3143 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003144
3145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146 ASSERT_EQ(0, motionArgs.buttonState);
3147 ASSERT_EQ(0, mFakePointerController->getButtonState());
3148 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3150 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 -08003151
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3153 ASSERT_EQ(0, motionArgs.buttonState);
3154 ASSERT_EQ(0, mFakePointerController->getButtonState());
3155 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3157 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3158
3159 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003160 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3161 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3163 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3164 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003165
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003167 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3169 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3171 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3172
3173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3175 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3176 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3178 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3179
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003180 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3181 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 ASSERT_EQ(0, motionArgs.buttonState);
3185 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3187 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3188
3189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003191 ASSERT_EQ(0, motionArgs.buttonState);
3192 ASSERT_EQ(0, mFakePointerController->getButtonState());
3193
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3195 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3197 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3198 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3199
3200 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003201 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3202 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3204 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3205 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003206
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003208 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3210 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3212 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3213
3214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3216 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3217 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3219 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3220
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003221 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3222 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225 ASSERT_EQ(0, motionArgs.buttonState);
3226 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3228 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 -08003229
3230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3231 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3232 ASSERT_EQ(0, motionArgs.buttonState);
3233 ASSERT_EQ(0, mFakePointerController->getButtonState());
3234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3235 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3236
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3238 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3239 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3240
3241 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003242 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3243 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3245 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3246 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003247
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003249 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3251 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3253 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3254
3255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3257 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3258 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3260 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3261
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003262 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3263 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266 ASSERT_EQ(0, motionArgs.buttonState);
3267 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3269 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 -08003270
3271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3272 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3273 ASSERT_EQ(0, motionArgs.buttonState);
3274 ASSERT_EQ(0, mFakePointerController->getButtonState());
3275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3276 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3277
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3280 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3281
3282 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003283 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3284 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3286 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3287 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003288
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003290 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3292 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3294 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3295
3296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3298 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3299 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3301 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3302
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003303 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3304 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307 ASSERT_EQ(0, motionArgs.buttonState);
3308 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3310 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 -08003311
3312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3313 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3314 ASSERT_EQ(0, motionArgs.buttonState);
3315 ASSERT_EQ(0, mFakePointerController->getButtonState());
3316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3317 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3318
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3320 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3321 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3322}
3323
3324TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3325 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3326 addConfigurationProperty("cursor.mode", "pointer");
3327 addMapperAndConfigure(mapper);
3328
3329 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3330 mFakePointerController->setPosition(100, 200);
3331 mFakePointerController->setButtonState(0);
3332
3333 NotifyMotionArgs args;
3334
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003335 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3336 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3337 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003339 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3340 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3342 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3343 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3344}
3345
3346TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3347 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3348 addConfigurationProperty("cursor.mode", "pointer");
3349 mFakePolicy->setPointerCapture(true);
3350 addMapperAndConfigure(mapper);
3351
3352 NotifyDeviceResetArgs resetArgs;
3353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3354 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3355 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3356
3357 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3358 mFakePointerController->setPosition(100, 200);
3359 mFakePointerController->setButtonState(0);
3360
3361 NotifyMotionArgs args;
3362
3363 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003364 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3365 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3366 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3368 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3371 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3372 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3373
3374 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003375 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3376 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3378 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3379 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3381 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3383 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3384 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3386 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3387
3388 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003389 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3390 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3392 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3395 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3397 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3398 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3400 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3401
3402 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003403 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3404 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3405 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3407 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3410 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3411 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3412
3413 // Disable pointer capture and check that the device generation got bumped
3414 // and events are generated the usual way.
3415 const uint32_t generation = mFakeContext->getGeneration();
3416 mFakePolicy->setPointerCapture(false);
3417 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3418 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3419
3420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3421 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3422 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3423
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003424 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3425 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3426 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3428 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3431 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3432 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3433}
3434
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003435TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3436 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3437 addMapperAndConfigure(mapper);
3438
Garfield Tan888a6a42020-01-09 11:39:16 -08003439 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003440 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003441 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3442 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3443 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3444 ViewportType::VIEWPORT_EXTERNAL);
3445 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3446 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3447
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003448 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3449 mFakePointerController->setPosition(100, 200);
3450 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003451
3452 NotifyMotionArgs args;
3453 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3454 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3455 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3457 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3458 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3460 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3461 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3462 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3463}
3464
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465
3466// --- TouchInputMapperTest ---
3467
3468class TouchInputMapperTest : public InputMapperTest {
3469protected:
3470 static const int32_t RAW_X_MIN;
3471 static const int32_t RAW_X_MAX;
3472 static const int32_t RAW_Y_MIN;
3473 static const int32_t RAW_Y_MAX;
3474 static const int32_t RAW_TOUCH_MIN;
3475 static const int32_t RAW_TOUCH_MAX;
3476 static const int32_t RAW_TOOL_MIN;
3477 static const int32_t RAW_TOOL_MAX;
3478 static const int32_t RAW_PRESSURE_MIN;
3479 static const int32_t RAW_PRESSURE_MAX;
3480 static const int32_t RAW_ORIENTATION_MIN;
3481 static const int32_t RAW_ORIENTATION_MAX;
3482 static const int32_t RAW_DISTANCE_MIN;
3483 static const int32_t RAW_DISTANCE_MAX;
3484 static const int32_t RAW_TILT_MIN;
3485 static const int32_t RAW_TILT_MAX;
3486 static const int32_t RAW_ID_MIN;
3487 static const int32_t RAW_ID_MAX;
3488 static const int32_t RAW_SLOT_MIN;
3489 static const int32_t RAW_SLOT_MAX;
3490 static const float X_PRECISION;
3491 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003492 static const float X_PRECISION_VIRTUAL;
3493 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494
3495 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003496 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497
3498 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3499
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003500 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003501 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003502
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503 enum Axes {
3504 POSITION = 1 << 0,
3505 TOUCH = 1 << 1,
3506 TOOL = 1 << 2,
3507 PRESSURE = 1 << 3,
3508 ORIENTATION = 1 << 4,
3509 MINOR = 1 << 5,
3510 ID = 1 << 6,
3511 DISTANCE = 1 << 7,
3512 TILT = 1 << 8,
3513 SLOT = 1 << 9,
3514 TOOL_TYPE = 1 << 10,
3515 };
3516
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003517 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3518 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003519 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003521 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522 int32_t toRawX(float displayX);
3523 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003524 float toCookedX(float rawX, float rawY);
3525 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003527 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003529 float toDisplayY(int32_t rawY, int32_t displayHeight);
3530
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531};
3532
3533const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3534const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3535const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3536const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3537const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3538const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3539const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3540const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003541const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3542const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3544const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3545const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3546const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3547const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3548const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3549const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3550const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3551const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3552const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3553const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3554const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003555const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3556 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3557const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3558 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003559const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3560 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561
3562const float TouchInputMapperTest::GEOMETRIC_SCALE =
3563 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3564 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3565
3566const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3567 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3568 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3569};
3570
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003571void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003572 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003573 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3574}
3575
3576void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3577 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3578 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579}
3580
Santos Cordonfa5cf462017-04-05 10:37:00 -07003581void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003582 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3583 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003584 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003585}
3586
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587void TouchInputMapperTest::prepareVirtualKeys() {
3588 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3589 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3590 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3591 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3592}
3593
Jason Gerecke489fda82012-09-07 17:19:40 -07003594void TouchInputMapperTest::prepareLocationCalibration() {
3595 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3596}
3597
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598int32_t TouchInputMapperTest::toRawX(float displayX) {
3599 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3600}
3601
3602int32_t TouchInputMapperTest::toRawY(float displayY) {
3603 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3604}
3605
Jason Gerecke489fda82012-09-07 17:19:40 -07003606float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3607 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3608 return rawX;
3609}
3610
3611float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3612 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3613 return rawY;
3614}
3615
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003617 return toDisplayX(rawX, DISPLAY_WIDTH);
3618}
3619
3620float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3621 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622}
3623
3624float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003625 return toDisplayY(rawY, DISPLAY_HEIGHT);
3626}
3627
3628float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3629 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003630}
3631
3632
3633// --- SingleTouchInputMapperTest ---
3634
3635class SingleTouchInputMapperTest : public TouchInputMapperTest {
3636protected:
3637 void prepareButtons();
3638 void prepareAxes(int axes);
3639
3640 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3641 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3642 void processUp(SingleTouchInputMapper* mappery);
3643 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3644 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3645 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3646 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3647 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3648 void processSync(SingleTouchInputMapper* mapper);
3649};
3650
3651void SingleTouchInputMapperTest::prepareButtons() {
3652 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3653}
3654
3655void SingleTouchInputMapperTest::prepareAxes(int axes) {
3656 if (axes & POSITION) {
3657 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3658 RAW_X_MIN, RAW_X_MAX, 0, 0);
3659 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3660 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3661 }
3662 if (axes & PRESSURE) {
3663 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3664 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3665 }
3666 if (axes & TOOL) {
3667 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3668 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3669 }
3670 if (axes & DISTANCE) {
3671 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3672 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3673 }
3674 if (axes & TILT) {
3675 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3676 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3677 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3678 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3679 }
3680}
3681
3682void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003683 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3684 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3685 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686}
3687
3688void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003689 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3690 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691}
3692
3693void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003694 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003695}
3696
3697void SingleTouchInputMapperTest::processPressure(
3698 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003699 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700}
3701
3702void SingleTouchInputMapperTest::processToolMajor(
3703 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003704 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705}
3706
3707void SingleTouchInputMapperTest::processDistance(
3708 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003709 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710}
3711
3712void SingleTouchInputMapperTest::processTilt(
3713 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003714 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3715 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716}
3717
3718void SingleTouchInputMapperTest::processKey(
3719 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003720 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721}
3722
3723void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003724 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725}
3726
3727
3728TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3729 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3730 prepareButtons();
3731 prepareAxes(POSITION);
3732 addMapperAndConfigure(mapper);
3733
3734 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3735}
3736
3737TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3738 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3739 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3740 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3741 prepareButtons();
3742 prepareAxes(POSITION);
3743 addMapperAndConfigure(mapper);
3744
3745 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3746}
3747
3748TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3749 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3750 prepareButtons();
3751 prepareAxes(POSITION);
3752 addConfigurationProperty("touch.deviceType", "touchPad");
3753 addMapperAndConfigure(mapper);
3754
3755 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3756}
3757
3758TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3759 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3760 prepareButtons();
3761 prepareAxes(POSITION);
3762 addConfigurationProperty("touch.deviceType", "touchScreen");
3763 addMapperAndConfigure(mapper);
3764
3765 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3766}
3767
3768TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3769 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3770 addConfigurationProperty("touch.deviceType", "touchScreen");
3771 prepareDisplay(DISPLAY_ORIENTATION_0);
3772 prepareButtons();
3773 prepareAxes(POSITION);
3774 prepareVirtualKeys();
3775 addMapperAndConfigure(mapper);
3776
3777 // Unknown key.
3778 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3779
3780 // Virtual key is down.
3781 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3782 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3783 processDown(mapper, x, y);
3784 processSync(mapper);
3785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3786
3787 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3788
3789 // Virtual key is up.
3790 processUp(mapper);
3791 processSync(mapper);
3792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3793
3794 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3795}
3796
3797TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3798 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3799 addConfigurationProperty("touch.deviceType", "touchScreen");
3800 prepareDisplay(DISPLAY_ORIENTATION_0);
3801 prepareButtons();
3802 prepareAxes(POSITION);
3803 prepareVirtualKeys();
3804 addMapperAndConfigure(mapper);
3805
3806 // Unknown key.
3807 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3808
3809 // Virtual key is down.
3810 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3811 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3812 processDown(mapper, x, y);
3813 processSync(mapper);
3814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3815
3816 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3817
3818 // Virtual key is up.
3819 processUp(mapper);
3820 processSync(mapper);
3821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3822
3823 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3824}
3825
3826TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3827 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3828 addConfigurationProperty("touch.deviceType", "touchScreen");
3829 prepareDisplay(DISPLAY_ORIENTATION_0);
3830 prepareButtons();
3831 prepareAxes(POSITION);
3832 prepareVirtualKeys();
3833 addMapperAndConfigure(mapper);
3834
3835 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3836 uint8_t flags[2] = { 0, 0 };
3837 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3838 ASSERT_TRUE(flags[0]);
3839 ASSERT_FALSE(flags[1]);
3840}
3841
3842TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3843 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3844 addConfigurationProperty("touch.deviceType", "touchScreen");
3845 prepareDisplay(DISPLAY_ORIENTATION_0);
3846 prepareButtons();
3847 prepareAxes(POSITION);
3848 prepareVirtualKeys();
3849 addMapperAndConfigure(mapper);
3850
3851 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3852
3853 NotifyKeyArgs args;
3854
3855 // Press virtual key.
3856 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3857 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3858 processDown(mapper, x, y);
3859 processSync(mapper);
3860
3861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3862 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3863 ASSERT_EQ(DEVICE_ID, args.deviceId);
3864 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3865 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3866 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3867 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3868 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3869 ASSERT_EQ(KEY_HOME, args.scanCode);
3870 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3871 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3872
3873 // Release virtual key.
3874 processUp(mapper);
3875 processSync(mapper);
3876
3877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3878 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3879 ASSERT_EQ(DEVICE_ID, args.deviceId);
3880 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3881 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3882 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3883 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3884 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3885 ASSERT_EQ(KEY_HOME, args.scanCode);
3886 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3887 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3888
3889 // Should not have sent any motions.
3890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3891}
3892
3893TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3894 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3895 addConfigurationProperty("touch.deviceType", "touchScreen");
3896 prepareDisplay(DISPLAY_ORIENTATION_0);
3897 prepareButtons();
3898 prepareAxes(POSITION);
3899 prepareVirtualKeys();
3900 addMapperAndConfigure(mapper);
3901
3902 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3903
3904 NotifyKeyArgs keyArgs;
3905
3906 // Press virtual key.
3907 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3908 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3909 processDown(mapper, x, y);
3910 processSync(mapper);
3911
3912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3913 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3914 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3915 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3916 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3917 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3918 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3919 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3920 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3921 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3922 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3923
3924 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3925 // into the display area.
3926 y -= 100;
3927 processMove(mapper, x, y);
3928 processSync(mapper);
3929
3930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3931 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3932 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3933 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3934 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3935 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3936 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3937 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3938 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3939 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3940 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3941 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3942
3943 NotifyMotionArgs motionArgs;
3944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3945 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3946 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3947 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3948 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3949 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3950 ASSERT_EQ(0, motionArgs.flags);
3951 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3952 ASSERT_EQ(0, motionArgs.buttonState);
3953 ASSERT_EQ(0, motionArgs.edgeFlags);
3954 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3955 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3958 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3959 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3960 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3961 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3962
3963 // Keep moving out of bounds. Should generate a pointer move.
3964 y -= 50;
3965 processMove(mapper, x, y);
3966 processSync(mapper);
3967
3968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3970 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3971 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3972 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3974 ASSERT_EQ(0, motionArgs.flags);
3975 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3976 ASSERT_EQ(0, motionArgs.buttonState);
3977 ASSERT_EQ(0, motionArgs.edgeFlags);
3978 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3979 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3982 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3983 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3984 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3985 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3986
3987 // Release out of bounds. Should generate a pointer up.
3988 processUp(mapper);
3989 processSync(mapper);
3990
3991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3992 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3993 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3994 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3995 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3996 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3997 ASSERT_EQ(0, motionArgs.flags);
3998 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3999 ASSERT_EQ(0, motionArgs.buttonState);
4000 ASSERT_EQ(0, motionArgs.edgeFlags);
4001 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4002 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4005 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4006 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4007 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4008 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4009
4010 // Should not have sent any more keys or motions.
4011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4013}
4014
4015TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
4016 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4017 addConfigurationProperty("touch.deviceType", "touchScreen");
4018 prepareDisplay(DISPLAY_ORIENTATION_0);
4019 prepareButtons();
4020 prepareAxes(POSITION);
4021 prepareVirtualKeys();
4022 addMapperAndConfigure(mapper);
4023
4024 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4025
4026 NotifyMotionArgs motionArgs;
4027
4028 // Initially go down out of bounds.
4029 int32_t x = -10;
4030 int32_t y = -10;
4031 processDown(mapper, x, y);
4032 processSync(mapper);
4033
4034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4035
4036 // Move into the display area. Should generate a pointer down.
4037 x = 50;
4038 y = 75;
4039 processMove(mapper, x, y);
4040 processSync(mapper);
4041
4042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4043 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4044 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4045 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4046 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4048 ASSERT_EQ(0, motionArgs.flags);
4049 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4050 ASSERT_EQ(0, motionArgs.buttonState);
4051 ASSERT_EQ(0, motionArgs.edgeFlags);
4052 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4053 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4056 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4057 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4058 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4059 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4060
4061 // Release. Should generate a pointer up.
4062 processUp(mapper);
4063 processSync(mapper);
4064
4065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4066 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4067 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4068 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4069 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4070 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4071 ASSERT_EQ(0, motionArgs.flags);
4072 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4073 ASSERT_EQ(0, motionArgs.buttonState);
4074 ASSERT_EQ(0, motionArgs.edgeFlags);
4075 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4076 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4079 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4080 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4081 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4082 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4083
4084 // Should not have sent any more keys or motions.
4085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4087}
4088
Santos Cordonfa5cf462017-04-05 10:37:00 -07004089TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
4090 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4091 addConfigurationProperty("touch.deviceType", "touchScreen");
4092 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4093
4094 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4095 prepareButtons();
4096 prepareAxes(POSITION);
4097 prepareVirtualKeys();
4098 addMapperAndConfigure(mapper);
4099
4100 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4101
4102 NotifyMotionArgs motionArgs;
4103
4104 // Down.
4105 int32_t x = 100;
4106 int32_t y = 125;
4107 processDown(mapper, x, y);
4108 processSync(mapper);
4109
4110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4111 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4112 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4113 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4114 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4115 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4116 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4117 ASSERT_EQ(0, motionArgs.flags);
4118 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4119 ASSERT_EQ(0, motionArgs.buttonState);
4120 ASSERT_EQ(0, motionArgs.edgeFlags);
4121 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4122 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4125 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4126 1, 0, 0, 0, 0, 0, 0, 0));
4127 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4128 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4130
4131 // Move.
4132 x += 50;
4133 y += 75;
4134 processMove(mapper, x, y);
4135 processSync(mapper);
4136
4137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4138 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4139 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4140 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4141 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4142 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4144 ASSERT_EQ(0, motionArgs.flags);
4145 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4146 ASSERT_EQ(0, motionArgs.buttonState);
4147 ASSERT_EQ(0, motionArgs.edgeFlags);
4148 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4149 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4152 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4153 1, 0, 0, 0, 0, 0, 0, 0));
4154 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4155 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4157
4158 // Up.
4159 processUp(mapper);
4160 processSync(mapper);
4161
4162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4163 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4164 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4165 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4166 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4167 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4168 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4169 ASSERT_EQ(0, motionArgs.flags);
4170 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4171 ASSERT_EQ(0, motionArgs.buttonState);
4172 ASSERT_EQ(0, motionArgs.edgeFlags);
4173 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4174 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4177 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4178 1, 0, 0, 0, 0, 0, 0, 0));
4179 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4180 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4181 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4182
4183 // Should not have sent any more keys or motions.
4184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4186}
4187
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
4189 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4190 addConfigurationProperty("touch.deviceType", "touchScreen");
4191 prepareDisplay(DISPLAY_ORIENTATION_0);
4192 prepareButtons();
4193 prepareAxes(POSITION);
4194 prepareVirtualKeys();
4195 addMapperAndConfigure(mapper);
4196
4197 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4198
4199 NotifyMotionArgs motionArgs;
4200
4201 // Down.
4202 int32_t x = 100;
4203 int32_t y = 125;
4204 processDown(mapper, x, y);
4205 processSync(mapper);
4206
4207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4208 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4209 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4210 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4211 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4212 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4213 ASSERT_EQ(0, motionArgs.flags);
4214 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4215 ASSERT_EQ(0, motionArgs.buttonState);
4216 ASSERT_EQ(0, motionArgs.edgeFlags);
4217 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4218 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4221 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4222 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4223 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4225
4226 // Move.
4227 x += 50;
4228 y += 75;
4229 processMove(mapper, x, y);
4230 processSync(mapper);
4231
4232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4234 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4235 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4236 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4238 ASSERT_EQ(0, motionArgs.flags);
4239 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4240 ASSERT_EQ(0, motionArgs.buttonState);
4241 ASSERT_EQ(0, motionArgs.edgeFlags);
4242 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4243 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4246 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4247 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4248 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4250
4251 // Up.
4252 processUp(mapper);
4253 processSync(mapper);
4254
4255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4257 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4258 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4259 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4260 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4261 ASSERT_EQ(0, motionArgs.flags);
4262 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4263 ASSERT_EQ(0, motionArgs.buttonState);
4264 ASSERT_EQ(0, motionArgs.edgeFlags);
4265 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4266 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4269 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4270 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4271 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4273
4274 // Should not have sent any more keys or motions.
4275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4277}
4278
4279TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
4280 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4281 addConfigurationProperty("touch.deviceType", "touchScreen");
4282 prepareButtons();
4283 prepareAxes(POSITION);
4284 addConfigurationProperty("touch.orientationAware", "0");
4285 addMapperAndConfigure(mapper);
4286
4287 NotifyMotionArgs args;
4288
4289 // Rotation 90.
4290 prepareDisplay(DISPLAY_ORIENTATION_90);
4291 processDown(mapper, toRawX(50), toRawY(75));
4292 processSync(mapper);
4293
4294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4295 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4296 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4297
4298 processUp(mapper);
4299 processSync(mapper);
4300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4301}
4302
4303TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4304 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4305 addConfigurationProperty("touch.deviceType", "touchScreen");
4306 prepareButtons();
4307 prepareAxes(POSITION);
4308 addMapperAndConfigure(mapper);
4309
4310 NotifyMotionArgs args;
4311
4312 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004313 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 prepareDisplay(DISPLAY_ORIENTATION_0);
4315 processDown(mapper, toRawX(50), toRawY(75));
4316 processSync(mapper);
4317
4318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4319 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4320 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4321
4322 processUp(mapper);
4323 processSync(mapper);
4324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4325
4326 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004327 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 prepareDisplay(DISPLAY_ORIENTATION_90);
4329 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4330 processSync(mapper);
4331
4332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4333 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4334 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4335
4336 processUp(mapper);
4337 processSync(mapper);
4338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4339
4340 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004341 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342 prepareDisplay(DISPLAY_ORIENTATION_180);
4343 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4344 processSync(mapper);
4345
4346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4347 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4348 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4349
4350 processUp(mapper);
4351 processSync(mapper);
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4353
4354 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004355 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 prepareDisplay(DISPLAY_ORIENTATION_270);
4357 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4358 processSync(mapper);
4359
4360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4361 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4362 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4363
4364 processUp(mapper);
4365 processSync(mapper);
4366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4367}
4368
4369TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4370 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4371 addConfigurationProperty("touch.deviceType", "touchScreen");
4372 prepareDisplay(DISPLAY_ORIENTATION_0);
4373 prepareButtons();
4374 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4375 addMapperAndConfigure(mapper);
4376
4377 // These calculations are based on the input device calibration documentation.
4378 int32_t rawX = 100;
4379 int32_t rawY = 200;
4380 int32_t rawPressure = 10;
4381 int32_t rawToolMajor = 12;
4382 int32_t rawDistance = 2;
4383 int32_t rawTiltX = 30;
4384 int32_t rawTiltY = 110;
4385
4386 float x = toDisplayX(rawX);
4387 float y = toDisplayY(rawY);
4388 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4389 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4390 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4391 float distance = float(rawDistance);
4392
4393 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4394 float tiltScale = M_PI / 180;
4395 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4396 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4397 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4398 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4399
4400 processDown(mapper, rawX, rawY);
4401 processPressure(mapper, rawPressure);
4402 processToolMajor(mapper, rawToolMajor);
4403 processDistance(mapper, rawDistance);
4404 processTilt(mapper, rawTiltX, rawTiltY);
4405 processSync(mapper);
4406
4407 NotifyMotionArgs args;
4408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4410 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4411 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4412}
4413
Jason Gerecke489fda82012-09-07 17:19:40 -07004414TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4415 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4416 addConfigurationProperty("touch.deviceType", "touchScreen");
4417 prepareDisplay(DISPLAY_ORIENTATION_0);
4418 prepareLocationCalibration();
4419 prepareButtons();
4420 prepareAxes(POSITION);
4421 addMapperAndConfigure(mapper);
4422
4423 int32_t rawX = 100;
4424 int32_t rawY = 200;
4425
4426 float x = toDisplayX(toCookedX(rawX, rawY));
4427 float y = toDisplayY(toCookedY(rawX, rawY));
4428
4429 processDown(mapper, rawX, rawY);
4430 processSync(mapper);
4431
4432 NotifyMotionArgs args;
4433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4435 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4436}
4437
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4439 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4440 addConfigurationProperty("touch.deviceType", "touchScreen");
4441 prepareDisplay(DISPLAY_ORIENTATION_0);
4442 prepareButtons();
4443 prepareAxes(POSITION);
4444 addMapperAndConfigure(mapper);
4445
4446 NotifyMotionArgs motionArgs;
4447 NotifyKeyArgs keyArgs;
4448
4449 processDown(mapper, 100, 200);
4450 processSync(mapper);
4451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4452 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4453 ASSERT_EQ(0, motionArgs.buttonState);
4454
4455 // press BTN_LEFT, release BTN_LEFT
4456 processKey(mapper, BTN_LEFT, 1);
4457 processSync(mapper);
4458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4460 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4461
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4464 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4465
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 processKey(mapper, BTN_LEFT, 0);
4467 processSync(mapper);
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004469 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004471
4472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004474 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475
4476 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4477 processKey(mapper, BTN_RIGHT, 1);
4478 processKey(mapper, BTN_MIDDLE, 1);
4479 processSync(mapper);
4480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4482 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4483 motionArgs.buttonState);
4484
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4487 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4488
4489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4490 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4491 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4492 motionArgs.buttonState);
4493
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 processKey(mapper, BTN_RIGHT, 0);
4495 processSync(mapper);
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004499
4500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004502 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503
4504 processKey(mapper, BTN_MIDDLE, 0);
4505 processSync(mapper);
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004509
4510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004512 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513
4514 // press BTN_BACK, release BTN_BACK
4515 processKey(mapper, BTN_BACK, 1);
4516 processSync(mapper);
4517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4518 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4519 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004520
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004523 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4524
4525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4526 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4527 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528
4529 processKey(mapper, BTN_BACK, 0);
4530 processSync(mapper);
4531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004532 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004534
4535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004537 ASSERT_EQ(0, motionArgs.buttonState);
4538
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4540 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4541 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4542
4543 // press BTN_SIDE, release BTN_SIDE
4544 processKey(mapper, BTN_SIDE, 1);
4545 processSync(mapper);
4546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4547 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4548 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004552 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4553
4554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4556 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557
4558 processKey(mapper, BTN_SIDE, 0);
4559 processSync(mapper);
4560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004561 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004563
4564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004566 ASSERT_EQ(0, motionArgs.buttonState);
4567
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4569 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4570 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4571
4572 // press BTN_FORWARD, release BTN_FORWARD
4573 processKey(mapper, BTN_FORWARD, 1);
4574 processSync(mapper);
4575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4576 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4577 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004578
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004581 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4582
4583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4585 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586
4587 processKey(mapper, BTN_FORWARD, 0);
4588 processSync(mapper);
4589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004590 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004592
4593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004595 ASSERT_EQ(0, motionArgs.buttonState);
4596
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4598 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4599 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4600
4601 // press BTN_EXTRA, release BTN_EXTRA
4602 processKey(mapper, BTN_EXTRA, 1);
4603 processSync(mapper);
4604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4605 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4606 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004607
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004610 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4611
4612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4613 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4614 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615
4616 processKey(mapper, BTN_EXTRA, 0);
4617 processSync(mapper);
4618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004621
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004624 ASSERT_EQ(0, motionArgs.buttonState);
4625
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4627 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4628 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4629
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4631
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 // press BTN_STYLUS, release BTN_STYLUS
4633 processKey(mapper, BTN_STYLUS, 1);
4634 processSync(mapper);
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004637 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4638
4639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4641 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642
4643 processKey(mapper, BTN_STYLUS, 0);
4644 processSync(mapper);
4645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004648
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004651 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652
4653 // press BTN_STYLUS2, release BTN_STYLUS2
4654 processKey(mapper, BTN_STYLUS2, 1);
4655 processSync(mapper);
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004658 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4659
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4661 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4662 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663
4664 processKey(mapper, BTN_STYLUS2, 0);
4665 processSync(mapper);
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004669
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004672 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673
4674 // release touch
4675 processUp(mapper);
4676 processSync(mapper);
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4678 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4679 ASSERT_EQ(0, motionArgs.buttonState);
4680}
4681
4682TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4683 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4684 addConfigurationProperty("touch.deviceType", "touchScreen");
4685 prepareDisplay(DISPLAY_ORIENTATION_0);
4686 prepareButtons();
4687 prepareAxes(POSITION);
4688 addMapperAndConfigure(mapper);
4689
4690 NotifyMotionArgs motionArgs;
4691
4692 // default tool type is finger
4693 processDown(mapper, 100, 200);
4694 processSync(mapper);
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4696 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4698
4699 // eraser
4700 processKey(mapper, BTN_TOOL_RUBBER, 1);
4701 processSync(mapper);
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4703 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4705
4706 // stylus
4707 processKey(mapper, BTN_TOOL_RUBBER, 0);
4708 processKey(mapper, BTN_TOOL_PEN, 1);
4709 processSync(mapper);
4710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4713
4714 // brush
4715 processKey(mapper, BTN_TOOL_PEN, 0);
4716 processKey(mapper, BTN_TOOL_BRUSH, 1);
4717 processSync(mapper);
4718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4721
4722 // pencil
4723 processKey(mapper, BTN_TOOL_BRUSH, 0);
4724 processKey(mapper, BTN_TOOL_PENCIL, 1);
4725 processSync(mapper);
4726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4729
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004730 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 processKey(mapper, BTN_TOOL_PENCIL, 0);
4732 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4733 processSync(mapper);
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4737
4738 // mouse
4739 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4740 processKey(mapper, BTN_TOOL_MOUSE, 1);
4741 processSync(mapper);
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4745
4746 // lens
4747 processKey(mapper, BTN_TOOL_MOUSE, 0);
4748 processKey(mapper, BTN_TOOL_LENS, 1);
4749 processSync(mapper);
4750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4753
4754 // double-tap
4755 processKey(mapper, BTN_TOOL_LENS, 0);
4756 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4757 processSync(mapper);
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4761
4762 // triple-tap
4763 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4764 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4765 processSync(mapper);
4766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4769
4770 // quad-tap
4771 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4772 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4773 processSync(mapper);
4774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4777
4778 // finger
4779 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4780 processKey(mapper, BTN_TOOL_FINGER, 1);
4781 processSync(mapper);
4782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4785
4786 // stylus trumps finger
4787 processKey(mapper, BTN_TOOL_PEN, 1);
4788 processSync(mapper);
4789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4792
4793 // eraser trumps stylus
4794 processKey(mapper, BTN_TOOL_RUBBER, 1);
4795 processSync(mapper);
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4799
4800 // mouse trumps eraser
4801 processKey(mapper, BTN_TOOL_MOUSE, 1);
4802 processSync(mapper);
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4806
4807 // back to default tool type
4808 processKey(mapper, BTN_TOOL_MOUSE, 0);
4809 processKey(mapper, BTN_TOOL_RUBBER, 0);
4810 processKey(mapper, BTN_TOOL_PEN, 0);
4811 processKey(mapper, BTN_TOOL_FINGER, 0);
4812 processSync(mapper);
4813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4815 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4816}
4817
4818TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4819 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4820 addConfigurationProperty("touch.deviceType", "touchScreen");
4821 prepareDisplay(DISPLAY_ORIENTATION_0);
4822 prepareButtons();
4823 prepareAxes(POSITION);
4824 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4825 addMapperAndConfigure(mapper);
4826
4827 NotifyMotionArgs motionArgs;
4828
4829 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4830 processKey(mapper, BTN_TOOL_FINGER, 1);
4831 processMove(mapper, 100, 200);
4832 processSync(mapper);
4833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4834 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4836 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4837
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4839 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4841 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4842
4843 // move a little
4844 processMove(mapper, 150, 250);
4845 processSync(mapper);
4846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4847 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4849 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4850
4851 // down when BTN_TOUCH is pressed, pressure defaults to 1
4852 processKey(mapper, BTN_TOUCH, 1);
4853 processSync(mapper);
4854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4855 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4857 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4858
4859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4860 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4862 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4863
4864 // up when BTN_TOUCH is released, hover restored
4865 processKey(mapper, BTN_TOUCH, 0);
4866 processSync(mapper);
4867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4868 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4870 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4871
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4875 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4876
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4878 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4880 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4881
4882 // exit hover when pointer goes away
4883 processKey(mapper, BTN_TOOL_FINGER, 0);
4884 processSync(mapper);
4885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4886 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4888 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4889}
4890
4891TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4892 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4893 addConfigurationProperty("touch.deviceType", "touchScreen");
4894 prepareDisplay(DISPLAY_ORIENTATION_0);
4895 prepareButtons();
4896 prepareAxes(POSITION | PRESSURE);
4897 addMapperAndConfigure(mapper);
4898
4899 NotifyMotionArgs motionArgs;
4900
4901 // initially hovering because pressure is 0
4902 processDown(mapper, 100, 200);
4903 processPressure(mapper, 0);
4904 processSync(mapper);
4905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4906 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4908 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4909
4910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4911 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4913 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4914
4915 // move a little
4916 processMove(mapper, 150, 250);
4917 processSync(mapper);
4918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4919 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4921 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4922
4923 // down when pressure is non-zero
4924 processPressure(mapper, RAW_PRESSURE_MAX);
4925 processSync(mapper);
4926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4927 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4929 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4930
4931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4932 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4934 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4935
4936 // up when pressure becomes 0, hover restored
4937 processPressure(mapper, 0);
4938 processSync(mapper);
4939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4940 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4942 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4943
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4945 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4947 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4948
4949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4950 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4952 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4953
4954 // exit hover when pointer goes away
4955 processUp(mapper);
4956 processSync(mapper);
4957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4958 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4960 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4961}
4962
Dan Harmsaca28402018-12-17 13:55:20 -08004963
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964// --- MultiTouchInputMapperTest ---
4965
4966class MultiTouchInputMapperTest : public TouchInputMapperTest {
4967protected:
4968 void prepareAxes(int axes);
4969
4970 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4971 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4972 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4973 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4974 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4975 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4976 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4977 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4978 void processId(MultiTouchInputMapper* mapper, int32_t id);
4979 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4980 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4981 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4982 void processMTSync(MultiTouchInputMapper* mapper);
4983 void processSync(MultiTouchInputMapper* mapper);
4984};
4985
4986void MultiTouchInputMapperTest::prepareAxes(int axes) {
4987 if (axes & POSITION) {
4988 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4989 RAW_X_MIN, RAW_X_MAX, 0, 0);
4990 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4991 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4992 }
4993 if (axes & TOUCH) {
4994 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4995 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4996 if (axes & MINOR) {
4997 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4998 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4999 }
5000 }
5001 if (axes & TOOL) {
5002 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
5003 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
5004 if (axes & MINOR) {
5005 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
5006 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
5007 }
5008 }
5009 if (axes & ORIENTATION) {
5010 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
5011 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
5012 }
5013 if (axes & PRESSURE) {
5014 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
5015 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
5016 }
5017 if (axes & DISTANCE) {
5018 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
5019 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
5020 }
5021 if (axes & ID) {
5022 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
5023 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
5024 }
5025 if (axes & SLOT) {
5026 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
5027 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5028 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
5029 }
5030 if (axes & TOOL_TYPE) {
5031 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
5032 0, MT_TOOL_MAX, 0, 0);
5033 }
5034}
5035
5036void MultiTouchInputMapperTest::processPosition(
5037 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005038 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5039 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040}
5041
5042void MultiTouchInputMapperTest::processTouchMajor(
5043 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005044 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045}
5046
5047void MultiTouchInputMapperTest::processTouchMinor(
5048 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005049 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050}
5051
5052void MultiTouchInputMapperTest::processToolMajor(
5053 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005054 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055}
5056
5057void MultiTouchInputMapperTest::processToolMinor(
5058 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005059 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060}
5061
5062void MultiTouchInputMapperTest::processOrientation(
5063 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005064 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065}
5066
5067void MultiTouchInputMapperTest::processPressure(
5068 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005069 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070}
5071
5072void MultiTouchInputMapperTest::processDistance(
5073 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005074 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075}
5076
5077void MultiTouchInputMapperTest::processId(
5078 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005079 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080}
5081
5082void MultiTouchInputMapperTest::processSlot(
5083 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005084 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085}
5086
5087void MultiTouchInputMapperTest::processToolType(
5088 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005089 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090}
5091
5092void MultiTouchInputMapperTest::processKey(
5093 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005094 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095}
5096
5097void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005098 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099}
5100
5101void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005102 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103}
5104
5105
5106TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
5107 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5108 addConfigurationProperty("touch.deviceType", "touchScreen");
5109 prepareDisplay(DISPLAY_ORIENTATION_0);
5110 prepareAxes(POSITION);
5111 prepareVirtualKeys();
5112 addMapperAndConfigure(mapper);
5113
5114 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5115
5116 NotifyMotionArgs motionArgs;
5117
5118 // Two fingers down at once.
5119 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5120 processPosition(mapper, x1, y1);
5121 processMTSync(mapper);
5122 processPosition(mapper, x2, y2);
5123 processMTSync(mapper);
5124 processSync(mapper);
5125
5126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5127 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5128 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5129 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5130 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5131 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5132 ASSERT_EQ(0, motionArgs.flags);
5133 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5134 ASSERT_EQ(0, motionArgs.buttonState);
5135 ASSERT_EQ(0, motionArgs.edgeFlags);
5136 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5137 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5140 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5141 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5142 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5144
5145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5146 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5147 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5148 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5149 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5150 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5151 motionArgs.action);
5152 ASSERT_EQ(0, motionArgs.flags);
5153 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5154 ASSERT_EQ(0, motionArgs.buttonState);
5155 ASSERT_EQ(0, motionArgs.edgeFlags);
5156 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5157 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5158 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5159 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5160 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5162 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5164 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5165 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5166 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5167 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5168
5169 // Move.
5170 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5171 processPosition(mapper, x1, y1);
5172 processMTSync(mapper);
5173 processPosition(mapper, x2, y2);
5174 processMTSync(mapper);
5175 processSync(mapper);
5176
5177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5178 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5179 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5180 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5181 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5183 ASSERT_EQ(0, motionArgs.flags);
5184 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5185 ASSERT_EQ(0, motionArgs.buttonState);
5186 ASSERT_EQ(0, motionArgs.edgeFlags);
5187 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5188 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5190 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5193 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5195 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5196 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5197 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5198 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5199
5200 // First finger up.
5201 x2 += 15; y2 -= 20;
5202 processPosition(mapper, x2, y2);
5203 processMTSync(mapper);
5204 processSync(mapper);
5205
5206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5207 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5208 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5209 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5210 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5211 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5212 motionArgs.action);
5213 ASSERT_EQ(0, motionArgs.flags);
5214 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5215 ASSERT_EQ(0, motionArgs.buttonState);
5216 ASSERT_EQ(0, motionArgs.edgeFlags);
5217 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5218 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5220 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5223 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5225 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5226 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5227 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5229
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5232 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5233 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5234 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5236 ASSERT_EQ(0, motionArgs.flags);
5237 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5238 ASSERT_EQ(0, motionArgs.buttonState);
5239 ASSERT_EQ(0, motionArgs.edgeFlags);
5240 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5241 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5244 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5245 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5246 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5248
5249 // Move.
5250 x2 += 20; y2 -= 25;
5251 processPosition(mapper, x2, y2);
5252 processMTSync(mapper);
5253 processSync(mapper);
5254
5255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5257 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5258 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5259 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5261 ASSERT_EQ(0, motionArgs.flags);
5262 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5263 ASSERT_EQ(0, motionArgs.buttonState);
5264 ASSERT_EQ(0, motionArgs.edgeFlags);
5265 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5266 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5269 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5270 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5271 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5273
5274 // New finger down.
5275 int32_t x3 = 700, y3 = 300;
5276 processPosition(mapper, x2, y2);
5277 processMTSync(mapper);
5278 processPosition(mapper, x3, y3);
5279 processMTSync(mapper);
5280 processSync(mapper);
5281
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5284 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5285 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5286 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5287 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5288 motionArgs.action);
5289 ASSERT_EQ(0, motionArgs.flags);
5290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5291 ASSERT_EQ(0, motionArgs.buttonState);
5292 ASSERT_EQ(0, motionArgs.edgeFlags);
5293 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5294 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5296 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5299 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5301 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5302 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5303 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5304 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5305
5306 // Second finger up.
5307 x3 += 30; y3 -= 20;
5308 processPosition(mapper, x3, y3);
5309 processMTSync(mapper);
5310 processSync(mapper);
5311
5312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5314 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5315 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5316 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5317 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5318 motionArgs.action);
5319 ASSERT_EQ(0, motionArgs.flags);
5320 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5321 ASSERT_EQ(0, motionArgs.buttonState);
5322 ASSERT_EQ(0, motionArgs.edgeFlags);
5323 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5324 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5326 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5329 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5331 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5332 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5333 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5334 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5335
5336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5338 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5339 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5340 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5342 ASSERT_EQ(0, motionArgs.flags);
5343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5344 ASSERT_EQ(0, motionArgs.buttonState);
5345 ASSERT_EQ(0, motionArgs.edgeFlags);
5346 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5347 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5350 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5351 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5352 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5354
5355 // Last finger up.
5356 processMTSync(mapper);
5357 processSync(mapper);
5358
5359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5360 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5361 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5362 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5363 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5364 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5365 ASSERT_EQ(0, motionArgs.flags);
5366 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5367 ASSERT_EQ(0, motionArgs.buttonState);
5368 ASSERT_EQ(0, motionArgs.edgeFlags);
5369 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5370 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5373 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5374 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5375 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5376 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5377
5378 // Should not have sent any more keys or motions.
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5381}
5382
5383TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5384 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5385 addConfigurationProperty("touch.deviceType", "touchScreen");
5386 prepareDisplay(DISPLAY_ORIENTATION_0);
5387 prepareAxes(POSITION | ID);
5388 prepareVirtualKeys();
5389 addMapperAndConfigure(mapper);
5390
5391 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5392
5393 NotifyMotionArgs motionArgs;
5394
5395 // Two fingers down at once.
5396 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5397 processPosition(mapper, x1, y1);
5398 processId(mapper, 1);
5399 processMTSync(mapper);
5400 processPosition(mapper, x2, y2);
5401 processId(mapper, 2);
5402 processMTSync(mapper);
5403 processSync(mapper);
5404
5405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5406 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5407 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5408 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5411 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5412
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5414 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5415 motionArgs.action);
5416 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5417 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5419 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5422 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5424 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5425
5426 // Move.
5427 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5428 processPosition(mapper, x1, y1);
5429 processId(mapper, 1);
5430 processMTSync(mapper);
5431 processPosition(mapper, x2, y2);
5432 processId(mapper, 2);
5433 processMTSync(mapper);
5434 processSync(mapper);
5435
5436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5438 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5441 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5444 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5446 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5447
5448 // First finger up.
5449 x2 += 15; y2 -= 20;
5450 processPosition(mapper, x2, y2);
5451 processId(mapper, 2);
5452 processMTSync(mapper);
5453 processSync(mapper);
5454
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5456 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5457 motionArgs.action);
5458 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5459 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5461 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5464 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5466 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5467
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5470 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5471 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5474 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5475
5476 // Move.
5477 x2 += 20; y2 -= 25;
5478 processPosition(mapper, x2, y2);
5479 processId(mapper, 2);
5480 processMTSync(mapper);
5481 processSync(mapper);
5482
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5485 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5486 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5489 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5490
5491 // New finger down.
5492 int32_t x3 = 700, y3 = 300;
5493 processPosition(mapper, x2, y2);
5494 processId(mapper, 2);
5495 processMTSync(mapper);
5496 processPosition(mapper, x3, y3);
5497 processId(mapper, 3);
5498 processMTSync(mapper);
5499 processSync(mapper);
5500
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5502 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5503 motionArgs.action);
5504 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5505 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5507 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5510 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5512 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5513
5514 // Second finger up.
5515 x3 += 30; y3 -= 20;
5516 processPosition(mapper, x3, y3);
5517 processId(mapper, 3);
5518 processMTSync(mapper);
5519 processSync(mapper);
5520
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5522 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5523 motionArgs.action);
5524 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5525 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5527 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5530 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5532 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5533
5534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5536 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5537 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5540 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5541
5542 // Last finger up.
5543 processMTSync(mapper);
5544 processSync(mapper);
5545
5546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5547 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5548 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5549 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5552 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5553
5554 // Should not have sent any more keys or motions.
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5557}
5558
5559TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5560 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5561 addConfigurationProperty("touch.deviceType", "touchScreen");
5562 prepareDisplay(DISPLAY_ORIENTATION_0);
5563 prepareAxes(POSITION | ID | SLOT);
5564 prepareVirtualKeys();
5565 addMapperAndConfigure(mapper);
5566
5567 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5568
5569 NotifyMotionArgs motionArgs;
5570
5571 // Two fingers down at once.
5572 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5573 processPosition(mapper, x1, y1);
5574 processId(mapper, 1);
5575 processSlot(mapper, 1);
5576 processPosition(mapper, x2, y2);
5577 processId(mapper, 2);
5578 processSync(mapper);
5579
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5581 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5582 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5583 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5586 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5587
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5589 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5590 motionArgs.action);
5591 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5592 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5594 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5597 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5599 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5600
5601 // Move.
5602 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5603 processSlot(mapper, 0);
5604 processPosition(mapper, x1, y1);
5605 processSlot(mapper, 1);
5606 processPosition(mapper, x2, y2);
5607 processSync(mapper);
5608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5611 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5612 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5614 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5617 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5619 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5620
5621 // First finger up.
5622 x2 += 15; y2 -= 20;
5623 processSlot(mapper, 0);
5624 processId(mapper, -1);
5625 processSlot(mapper, 1);
5626 processPosition(mapper, x2, y2);
5627 processSync(mapper);
5628
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5631 motionArgs.action);
5632 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5635 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5638 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5640 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5641
5642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5644 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5645 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5648 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5649
5650 // Move.
5651 x2 += 20; y2 -= 25;
5652 processPosition(mapper, x2, y2);
5653 processSync(mapper);
5654
5655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5657 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5658 ASSERT_EQ(1, 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(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5662
5663 // New finger down.
5664 int32_t x3 = 700, y3 = 300;
5665 processPosition(mapper, x2, y2);
5666 processSlot(mapper, 0);
5667 processId(mapper, 3);
5668 processPosition(mapper, x3, y3);
5669 processSync(mapper);
5670
5671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5672 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5673 motionArgs.action);
5674 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5675 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5677 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5680 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5682 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5683
5684 // Second finger up.
5685 x3 += 30; y3 -= 20;
5686 processSlot(mapper, 1);
5687 processId(mapper, -1);
5688 processSlot(mapper, 0);
5689 processPosition(mapper, x3, y3);
5690 processSync(mapper);
5691
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5693 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5694 motionArgs.action);
5695 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5696 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5698 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5701 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5703 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5704
5705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5707 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5708 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5711 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5712
5713 // Last finger up.
5714 processId(mapper, -1);
5715 processSync(mapper);
5716
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5718 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5719 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5720 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5723 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5724
5725 // Should not have sent any more keys or motions.
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5728}
5729
5730TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5731 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5732 addConfigurationProperty("touch.deviceType", "touchScreen");
5733 prepareDisplay(DISPLAY_ORIENTATION_0);
5734 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5735 addMapperAndConfigure(mapper);
5736
5737 // These calculations are based on the input device calibration documentation.
5738 int32_t rawX = 100;
5739 int32_t rawY = 200;
5740 int32_t rawTouchMajor = 7;
5741 int32_t rawTouchMinor = 6;
5742 int32_t rawToolMajor = 9;
5743 int32_t rawToolMinor = 8;
5744 int32_t rawPressure = 11;
5745 int32_t rawDistance = 0;
5746 int32_t rawOrientation = 3;
5747 int32_t id = 5;
5748
5749 float x = toDisplayX(rawX);
5750 float y = toDisplayY(rawY);
5751 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5752 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5753 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5754 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5755 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5756 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5757 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5758 float distance = float(rawDistance);
5759
5760 processPosition(mapper, rawX, rawY);
5761 processTouchMajor(mapper, rawTouchMajor);
5762 processTouchMinor(mapper, rawTouchMinor);
5763 processToolMajor(mapper, rawToolMajor);
5764 processToolMinor(mapper, rawToolMinor);
5765 processPressure(mapper, rawPressure);
5766 processOrientation(mapper, rawOrientation);
5767 processDistance(mapper, rawDistance);
5768 processId(mapper, id);
5769 processMTSync(mapper);
5770 processSync(mapper);
5771
5772 NotifyMotionArgs args;
5773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5774 ASSERT_EQ(0, args.pointerProperties[0].id);
5775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5776 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5777 orientation, distance));
5778}
5779
5780TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5781 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5782 addConfigurationProperty("touch.deviceType", "touchScreen");
5783 prepareDisplay(DISPLAY_ORIENTATION_0);
5784 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5785 addConfigurationProperty("touch.size.calibration", "geometric");
5786 addMapperAndConfigure(mapper);
5787
5788 // These calculations are based on the input device calibration documentation.
5789 int32_t rawX = 100;
5790 int32_t rawY = 200;
5791 int32_t rawTouchMajor = 140;
5792 int32_t rawTouchMinor = 120;
5793 int32_t rawToolMajor = 180;
5794 int32_t rawToolMinor = 160;
5795
5796 float x = toDisplayX(rawX);
5797 float y = toDisplayY(rawY);
5798 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5799 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5800 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5801 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5802 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5803
5804 processPosition(mapper, rawX, rawY);
5805 processTouchMajor(mapper, rawTouchMajor);
5806 processTouchMinor(mapper, rawTouchMinor);
5807 processToolMajor(mapper, rawToolMajor);
5808 processToolMinor(mapper, rawToolMinor);
5809 processMTSync(mapper);
5810 processSync(mapper);
5811
5812 NotifyMotionArgs args;
5813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5815 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5816}
5817
5818TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5819 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5820 addConfigurationProperty("touch.deviceType", "touchScreen");
5821 prepareDisplay(DISPLAY_ORIENTATION_0);
5822 prepareAxes(POSITION | TOUCH | TOOL);
5823 addConfigurationProperty("touch.size.calibration", "diameter");
5824 addConfigurationProperty("touch.size.scale", "10");
5825 addConfigurationProperty("touch.size.bias", "160");
5826 addConfigurationProperty("touch.size.isSummed", "1");
5827 addMapperAndConfigure(mapper);
5828
5829 // These calculations are based on the input device calibration documentation.
5830 // Note: We only provide a single common touch/tool value because the device is assumed
5831 // not to emit separate values for each pointer (isSummed = 1).
5832 int32_t rawX = 100;
5833 int32_t rawY = 200;
5834 int32_t rawX2 = 150;
5835 int32_t rawY2 = 250;
5836 int32_t rawTouchMajor = 5;
5837 int32_t rawToolMajor = 8;
5838
5839 float x = toDisplayX(rawX);
5840 float y = toDisplayY(rawY);
5841 float x2 = toDisplayX(rawX2);
5842 float y2 = toDisplayY(rawY2);
5843 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5844 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5845 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5846
5847 processPosition(mapper, rawX, rawY);
5848 processTouchMajor(mapper, rawTouchMajor);
5849 processToolMajor(mapper, rawToolMajor);
5850 processMTSync(mapper);
5851 processPosition(mapper, rawX2, rawY2);
5852 processTouchMajor(mapper, rawTouchMajor);
5853 processToolMajor(mapper, rawToolMajor);
5854 processMTSync(mapper);
5855 processSync(mapper);
5856
5857 NotifyMotionArgs args;
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5859 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5863 args.action);
5864 ASSERT_EQ(size_t(2), args.pointerCount);
5865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5866 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5868 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5869}
5870
5871TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5872 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5873 addConfigurationProperty("touch.deviceType", "touchScreen");
5874 prepareDisplay(DISPLAY_ORIENTATION_0);
5875 prepareAxes(POSITION | TOUCH | TOOL);
5876 addConfigurationProperty("touch.size.calibration", "area");
5877 addConfigurationProperty("touch.size.scale", "43");
5878 addConfigurationProperty("touch.size.bias", "3");
5879 addMapperAndConfigure(mapper);
5880
5881 // These calculations are based on the input device calibration documentation.
5882 int32_t rawX = 100;
5883 int32_t rawY = 200;
5884 int32_t rawTouchMajor = 5;
5885 int32_t rawToolMajor = 8;
5886
5887 float x = toDisplayX(rawX);
5888 float y = toDisplayY(rawY);
5889 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5890 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5891 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5892
5893 processPosition(mapper, rawX, rawY);
5894 processTouchMajor(mapper, rawTouchMajor);
5895 processToolMajor(mapper, rawToolMajor);
5896 processMTSync(mapper);
5897 processSync(mapper);
5898
5899 NotifyMotionArgs args;
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5902 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5903}
5904
5905TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5906 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5907 addConfigurationProperty("touch.deviceType", "touchScreen");
5908 prepareDisplay(DISPLAY_ORIENTATION_0);
5909 prepareAxes(POSITION | PRESSURE);
5910 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5911 addConfigurationProperty("touch.pressure.scale", "0.01");
5912 addMapperAndConfigure(mapper);
5913
Michael Wrightaa449c92017-12-13 21:21:43 +00005914 InputDeviceInfo info;
5915 mapper->populateDeviceInfo(&info);
5916 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5917 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5918 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5919
Michael Wrightd02c5b62014-02-10 15:10:22 -08005920 // These calculations are based on the input device calibration documentation.
5921 int32_t rawX = 100;
5922 int32_t rawY = 200;
5923 int32_t rawPressure = 60;
5924
5925 float x = toDisplayX(rawX);
5926 float y = toDisplayY(rawY);
5927 float pressure = float(rawPressure) * 0.01f;
5928
5929 processPosition(mapper, rawX, rawY);
5930 processPressure(mapper, rawPressure);
5931 processMTSync(mapper);
5932 processSync(mapper);
5933
5934 NotifyMotionArgs args;
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5937 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5938}
5939
5940TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5941 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5942 addConfigurationProperty("touch.deviceType", "touchScreen");
5943 prepareDisplay(DISPLAY_ORIENTATION_0);
5944 prepareAxes(POSITION | ID | SLOT);
5945 addMapperAndConfigure(mapper);
5946
5947 NotifyMotionArgs motionArgs;
5948 NotifyKeyArgs keyArgs;
5949
5950 processId(mapper, 1);
5951 processPosition(mapper, 100, 200);
5952 processSync(mapper);
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5954 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5955 ASSERT_EQ(0, motionArgs.buttonState);
5956
5957 // press BTN_LEFT, release BTN_LEFT
5958 processKey(mapper, BTN_LEFT, 1);
5959 processSync(mapper);
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5961 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5962 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5963
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5966 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5967
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968 processKey(mapper, BTN_LEFT, 0);
5969 processSync(mapper);
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005971 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005973
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005976 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977
5978 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5979 processKey(mapper, BTN_RIGHT, 1);
5980 processKey(mapper, BTN_MIDDLE, 1);
5981 processSync(mapper);
5982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5984 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5985 motionArgs.buttonState);
5986
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5989 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5990
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5992 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5993 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5994 motionArgs.buttonState);
5995
Michael Wrightd02c5b62014-02-10 15:10:22 -08005996 processKey(mapper, BTN_RIGHT, 0);
5997 processSync(mapper);
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006001
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006004 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005
6006 processKey(mapper, BTN_MIDDLE, 0);
6007 processSync(mapper);
6008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006009 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006011
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006014 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015
6016 // press BTN_BACK, release BTN_BACK
6017 processKey(mapper, BTN_BACK, 1);
6018 processSync(mapper);
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6020 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6021 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006022
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006025 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6026
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6029 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030
6031 processKey(mapper, BTN_BACK, 0);
6032 processSync(mapper);
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006036
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006039 ASSERT_EQ(0, motionArgs.buttonState);
6040
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6042 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6043 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6044
6045 // press BTN_SIDE, release BTN_SIDE
6046 processKey(mapper, BTN_SIDE, 1);
6047 processSync(mapper);
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6049 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6050 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006051
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006054 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6055
6056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6057 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6058 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059
6060 processKey(mapper, BTN_SIDE, 0);
6061 processSync(mapper);
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006065
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006068 ASSERT_EQ(0, motionArgs.buttonState);
6069
Michael Wrightd02c5b62014-02-10 15:10:22 -08006070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6071 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6072 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6073
6074 // press BTN_FORWARD, release BTN_FORWARD
6075 processKey(mapper, BTN_FORWARD, 1);
6076 processSync(mapper);
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6078 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6079 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006080
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006083 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6084
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6087 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088
6089 processKey(mapper, BTN_FORWARD, 0);
6090 processSync(mapper);
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006093 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006094
6095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006097 ASSERT_EQ(0, motionArgs.buttonState);
6098
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6100 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6101 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6102
6103 // press BTN_EXTRA, release BTN_EXTRA
6104 processKey(mapper, BTN_EXTRA, 1);
6105 processSync(mapper);
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6107 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6108 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006109
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006112 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6113
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6116 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117
6118 processKey(mapper, BTN_EXTRA, 0);
6119 processSync(mapper);
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006122 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006123
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006126 ASSERT_EQ(0, motionArgs.buttonState);
6127
Michael Wrightd02c5b62014-02-10 15:10:22 -08006128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6129 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6130 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6131
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6133
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 // press BTN_STYLUS, release BTN_STYLUS
6135 processKey(mapper, BTN_STYLUS, 1);
6136 processSync(mapper);
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006139 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6140
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6143 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144
6145 processKey(mapper, BTN_STYLUS, 0);
6146 processSync(mapper);
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006150
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006153 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154
6155 // press BTN_STYLUS2, release BTN_STYLUS2
6156 processKey(mapper, BTN_STYLUS2, 1);
6157 processSync(mapper);
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006160 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6161
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6163 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6164 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165
6166 processKey(mapper, BTN_STYLUS2, 0);
6167 processSync(mapper);
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006169 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006171
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006174 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175
6176 // release touch
6177 processId(mapper, -1);
6178 processSync(mapper);
6179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6180 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6181 ASSERT_EQ(0, motionArgs.buttonState);
6182}
6183
6184TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
6185 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6186 addConfigurationProperty("touch.deviceType", "touchScreen");
6187 prepareDisplay(DISPLAY_ORIENTATION_0);
6188 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6189 addMapperAndConfigure(mapper);
6190
6191 NotifyMotionArgs motionArgs;
6192
6193 // default tool type is finger
6194 processId(mapper, 1);
6195 processPosition(mapper, 100, 200);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6200
6201 // eraser
6202 processKey(mapper, BTN_TOOL_RUBBER, 1);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6207
6208 // stylus
6209 processKey(mapper, BTN_TOOL_RUBBER, 0);
6210 processKey(mapper, BTN_TOOL_PEN, 1);
6211 processSync(mapper);
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6215
6216 // brush
6217 processKey(mapper, BTN_TOOL_PEN, 0);
6218 processKey(mapper, BTN_TOOL_BRUSH, 1);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6223
6224 // pencil
6225 processKey(mapper, BTN_TOOL_BRUSH, 0);
6226 processKey(mapper, BTN_TOOL_PENCIL, 1);
6227 processSync(mapper);
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6231
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006232 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 processKey(mapper, BTN_TOOL_PENCIL, 0);
6234 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6235 processSync(mapper);
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6239
6240 // mouse
6241 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6242 processKey(mapper, BTN_TOOL_MOUSE, 1);
6243 processSync(mapper);
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6246 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6247
6248 // lens
6249 processKey(mapper, BTN_TOOL_MOUSE, 0);
6250 processKey(mapper, BTN_TOOL_LENS, 1);
6251 processSync(mapper);
6252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6255
6256 // double-tap
6257 processKey(mapper, BTN_TOOL_LENS, 0);
6258 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6263
6264 // triple-tap
6265 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6266 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6267 processSync(mapper);
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6271
6272 // quad-tap
6273 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6274 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6279
6280 // finger
6281 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6282 processKey(mapper, BTN_TOOL_FINGER, 1);
6283 processSync(mapper);
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6287
6288 // stylus trumps finger
6289 processKey(mapper, BTN_TOOL_PEN, 1);
6290 processSync(mapper);
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6294
6295 // eraser trumps stylus
6296 processKey(mapper, BTN_TOOL_RUBBER, 1);
6297 processSync(mapper);
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6301
6302 // mouse trumps eraser
6303 processKey(mapper, BTN_TOOL_MOUSE, 1);
6304 processSync(mapper);
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6308
6309 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6310 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6311 processSync(mapper);
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6315
6316 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6317 processToolType(mapper, MT_TOOL_PEN);
6318 processSync(mapper);
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6322
6323 // back to default tool type
6324 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6325 processKey(mapper, BTN_TOOL_MOUSE, 0);
6326 processKey(mapper, BTN_TOOL_RUBBER, 0);
6327 processKey(mapper, BTN_TOOL_PEN, 0);
6328 processKey(mapper, BTN_TOOL_FINGER, 0);
6329 processSync(mapper);
6330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6333}
6334
6335TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6336 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6337 addConfigurationProperty("touch.deviceType", "touchScreen");
6338 prepareDisplay(DISPLAY_ORIENTATION_0);
6339 prepareAxes(POSITION | ID | SLOT);
6340 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6341 addMapperAndConfigure(mapper);
6342
6343 NotifyMotionArgs motionArgs;
6344
6345 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6346 processId(mapper, 1);
6347 processPosition(mapper, 100, 200);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6352 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6353
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6355 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6357 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6358
6359 // move a little
6360 processPosition(mapper, 150, 250);
6361 processSync(mapper);
6362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6365 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6366
6367 // down when BTN_TOUCH is pressed, pressure defaults to 1
6368 processKey(mapper, BTN_TOUCH, 1);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6371 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6373 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6374
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6376 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6378 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6379
6380 // up when BTN_TOUCH is released, hover restored
6381 processKey(mapper, BTN_TOUCH, 0);
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6384 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6386 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6387
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6389 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6391 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6392
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6396 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6397
6398 // exit hover when pointer goes away
6399 processId(mapper, -1);
6400 processSync(mapper);
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6404 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6405}
6406
6407TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6408 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6409 addConfigurationProperty("touch.deviceType", "touchScreen");
6410 prepareDisplay(DISPLAY_ORIENTATION_0);
6411 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6412 addMapperAndConfigure(mapper);
6413
6414 NotifyMotionArgs motionArgs;
6415
6416 // initially hovering because pressure is 0
6417 processId(mapper, 1);
6418 processPosition(mapper, 100, 200);
6419 processPressure(mapper, 0);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6424 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6425
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6429 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6430
6431 // move a little
6432 processPosition(mapper, 150, 250);
6433 processSync(mapper);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6435 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6437 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6438
6439 // down when pressure becomes non-zero
6440 processPressure(mapper, RAW_PRESSURE_MAX);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6445 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6446
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6450 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6451
6452 // up when pressure becomes 0, hover restored
6453 processPressure(mapper, 0);
6454 processSync(mapper);
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6456 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6458 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6459
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6463 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6464
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6468 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6469
6470 // exit hover when pointer goes away
6471 processId(mapper, -1);
6472 processSync(mapper);
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6476 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6477}
6478
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006479/**
6480 * Set the input device port <--> display port associations, and check that the
6481 * events are routed to the display that matches the display port.
6482 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6483 */
6484TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6485 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6486 const std::string usb2 = "USB2";
6487 const uint8_t hdmi1 = 0;
6488 const uint8_t hdmi2 = 1;
6489 const std::string secondaryUniqueId = "uniqueId2";
6490 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6491
6492 addConfigurationProperty("touch.deviceType", "touchScreen");
6493 prepareAxes(POSITION);
6494 addMapperAndConfigure(mapper);
6495
6496 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6497 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6498
6499 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6500 // for this input device is specified, and the matching viewport is not present,
6501 // the input device should be disabled (at the mapper level).
6502
6503 // Add viewport for display 2 on hdmi2
6504 prepareSecondaryDisplay(type, hdmi2);
6505 // Send a touch event
6506 processPosition(mapper, 100, 100);
6507 processSync(mapper);
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6509
6510 // Add viewport for display 1 on hdmi1
6511 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6512 // Send a touch event again
6513 processPosition(mapper, 100, 100);
6514 processSync(mapper);
6515
6516 NotifyMotionArgs args;
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6518 ASSERT_EQ(DISPLAY_ID, args.displayId);
6519}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520
Arthur Hung41a712e2018-11-22 19:41:03 +08006521/**
6522 * Expect fallback to internal viewport if device is external and external viewport is not present.
6523 */
6524TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6525 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6526 prepareAxes(POSITION);
6527 addConfigurationProperty("touch.deviceType", "touchScreen");
6528 prepareDisplay(DISPLAY_ORIENTATION_0);
6529 mDevice->setExternal(true);
6530 addMapperAndConfigure(mapper);
6531
6532 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6533
6534 NotifyMotionArgs motionArgs;
6535
6536 // Expect the event to be sent to the internal viewport,
6537 // because an external viewport is not present.
6538 processPosition(mapper, 100, 100);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6542
6543 // Expect the event to be sent to the external viewport if it is present.
6544 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6545 processPosition(mapper, 100, 100);
6546 processSync(mapper);
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6548 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6549}
6550
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006551TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006552 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006553 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006554 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006555 fakePointerController->setPosition(100, 200);
6556 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006557 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6558
Garfield Tan888a6a42020-01-09 11:39:16 -08006559 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6560 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6561
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006562 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6563 prepareDisplay(DISPLAY_ORIENTATION_0);
6564 prepareAxes(POSITION);
6565 addMapperAndConfigure(mapper);
6566
6567 // Check source is mouse that would obtain the PointerController.
6568 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6569
6570 NotifyMotionArgs motionArgs;
6571 processPosition(mapper, 100, 100);
6572 processSync(mapper);
6573
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6575 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6576 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6577}
6578
Arthur Hung7c645402019-01-25 17:45:42 +08006579TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6580 // Setup the first touch screen device.
6581 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6582 prepareAxes(POSITION | ID | SLOT);
6583 addConfigurationProperty("touch.deviceType", "touchScreen");
6584 addMapperAndConfigure(mapper);
6585
6586 // Create the second touch screen device, and enable multi fingers.
6587 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006588 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006589 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006590 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006591 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006592 std::unique_ptr<InputDevice> device2 =
6593 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6594 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006595 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6596 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6597 0 /*flat*/, 0 /*fuzz*/);
6598 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6599 0 /*flat*/, 0 /*fuzz*/);
6600 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6601 0 /*flat*/, 0 /*fuzz*/);
6602 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6603 0 /*flat*/, 0 /*fuzz*/);
6604 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6605 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6606 String8("touchScreen"));
6607
6608 // Setup the second touch screen device.
Arthur Hung2c9a3342019-07-23 14:18:59 +08006609 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2.get());
Arthur Hung7c645402019-01-25 17:45:42 +08006610 device2->addMapper(mapper2);
6611 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6612 device2->reset(ARBITRARY_TIME);
6613
6614 // Setup PointerController.
6615 sp<FakePointerController> fakePointerController = new FakePointerController();
6616 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6617 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6618
6619 // Setup policy for associated displays and show touches.
6620 const uint8_t hdmi1 = 0;
6621 const uint8_t hdmi2 = 1;
6622 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6623 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6624 mFakePolicy->setShowTouches(true);
6625
6626 // Create displays.
6627 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6628 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6629
6630 // Default device will reconfigure above, need additional reconfiguration for another device.
6631 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6632 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6633
6634 // Two fingers down at default display.
6635 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6636 processPosition(mapper, x1, y1);
6637 processId(mapper, 1);
6638 processSlot(mapper, 1);
6639 processPosition(mapper, x2, y2);
6640 processId(mapper, 2);
6641 processSync(mapper);
6642
6643 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6644 fakePointerController->getSpots().find(DISPLAY_ID);
6645 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6646 ASSERT_EQ(size_t(2), iter->second.size());
6647
6648 // Two fingers down at second display.
6649 processPosition(mapper2, x1, y1);
6650 processId(mapper2, 1);
6651 processSlot(mapper2, 1);
6652 processPosition(mapper2, x2, y2);
6653 processId(mapper2, 2);
6654 processSync(mapper2);
6655
6656 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6657 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6658 ASSERT_EQ(size_t(2), iter->second.size());
6659}
6660
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006661TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6662 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6663 prepareAxes(POSITION);
6664 addConfigurationProperty("touch.deviceType", "touchScreen");
6665 prepareDisplay(DISPLAY_ORIENTATION_0);
6666 addMapperAndConfigure(mapper);
6667
6668 NotifyMotionArgs motionArgs;
6669 // Unrotated video frame
6670 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6671 std::vector<TouchVideoFrame> frames{frame};
6672 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6673 processPosition(mapper, 100, 200);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(frames, motionArgs.videoFrames);
6677
6678 // Subsequent touch events should not have any videoframes
6679 // This is implemented separately in FakeEventHub,
6680 // but that should match the behaviour of TouchVideoDevice.
6681 processPosition(mapper, 200, 200);
6682 processSync(mapper);
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6684 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6685}
6686
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006687TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6688 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6689 prepareAxes(POSITION);
6690 addConfigurationProperty("touch.deviceType", "touchScreen");
6691 addMapperAndConfigure(mapper);
6692 // Unrotated video frame
6693 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6694 NotifyMotionArgs motionArgs;
6695
6696 // Test all 4 orientations
6697 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6698 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6699 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6700 clearViewports();
6701 prepareDisplay(orientation);
6702 std::vector<TouchVideoFrame> frames{frame};
6703 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6704 processPosition(mapper, 100, 200);
6705 processSync(mapper);
6706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6707 frames[0].rotate(orientation);
6708 ASSERT_EQ(frames, motionArgs.videoFrames);
6709 }
6710}
6711
6712TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6713 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6714 prepareAxes(POSITION);
6715 addConfigurationProperty("touch.deviceType", "touchScreen");
6716 addMapperAndConfigure(mapper);
6717 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6718 // so mix these.
6719 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6720 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6721 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6722 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6723 NotifyMotionArgs motionArgs;
6724
6725 prepareDisplay(DISPLAY_ORIENTATION_90);
6726 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6727 processPosition(mapper, 100, 200);
6728 processSync(mapper);
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6730 std::for_each(frames.begin(), frames.end(),
6731 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6732 ASSERT_EQ(frames, motionArgs.videoFrames);
6733}
6734
Arthur Hung9da14732019-09-02 16:16:58 +08006735/**
6736 * If we had defined port associations, but the viewport is not ready, the touch device would be
6737 * expected to be disabled, and it should be enabled after the viewport has found.
6738 */
6739TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
6740 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6741 constexpr uint8_t hdmi2 = 1;
6742 const std::string secondaryUniqueId = "uniqueId2";
6743 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6744
6745 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6746
6747 addConfigurationProperty("touch.deviceType", "touchScreen");
6748 prepareAxes(POSITION);
6749 addMapperAndConfigure(mapper);
6750
6751 ASSERT_EQ(mDevice->isEnabled(), false);
6752
6753 // Add display on hdmi2, the device should be enabled and can receive touch event.
6754 prepareSecondaryDisplay(type, hdmi2);
6755 ASSERT_EQ(mDevice->isEnabled(), true);
6756
6757 // Send a touch event.
6758 processPosition(mapper, 100, 100);
6759 processSync(mapper);
6760
6761 NotifyMotionArgs args;
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6763 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6764}
6765
Arthur Hung6cd19a42019-08-30 19:04:12 +08006766/**
6767 * Test touch should not work if outside of surface.
6768 */
6769TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
6770 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6771 addConfigurationProperty("touch.deviceType", "touchScreen");
6772 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006773 prepareAxes(POSITION);
6774 addMapperAndConfigure(mapper);
6775
Arthur Hung05de5772019-09-26 18:31:26 +08006776 // Touch on left-top area should work.
6777 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6778 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6779 processPosition(mapper, rawX, rawY);
6780 processSync(mapper);
6781
6782 NotifyMotionArgs args;
6783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6784
6785 // Reset.
6786 mapper->reset(ARBITRARY_TIME);
6787
6788 // Let logical display be different to physical display and rotate 90-degrees.
6789 std::optional<DisplayViewport> internalViewport =
6790 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6791 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6792 internalViewport->logicalLeft = 0;
6793 internalViewport->logicalTop = 0;
6794 internalViewport->logicalRight = DISPLAY_HEIGHT;
6795 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6796
6797 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6798 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6799 internalViewport->physicalRight = DISPLAY_HEIGHT;
6800 internalViewport->physicalBottom = DISPLAY_WIDTH;
6801
6802 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6803 internalViewport->deviceHeight = DISPLAY_WIDTH;
6804 mFakePolicy->updateViewport(internalViewport.value());
6805 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6806
6807 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006808 processPosition(mapper, rawX, rawY);
6809 processSync(mapper);
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6811}
6812
Arthur Hung421eb1c2020-01-16 00:09:42 +08006813TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
6814 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6815 addConfigurationProperty("touch.deviceType", "touchScreen");
6816 prepareDisplay(DISPLAY_ORIENTATION_0);
6817 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6818 addMapperAndConfigure(mapper);
6819
6820 NotifyMotionArgs motionArgs;
6821
6822 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6823 // finger down
6824 processId(mapper, 1);
6825 processPosition(mapper, x1, y1);
6826 processSync(mapper);
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6828 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6830
6831 // finger move
6832 processId(mapper, 1);
6833 processPosition(mapper, x2, y2);
6834 processSync(mapper);
6835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6837 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6838
6839 // finger up.
6840 processId(mapper, -1);
6841 processSync(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6843 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6845
6846 // new finger down
6847 processId(mapper, 1);
6848 processPosition(mapper, x3, y3);
6849 processSync(mapper);
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6851 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6853}
6854
6855/**
6856 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
6857 * UP events should be ignored.
6858 */
6859TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
6860 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6861 addConfigurationProperty("touch.deviceType", "touchScreen");
6862 prepareDisplay(DISPLAY_ORIENTATION_0);
6863 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6864 addMapperAndConfigure(mapper);
6865
6866 NotifyMotionArgs motionArgs;
6867
6868 // default tool type is finger
6869 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6870 processId(mapper, 1);
6871 processPosition(mapper, x1, y1);
6872 processSync(mapper);
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6874 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6876
6877 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
6878 processToolType(mapper, MT_TOOL_PALM);
6879 processSync(mapper);
6880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6881 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6882
6883 // Ignore the following MOVE and UP events if had detect a palm event.
6884 processId(mapper, 1);
6885 processPosition(mapper, x2, y2);
6886 processSync(mapper);
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6888
6889 // finger up.
6890 processId(mapper, -1);
6891 processSync(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6893
6894 // new finger down
6895 processToolType(mapper, MT_TOOL_FINGER);
6896 processId(mapper, 1);
6897 processPosition(mapper, x3, y3);
6898 processSync(mapper);
6899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6900 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6902}
6903
Michael Wrightd02c5b62014-02-10 15:10:22 -08006904} // namespace android