blob: c1c912214a055e22861d1b4f07ae29106de396ac [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
Arthur Hungc7ad2d02018-12-18 17:41:29 +080093 void setDisplayId(int32_t displayId) {
94 mDisplayId = displayId;
95 }
96
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 virtual void setPosition(float x, float y) {
98 mX = x;
99 mY = y;
100 }
101
102 virtual void setButtonState(int32_t buttonState) {
103 mButtonState = buttonState;
104 }
105
106 virtual int32_t getButtonState() const {
107 return mButtonState;
108 }
109
110 virtual void getPosition(float* outX, float* outY) const {
111 *outX = mX;
112 *outY = mY;
113 }
114
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800115 virtual int32_t getDisplayId() const {
116 return mDisplayId;
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
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700284 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700285 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
286 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700287 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
288 || orientation == DISPLAY_ORIENTATION_270);
289 DisplayViewport v;
290 v.displayId = displayId;
291 v.orientation = orientation;
292 v.logicalLeft = 0;
293 v.logicalTop = 0;
294 v.logicalRight = isRotated ? height : width;
295 v.logicalBottom = isRotated ? width : height;
296 v.physicalLeft = 0;
297 v.physicalTop = 0;
298 v.physicalRight = isRotated ? height : width;
299 v.physicalBottom = isRotated ? width : height;
300 v.deviceWidth = isRotated ? height : width;
301 v.deviceHeight = isRotated ? width : height;
302 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700303 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100304 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700305 return v;
306 }
307
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
309 *outConfig = mConfig;
310 }
311
312 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
313 return mPointerControllers.valueFor(deviceId);
314 }
315
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800316 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700317 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700319 mInputDevicesChanged = true;
320 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 }
322
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100323 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700324 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 }
326
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100327 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
328 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 }
330};
331
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332// --- FakeEventHub ---
333
334class FakeEventHub : public EventHubInterface {
335 struct KeyInfo {
336 int32_t keyCode;
337 uint32_t flags;
338 };
339
340 struct Device {
341 InputDeviceIdentifier identifier;
342 uint32_t classes;
343 PropertyMap configuration;
344 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
345 KeyedVector<int, bool> relativeAxes;
346 KeyedVector<int32_t, int32_t> keyCodeStates;
347 KeyedVector<int32_t, int32_t> scanCodeStates;
348 KeyedVector<int32_t, int32_t> switchStates;
349 KeyedVector<int32_t, int32_t> absoluteAxisValue;
350 KeyedVector<int32_t, KeyInfo> keysByScanCode;
351 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
352 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800353 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700354 bool enabled;
355
356 status_t enable() {
357 enabled = true;
358 return OK;
359 }
360
361 status_t disable() {
362 enabled = false;
363 return OK;
364 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700366 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700367 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368 }
369 };
370
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700371 std::mutex mLock;
372 std::condition_variable mEventsCondition;
373
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100375 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700376 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600377 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700379public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380 virtual ~FakeEventHub() {
381 for (size_t i = 0; i < mDevices.size(); i++) {
382 delete mDevices.valueAt(i);
383 }
384 }
385
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386 FakeEventHub() { }
387
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100388 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 Device* device = new Device(classes);
390 device->identifier.name = name;
391 mDevices.add(deviceId, device);
392
393 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
394 }
395
396 void removeDevice(int32_t deviceId) {
397 delete mDevices.valueFor(deviceId);
398 mDevices.removeItem(deviceId);
399
400 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
401 }
402
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700403 bool isDeviceEnabled(int32_t deviceId) {
404 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700405 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700406 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
407 return false;
408 }
409 return device->enabled;
410 }
411
412 status_t enableDevice(int32_t deviceId) {
413 status_t result;
414 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700415 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700416 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
417 return BAD_VALUE;
418 }
419 if (device->enabled) {
420 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
421 return OK;
422 }
423 result = device->enable();
424 return result;
425 }
426
427 status_t disableDevice(int32_t deviceId) {
428 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700429 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700430 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
431 return BAD_VALUE;
432 }
433 if (!device->enabled) {
434 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
435 return OK;
436 }
437 return device->disable();
438 }
439
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440 void finishDeviceScan() {
441 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
442 }
443
444 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
445 Device* device = getDevice(deviceId);
446 device->configuration.addProperty(key, value);
447 }
448
449 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
450 Device* device = getDevice(deviceId);
451 device->configuration.addAll(configuration);
452 }
453
454 void addAbsoluteAxis(int32_t deviceId, int axis,
455 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
456 Device* device = getDevice(deviceId);
457
458 RawAbsoluteAxisInfo info;
459 info.valid = true;
460 info.minValue = minValue;
461 info.maxValue = maxValue;
462 info.flat = flat;
463 info.fuzz = fuzz;
464 info.resolution = resolution;
465 device->absoluteAxes.add(axis, info);
466 }
467
468 void addRelativeAxis(int32_t deviceId, int32_t axis) {
469 Device* device = getDevice(deviceId);
470 device->relativeAxes.add(axis, true);
471 }
472
473 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
474 Device* device = getDevice(deviceId);
475 device->keyCodeStates.replaceValueFor(keyCode, state);
476 }
477
478 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
479 Device* device = getDevice(deviceId);
480 device->scanCodeStates.replaceValueFor(scanCode, state);
481 }
482
483 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
484 Device* device = getDevice(deviceId);
485 device->switchStates.replaceValueFor(switchCode, state);
486 }
487
488 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
489 Device* device = getDevice(deviceId);
490 device->absoluteAxisValue.replaceValueFor(axis, value);
491 }
492
493 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
494 int32_t keyCode, uint32_t flags) {
495 Device* device = getDevice(deviceId);
496 KeyInfo info;
497 info.keyCode = keyCode;
498 info.flags = flags;
499 if (scanCode) {
500 device->keysByScanCode.add(scanCode, info);
501 }
502 if (usageCode) {
503 device->keysByUsageCode.add(usageCode, info);
504 }
505 }
506
507 void addLed(int32_t deviceId, int32_t led, bool initialState) {
508 Device* device = getDevice(deviceId);
509 device->leds.add(led, initialState);
510 }
511
512 bool getLedState(int32_t deviceId, int32_t led) {
513 Device* device = getDevice(deviceId);
514 return device->leds.valueFor(led);
515 }
516
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100517 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518 return mExcludedDevices;
519 }
520
521 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
522 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800523 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524 }
525
526 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
527 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700528 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 RawEvent event;
530 event.when = when;
531 event.deviceId = deviceId;
532 event.type = type;
533 event.code = code;
534 event.value = value;
535 mEvents.push_back(event);
536
537 if (type == EV_ABS) {
538 setAbsoluteAxisValue(deviceId, code, value);
539 }
540 }
541
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600542 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
543 std::vector<TouchVideoFrame>> videoFrames) {
544 mVideoFrames = std::move(videoFrames);
545 }
546
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700548 std::unique_lock<std::mutex> lock(mLock);
549 base::ScopedLockAssertion assumeLocked(mLock);
550 const bool queueIsEmpty =
551 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
552 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
553 if (!queueIsEmpty) {
554 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
555 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556 }
557
558private:
559 Device* getDevice(int32_t deviceId) const {
560 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100561 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 }
563
564 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
565 Device* device = getDevice(deviceId);
566 return device ? device->classes : 0;
567 }
568
569 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
570 Device* device = getDevice(deviceId);
571 return device ? device->identifier : InputDeviceIdentifier();
572 }
573
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100574 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800575 return 0;
576 }
577
578 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
579 Device* device = getDevice(deviceId);
580 if (device) {
581 *outConfiguration = device->configuration;
582 }
583 }
584
585 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
586 RawAbsoluteAxisInfo* outAxisInfo) const {
587 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800588 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 ssize_t index = device->absoluteAxes.indexOfKey(axis);
590 if (index >= 0) {
591 *outAxisInfo = device->absoluteAxes.valueAt(index);
592 return OK;
593 }
594 }
595 outAxisInfo->clear();
596 return -1;
597 }
598
599 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
600 Device* device = getDevice(deviceId);
601 if (device) {
602 return device->relativeAxes.indexOfKey(axis) >= 0;
603 }
604 return false;
605 }
606
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100607 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608 return false;
609 }
610
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700611 virtual status_t mapKey(int32_t deviceId,
612 int32_t scanCode, int32_t usageCode, int32_t metaState,
613 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614 Device* device = getDevice(deviceId);
615 if (device) {
616 const KeyInfo* key = getKey(device, scanCode, usageCode);
617 if (key) {
618 if (outKeycode) {
619 *outKeycode = key->keyCode;
620 }
621 if (outFlags) {
622 *outFlags = key->flags;
623 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700624 if (outMetaState) {
625 *outMetaState = metaState;
626 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 return OK;
628 }
629 }
630 return NAME_NOT_FOUND;
631 }
632
633 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
634 if (usageCode) {
635 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
636 if (index >= 0) {
637 return &device->keysByUsageCode.valueAt(index);
638 }
639 }
640 if (scanCode) {
641 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
642 if (index >= 0) {
643 return &device->keysByScanCode.valueAt(index);
644 }
645 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700646 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647 }
648
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100649 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800650 return NAME_NOT_FOUND;
651 }
652
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100653 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654 mExcludedDevices = devices;
655 }
656
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100657 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700658 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659 if (mEvents.empty()) {
660 return 0;
661 }
662
663 *buffer = *mEvents.begin();
664 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700665 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 return 1;
667 }
668
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800669 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600670 auto it = mVideoFrames.find(deviceId);
671 if (it != mVideoFrames.end()) {
672 std::vector<TouchVideoFrame> frames = std::move(it->second);
673 mVideoFrames.erase(deviceId);
674 return frames;
675 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800676 return {};
677 }
678
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
680 Device* device = getDevice(deviceId);
681 if (device) {
682 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
683 if (index >= 0) {
684 return device->scanCodeStates.valueAt(index);
685 }
686 }
687 return AKEY_STATE_UNKNOWN;
688 }
689
690 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
691 Device* device = getDevice(deviceId);
692 if (device) {
693 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
694 if (index >= 0) {
695 return device->keyCodeStates.valueAt(index);
696 }
697 }
698 return AKEY_STATE_UNKNOWN;
699 }
700
701 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
702 Device* device = getDevice(deviceId);
703 if (device) {
704 ssize_t index = device->switchStates.indexOfKey(sw);
705 if (index >= 0) {
706 return device->switchStates.valueAt(index);
707 }
708 }
709 return AKEY_STATE_UNKNOWN;
710 }
711
712 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
713 int32_t* outValue) const {
714 Device* device = getDevice(deviceId);
715 if (device) {
716 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
717 if (index >= 0) {
718 *outValue = device->absoluteAxisValue.valueAt(index);
719 return OK;
720 }
721 }
722 *outValue = 0;
723 return -1;
724 }
725
726 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
727 uint8_t* outFlags) const {
728 bool result = false;
729 Device* device = getDevice(deviceId);
730 if (device) {
731 for (size_t i = 0; i < numCodes; i++) {
732 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
733 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
734 outFlags[i] = 1;
735 result = true;
736 }
737 }
738 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
739 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
740 outFlags[i] = 1;
741 result = true;
742 }
743 }
744 }
745 }
746 return result;
747 }
748
749 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
750 Device* device = getDevice(deviceId);
751 if (device) {
752 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
753 return index >= 0;
754 }
755 return false;
756 }
757
758 virtual bool hasLed(int32_t deviceId, int32_t led) const {
759 Device* device = getDevice(deviceId);
760 return device && device->leds.indexOfKey(led) >= 0;
761 }
762
763 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
764 Device* device = getDevice(deviceId);
765 if (device) {
766 ssize_t index = device->leds.indexOfKey(led);
767 if (index >= 0) {
768 device->leds.replaceValueAt(led, on);
769 } else {
770 ADD_FAILURE()
771 << "Attempted to set the state of an LED that the EventHub declared "
772 "was not present. led=" << led;
773 }
774 }
775 }
776
777 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800778 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779 outVirtualKeys.clear();
780
781 Device* device = getDevice(deviceId);
782 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800783 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784 }
785 }
786
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100787 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700788 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789 }
790
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100791 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792 return false;
793 }
794
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100795 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796 }
797
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100798 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800799 }
800
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100801 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802 return false;
803 }
804
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800805 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806 }
807
808 virtual void monitor() {
809 }
810
811 virtual void requestReopenDevices() {
812 }
813
814 virtual void wake() {
815 }
816};
817
818
819// --- FakeInputReaderContext ---
820
821class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700822 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 sp<InputReaderPolicyInterface> mPolicy;
824 sp<InputListenerInterface> mListener;
825 int32_t mGlobalMetaState;
826 bool mUpdateGlobalMetaStateWasCalled;
827 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800828 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829
830public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700831 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
832 const sp<InputReaderPolicyInterface>& policy,
833 const sp<InputListenerInterface>& listener)
834 : mEventHub(eventHub),
835 mPolicy(policy),
836 mListener(listener),
837 mGlobalMetaState(0),
838 mNextSequenceNum(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839
840 virtual ~FakeInputReaderContext() { }
841
842 void assertUpdateGlobalMetaStateWasCalled() {
843 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
844 << "Expected updateGlobalMetaState() to have been called.";
845 mUpdateGlobalMetaStateWasCalled = false;
846 }
847
848 void setGlobalMetaState(int32_t state) {
849 mGlobalMetaState = state;
850 }
851
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800852 uint32_t getGeneration() {
853 return mGeneration;
854 }
855
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856private:
857 virtual void updateGlobalMetaState() {
858 mUpdateGlobalMetaStateWasCalled = true;
859 }
860
861 virtual int32_t getGlobalMetaState() {
862 return mGlobalMetaState;
863 }
864
865 virtual EventHubInterface* getEventHub() {
866 return mEventHub.get();
867 }
868
869 virtual InputReaderPolicyInterface* getPolicy() {
870 return mPolicy.get();
871 }
872
873 virtual InputListenerInterface* getListener() {
874 return mListener.get();
875 }
876
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100877 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878 }
879
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100880 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881 return false;
882 }
883
884 virtual void fadePointer() {
885 }
886
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100887 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800888 }
889
890 virtual int32_t bumpGeneration() {
891 return ++mGeneration;
892 }
Michael Wright842500e2015-03-13 17:32:02 -0700893
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800894 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700895
896 }
897
898 virtual void dispatchExternalStylusState(const StylusState&) {
899
900 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800901
902 virtual uint32_t getNextSequenceNum() {
903 return mNextSequenceNum++;
904 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905};
906
907
908// --- FakeInputMapper ---
909
910class FakeInputMapper : public InputMapper {
911 uint32_t mSources;
912 int32_t mKeyboardType;
913 int32_t mMetaState;
914 KeyedVector<int32_t, int32_t> mKeyCodeStates;
915 KeyedVector<int32_t, int32_t> mScanCodeStates;
916 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800917 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700919 std::mutex mLock;
920 std::condition_variable mStateChangedCondition;
921 bool mConfigureWasCalled GUARDED_BY(mLock);
922 bool mResetWasCalled GUARDED_BY(mLock);
923 bool mProcessWasCalled GUARDED_BY(mLock);
924 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925
Arthur Hungc23540e2018-11-29 20:42:11 +0800926 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927public:
928 FakeInputMapper(InputDevice* device, uint32_t sources) :
929 InputMapper(device),
930 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
931 mMetaState(0),
932 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
933 }
934
935 virtual ~FakeInputMapper() { }
936
937 void setKeyboardType(int32_t keyboardType) {
938 mKeyboardType = keyboardType;
939 }
940
941 void setMetaState(int32_t metaState) {
942 mMetaState = metaState;
943 }
944
945 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700946 std::unique_lock<std::mutex> lock(mLock);
947 base::ScopedLockAssertion assumeLocked(mLock);
948 const bool configureCalled =
949 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
950 return mConfigureWasCalled;
951 });
952 if (!configureCalled) {
953 FAIL() << "Expected configure() to have been called.";
954 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955 mConfigureWasCalled = false;
956 }
957
958 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700959 std::unique_lock<std::mutex> lock(mLock);
960 base::ScopedLockAssertion assumeLocked(mLock);
961 const bool resetCalled =
962 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
963 return mResetWasCalled;
964 });
965 if (!resetCalled) {
966 FAIL() << "Expected reset() to have been called.";
967 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 mResetWasCalled = false;
969 }
970
Yi Kong9b14ac62018-07-17 13:48:38 -0700971 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700972 std::unique_lock<std::mutex> lock(mLock);
973 base::ScopedLockAssertion assumeLocked(mLock);
974 const bool processCalled =
975 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
976 return mProcessWasCalled;
977 });
978 if (!processCalled) {
979 FAIL() << "Expected process() to have been called.";
980 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981 if (outLastEvent) {
982 *outLastEvent = mLastEvent;
983 }
984 mProcessWasCalled = false;
985 }
986
987 void setKeyCodeState(int32_t keyCode, int32_t state) {
988 mKeyCodeStates.replaceValueFor(keyCode, state);
989 }
990
991 void setScanCodeState(int32_t scanCode, int32_t state) {
992 mScanCodeStates.replaceValueFor(scanCode, state);
993 }
994
995 void setSwitchState(int32_t switchCode, int32_t state) {
996 mSwitchStates.replaceValueFor(switchCode, state);
997 }
998
999 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001000 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001 }
1002
1003private:
1004 virtual uint32_t getSources() {
1005 return mSources;
1006 }
1007
1008 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1009 InputMapper::populateDeviceInfo(deviceInfo);
1010
1011 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1012 deviceInfo->setKeyboardType(mKeyboardType);
1013 }
1014 }
1015
Arthur Hungc23540e2018-11-29 20:42:11 +08001016 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001017 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001019
1020 // Find the associated viewport if exist.
1021 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
1022 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1023 mViewport = config->getDisplayViewportByPort(*displayPort);
1024 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001025
1026 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001027 }
1028
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001029 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001030 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001032 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001033 }
1034
1035 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001036 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001037 mLastEvent = *rawEvent;
1038 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001039 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001040 }
1041
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001042 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1044 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1045 }
1046
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001047 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1049 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1050 }
1051
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001052 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1054 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1055 }
1056
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001057 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058 const int32_t* keyCodes, uint8_t* outFlags) {
1059 bool result = false;
1060 for (size_t i = 0; i < numCodes; i++) {
1061 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1062 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1063 outFlags[i] = 1;
1064 result = true;
1065 }
1066 }
1067 }
1068 return result;
1069 }
1070
1071 virtual int32_t getMetaState() {
1072 return mMetaState;
1073 }
1074
1075 virtual void fadePointer() {
1076 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001077
1078 virtual std::optional<int32_t> getAssociatedDisplay() {
1079 if (mViewport) {
1080 return std::make_optional(mViewport->displayId);
1081 }
1082 return std::nullopt;
1083 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084};
1085
1086
1087// --- InstrumentedInputReader ---
1088
1089class InstrumentedInputReader : public InputReader {
1090 InputDevice* mNextDevice;
1091
1092public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001093 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1094 const sp<InputReaderPolicyInterface>& policy,
1095 const sp<InputListenerInterface>& listener)
1096 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097
1098 virtual ~InstrumentedInputReader() {
1099 if (mNextDevice) {
1100 delete mNextDevice;
1101 }
1102 }
1103
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001104 void setNextDevice(InputDevice* device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001106 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001107 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108 InputDeviceIdentifier identifier;
1109 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001110 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111 int32_t generation = deviceId + 1;
1112 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001113 classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001114 }
1115
1116protected:
1117 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001118 const InputDeviceIdentifier& identifier,
1119 uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120 if (mNextDevice) {
1121 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001122 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 return device;
1124 }
1125 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1126 }
1127
1128 friend class InputReaderTest;
1129};
1130
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001131// --- InputReaderPolicyTest ---
1132class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001133protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001134 sp<FakeInputReaderPolicy> mFakePolicy;
1135
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001136 virtual void SetUp() {
1137 mFakePolicy = new FakeInputReaderPolicy();
1138 }
1139 virtual void TearDown() {
1140 mFakePolicy.clear();
1141 }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001142};
1143
1144/**
1145 * Check that empty set of viewports is an acceptable configuration.
1146 * Also try to get internal viewport two different ways - by type and by uniqueId.
1147 *
1148 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1149 * Such configuration is not currently allowed.
1150 */
1151TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001152 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001153
1154 // We didn't add any viewports yet, so there shouldn't be any.
1155 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001156 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001157 ASSERT_FALSE(internalViewport);
1158
1159 // Add an internal viewport, then clear it
1160 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001161 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001162
1163 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001164 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001165 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001166 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001167
1168 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001169 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001170 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001171 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001172
1173 mFakePolicy->clearViewports();
1174 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001175 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001176 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001177 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001178 ASSERT_FALSE(internalViewport);
1179}
1180
1181TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1182 const std::string internalUniqueId = "local:0";
1183 const std::string externalUniqueId = "local:1";
1184 const std::string virtualUniqueId1 = "virtual:2";
1185 const std::string virtualUniqueId2 = "virtual:3";
1186 constexpr int32_t virtualDisplayId1 = 2;
1187 constexpr int32_t virtualDisplayId2 = 3;
1188
1189 // Add an internal viewport
1190 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001191 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001192 // Add an external viewport
1193 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001194 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001195 // Add an virtual viewport
1196 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001197 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001198 // Add another virtual viewport
1199 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001200 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001201
1202 // Check matching by type for internal
1203 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001204 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001205 ASSERT_TRUE(internalViewport);
1206 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1207
1208 // Check matching by type for external
1209 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001210 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001211 ASSERT_TRUE(externalViewport);
1212 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1213
1214 // Check matching by uniqueId for virtual viewport #1
1215 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001216 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001217 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001218 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001219 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1220 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1221
1222 // Check matching by uniqueId for virtual viewport #2
1223 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001224 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001225 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001226 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001227 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1228 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1229}
1230
1231
1232/**
1233 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1234 * that lookup works by checking display id.
1235 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1236 */
1237TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1238 const std::string uniqueId1 = "uniqueId1";
1239 const std::string uniqueId2 = "uniqueId2";
1240 constexpr int32_t displayId1 = 2;
1241 constexpr int32_t displayId2 = 3;
1242
1243 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1244 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1245 for (const ViewportType& type : types) {
1246 mFakePolicy->clearViewports();
1247 // Add a viewport
1248 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001249 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001250 // Add another viewport
1251 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001252 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001253
1254 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001255 std::optional<DisplayViewport> viewport1 =
1256 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001257 ASSERT_TRUE(viewport1);
1258 ASSERT_EQ(displayId1, viewport1->displayId);
1259 ASSERT_EQ(type, viewport1->type);
1260
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001261 std::optional<DisplayViewport> viewport2 =
1262 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001263 ASSERT_TRUE(viewport2);
1264 ASSERT_EQ(displayId2, viewport2->displayId);
1265 ASSERT_EQ(type, viewport2->type);
1266
1267 // When there are multiple viewports of the same kind, and uniqueId is not specified
1268 // in the call to getDisplayViewport, then that situation is not supported.
1269 // The viewports can be stored in any order, so we cannot rely on the order, since that
1270 // is just implementation detail.
1271 // However, we can check that it still returns *a* viewport, we just cannot assert
1272 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001273 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001274 ASSERT_TRUE(someViewport);
1275 }
1276}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001278/**
1279 * Check getDisplayViewportByPort
1280 */
1281TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1282 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1283 const std::string uniqueId1 = "uniqueId1";
1284 const std::string uniqueId2 = "uniqueId2";
1285 constexpr int32_t displayId1 = 1;
1286 constexpr int32_t displayId2 = 2;
1287 const uint8_t hdmi1 = 0;
1288 const uint8_t hdmi2 = 1;
1289 const uint8_t hdmi3 = 2;
1290
1291 mFakePolicy->clearViewports();
1292 // Add a viewport that's associated with some display port that's not of interest.
1293 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1294 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1295 // Add another viewport, connected to HDMI1 port
1296 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1297 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1298
1299 // Check that correct display viewport was returned by comparing the display ports.
1300 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1301 ASSERT_TRUE(hdmi1Viewport);
1302 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1303 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1304
1305 // Check that we can still get the same viewport using the uniqueId
1306 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1307 ASSERT_TRUE(hdmi1Viewport);
1308 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1309 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1310 ASSERT_EQ(type, hdmi1Viewport->type);
1311
1312 // Check that we cannot find a port with "HDMI2", because we never added one
1313 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1314 ASSERT_FALSE(hdmi2Viewport);
1315}
1316
Michael Wrightd02c5b62014-02-10 15:10:22 -08001317// --- InputReaderTest ---
1318
1319class InputReaderTest : public testing::Test {
1320protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001321 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001323 std::shared_ptr<FakeEventHub> mFakeEventHub;
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001324 sp<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001326 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001327 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001329 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001330
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001331 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001332 }
1333
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001334 virtual void TearDown() {
1335 mReader.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001336
1337 mFakeListener.clear();
1338 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001339 }
1340
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001341 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001342 const PropertyMap* configuration) {
1343 mFakeEventHub->addDevice(deviceId, name, classes);
1344
1345 if (configuration) {
1346 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1347 }
1348 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001349 mReader->loopOnce();
1350 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001351 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1352 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001353 }
1354
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001355 void disableDevice(int32_t deviceId, InputDevice* device) {
1356 mFakePolicy->addDisabledDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001357 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001358 }
1359
1360 void enableDevice(int32_t deviceId, InputDevice* device) {
1361 mFakePolicy->removeDisabledDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001362 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1363 }
1364
1365 void configureDevice(uint32_t changes, InputDevice* device) {
1366 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001367 }
1368
Michael Wrightd02c5b62014-02-10 15:10:22 -08001369 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001370 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001371 const PropertyMap* configuration) {
1372 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1373 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1374 device->addMapper(mapper);
1375 mReader->setNextDevice(device);
1376 addDevice(deviceId, name, classes, configuration);
1377 return mapper;
1378 }
1379};
1380
1381TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001382 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001383 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001384 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001385 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001387 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001388 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001389 ASSERT_EQ(1U, inputDevices.size());
1390 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001391 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001392 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1393 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1394 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1395
1396 // Should also have received a notification describing the new input devices.
1397 inputDevices = mFakePolicy->getInputDevices();
1398 ASSERT_EQ(1U, inputDevices.size());
1399 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001400 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1402 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1403 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1404}
1405
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001406TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1407 constexpr int32_t deviceId = 1;
1408 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001409 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001410 // Must add at least one mapper or the device will be ignored!
1411 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1412 device->addMapper(mapper);
1413 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001414 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001415
Yi Kong9b14ac62018-07-17 13:48:38 -07001416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001417
1418 NotifyDeviceResetArgs resetArgs;
1419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001420 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001421 ASSERT_EQ(deviceId, resetArgs.deviceId);
1422
1423 ASSERT_EQ(device->isEnabled(), true);
1424 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001425 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001426
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001428 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001429 ASSERT_EQ(deviceId, resetArgs.deviceId);
1430 ASSERT_EQ(device->isEnabled(), false);
1431
1432 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001433 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001436 ASSERT_EQ(device->isEnabled(), false);
1437
1438 enableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001439 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001441 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001442 ASSERT_EQ(deviceId, resetArgs.deviceId);
1443 ASSERT_EQ(device->isEnabled(), true);
1444}
1445
Michael Wrightd02c5b62014-02-10 15:10:22 -08001446TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001447 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001448 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001449 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001450 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1451
1452 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1453 AINPUT_SOURCE_ANY, AKEYCODE_A))
1454 << "Should return unknown when the device id is >= 0 but unknown.";
1455
1456 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1457 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1458 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1459
1460 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1461 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1462 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1463
1464 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1465 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1466 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1467
1468 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1469 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1470 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1471}
1472
1473TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001474 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001475 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001476 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001477 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1478
1479 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1480 AINPUT_SOURCE_ANY, KEY_A))
1481 << "Should return unknown when the device id is >= 0 but unknown.";
1482
1483 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1484 AINPUT_SOURCE_TRACKBALL, KEY_A))
1485 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1486
1487 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1488 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1489 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1490
1491 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1492 AINPUT_SOURCE_TRACKBALL, KEY_A))
1493 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1494
1495 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1496 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1497 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1498}
1499
1500TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001501 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001502 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001503 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001504 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1505
1506 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1507 AINPUT_SOURCE_ANY, SW_LID))
1508 << "Should return unknown when the device id is >= 0 but unknown.";
1509
1510 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1511 AINPUT_SOURCE_TRACKBALL, SW_LID))
1512 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1513
1514 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1515 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1516 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1517
1518 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1519 AINPUT_SOURCE_TRACKBALL, SW_LID))
1520 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1521
1522 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1523 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1524 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1525}
1526
1527TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001528 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001529 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001530 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001531
Michael Wrightd02c5b62014-02-10 15:10:22 -08001532 mapper->addSupportedKeyCode(AKEYCODE_A);
1533 mapper->addSupportedKeyCode(AKEYCODE_B);
1534
1535 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1536 uint8_t flags[4] = { 0, 0, 0, 1 };
1537
1538 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1539 << "Should return false when device id is >= 0 but unknown.";
1540 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1541
1542 flags[3] = 1;
1543 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1544 << "Should return false when device id is valid but the sources are not supported by the device.";
1545 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1546
1547 flags[3] = 1;
1548 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1549 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1550 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1551
1552 flags[3] = 1;
1553 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1554 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1555 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1556
1557 flags[3] = 1;
1558 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1559 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1560 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1561}
1562
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001563TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001564 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001565
1566 NotifyConfigurationChangedArgs args;
1567
1568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1569 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1570}
1571
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001572TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001573 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001574 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001575 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001576
1577 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001578 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1580
1581 RawEvent event;
1582 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1583 ASSERT_EQ(0, event.when);
1584 ASSERT_EQ(1, event.deviceId);
1585 ASSERT_EQ(EV_KEY, event.type);
1586 ASSERT_EQ(KEY_A, event.code);
1587 ASSERT_EQ(1, event.value);
1588}
1589
Prabir Pradhan42611e02018-11-27 14:04:02 -08001590TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1591 constexpr int32_t deviceId = 1;
1592 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001593 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001594 // Must add at least one mapper or the device will be ignored!
1595 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1596 device->addMapper(mapper);
1597 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001598 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001599
1600 NotifyDeviceResetArgs resetArgs;
1601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1602 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1603
1604 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001605 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001607 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1608 prevSequenceNum = resetArgs.sequenceNum;
1609
1610 enableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001611 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001613 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1614 prevSequenceNum = resetArgs.sequenceNum;
1615
1616 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001617 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001619 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1620 prevSequenceNum = resetArgs.sequenceNum;
1621}
1622
Arthur Hungc23540e2018-11-29 20:42:11 +08001623TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1624 constexpr int32_t deviceId = 1;
1625 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1626 const char* DEVICE_LOCATION = "USB1";
1627 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1628 DEVICE_LOCATION);
1629 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1630 device->addMapper(mapper);
1631 mReader->setNextDevice(device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001632 addDevice(deviceId, "fake", deviceClass, nullptr);
Arthur Hungc23540e2018-11-29 20:42:11 +08001633
1634 const uint8_t hdmi1 = 1;
1635
1636 // Associated touch screen with second display.
1637 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1638
1639 // Add default and second display.
1640 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1641 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1642 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1643 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1644 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001645 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001647
Arthur Hung2c9a3342019-07-23 14:18:59 +08001648 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001649 ASSERT_EQ(deviceId, device->getId());
1650 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1651 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001652
1653 // Can't dispatch event from a disabled device.
1654 disableDevice(deviceId, device);
1655 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001656}
1657
Michael Wrightd02c5b62014-02-10 15:10:22 -08001658
1659// --- InputDeviceTest ---
1660
1661class InputDeviceTest : public testing::Test {
1662protected:
1663 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001664 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665 static const int32_t DEVICE_ID;
1666 static const int32_t DEVICE_GENERATION;
1667 static const int32_t DEVICE_CONTROLLER_NUMBER;
1668 static const uint32_t DEVICE_CLASSES;
1669
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001670 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001671 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001672 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001673 FakeInputReaderContext* mFakeContext;
1674
1675 InputDevice* mDevice;
1676
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001677 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001678 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001680 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1682
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001683 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001684 InputDeviceIdentifier identifier;
1685 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001686 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001687 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1688 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1689 }
1690
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001691 virtual void TearDown() {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001692 delete mDevice;
1693
1694 delete mFakeContext;
1695 mFakeListener.clear();
1696 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001697 }
1698};
1699
1700const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001701const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001702const int32_t InputDeviceTest::DEVICE_ID = 1;
1703const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1704const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1705const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1706 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1707
1708TEST_F(InputDeviceTest, ImmutableProperties) {
1709 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001710 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001711 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1712}
1713
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001714TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1715 ASSERT_EQ(mDevice->isEnabled(), true);
1716}
1717
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1719 // Configuration.
1720 InputReaderConfiguration config;
1721 mDevice->configure(ARBITRARY_TIME, &config, 0);
1722
1723 // Reset.
1724 mDevice->reset(ARBITRARY_TIME);
1725
1726 NotifyDeviceResetArgs resetArgs;
1727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1728 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1729 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1730
1731 // Metadata.
1732 ASSERT_TRUE(mDevice->isIgnored());
1733 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1734
1735 InputDeviceInfo info;
1736 mDevice->getDeviceInfo(&info);
1737 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001738 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001739 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1740 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1741
1742 // State queries.
1743 ASSERT_EQ(0, mDevice->getMetaState());
1744
1745 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1746 << "Ignored device should return unknown key code state.";
1747 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1748 << "Ignored device should return unknown scan code state.";
1749 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1750 << "Ignored device should return unknown switch state.";
1751
1752 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1753 uint8_t flags[2] = { 0, 1 };
1754 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1755 << "Ignored device should never mark any key codes.";
1756 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1757 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1758}
1759
1760TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1761 // Configuration.
1762 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1763
1764 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1765 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1766 mapper1->setMetaState(AMETA_ALT_ON);
1767 mapper1->addSupportedKeyCode(AKEYCODE_A);
1768 mapper1->addSupportedKeyCode(AKEYCODE_B);
1769 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1770 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1771 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1772 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1773 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1774 mDevice->addMapper(mapper1);
1775
1776 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1777 mapper2->setMetaState(AMETA_SHIFT_ON);
1778 mDevice->addMapper(mapper2);
1779
1780 InputReaderConfiguration config;
1781 mDevice->configure(ARBITRARY_TIME, &config, 0);
1782
1783 String8 propertyValue;
1784 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1785 << "Device should have read configuration during configuration phase.";
1786 ASSERT_STREQ("value", propertyValue.string());
1787
1788 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1789 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1790
1791 // Reset
1792 mDevice->reset(ARBITRARY_TIME);
1793 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1794 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1795
1796 NotifyDeviceResetArgs resetArgs;
1797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1798 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1799 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1800
1801 // Metadata.
1802 ASSERT_FALSE(mDevice->isIgnored());
1803 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1804
1805 InputDeviceInfo info;
1806 mDevice->getDeviceInfo(&info);
1807 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001808 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1810 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1811
1812 // State queries.
1813 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1814 << "Should query mappers and combine meta states.";
1815
1816 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1817 << "Should return unknown key code state when source not supported.";
1818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1819 << "Should return unknown scan code state when source not supported.";
1820 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1821 << "Should return unknown switch state when source not supported.";
1822
1823 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1824 << "Should query mapper when source is supported.";
1825 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1826 << "Should query mapper when source is supported.";
1827 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1828 << "Should query mapper when source is supported.";
1829
1830 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1831 uint8_t flags[4] = { 0, 0, 0, 1 };
1832 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1833 << "Should do nothing when source is unsupported.";
1834 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1835 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1836 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1837 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1838
1839 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1840 << "Should query mapper when source is supported.";
1841 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1842 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1843 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1844 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1845
1846 // Event handling.
1847 RawEvent event;
1848 mDevice->process(&event, 1);
1849
1850 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1851 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1852}
1853
Arthur Hung2c9a3342019-07-23 14:18:59 +08001854// A single input device is associated with a specific display. Check that:
1855// 1. Device is disabled if the viewport corresponding to the associated display is not found
1856// 2. Device is disabled when setEnabled API is called
1857TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
1858 FakeInputMapper* mapper = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1859 mDevice->addMapper(mapper);
1860
1861 // First Configuration.
1862 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1863
1864 // Device should be enabled by default.
1865 ASSERT_TRUE(mDevice->isEnabled());
1866
1867 // Prepare associated info.
1868 constexpr uint8_t hdmi = 1;
1869 const std::string UNIQUE_ID = "local:1";
1870
1871 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1872 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1873 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1874 // Device should be disabled because it is associated with a specific display via
1875 // input port <-> display port association, but the corresponding display is not found
1876 ASSERT_FALSE(mDevice->isEnabled());
1877
1878 // Prepare displays.
1879 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1880 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1881 ViewportType::VIEWPORT_INTERNAL);
1882 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1883 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1884 ASSERT_TRUE(mDevice->isEnabled());
1885
1886 // Device should be disabled after set disable.
1887 mFakePolicy->addDisabledDevice(mDevice->getId());
1888 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1889 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1890 ASSERT_FALSE(mDevice->isEnabled());
1891
1892 // Device should still be disabled even found the associated display.
1893 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1894 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1895 ASSERT_FALSE(mDevice->isEnabled());
1896}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001897
1898// --- InputMapperTest ---
1899
1900class InputMapperTest : public testing::Test {
1901protected:
1902 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001903 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001904 static const int32_t DEVICE_ID;
1905 static const int32_t DEVICE_GENERATION;
1906 static const int32_t DEVICE_CONTROLLER_NUMBER;
1907 static const uint32_t DEVICE_CLASSES;
1908
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001909 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001910 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001911 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001912 FakeInputReaderContext* mFakeContext;
1913 InputDevice* mDevice;
1914
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001915 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001916 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001917 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001918 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001919 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1920 InputDeviceIdentifier identifier;
1921 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001922 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1924 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1925
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001926 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001927 }
1928
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001929 virtual void TearDown() {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930 delete mDevice;
1931 delete mFakeContext;
1932 mFakeListener.clear();
1933 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001934 }
1935
1936 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001937 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001938 }
1939
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001940 void configureDevice(uint32_t changes) {
1941 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1942 }
1943
Michael Wrightd02c5b62014-02-10 15:10:22 -08001944 void addMapperAndConfigure(InputMapper* mapper) {
1945 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001946 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001947 mDevice->reset(ARBITRARY_TIME);
1948 }
1949
1950 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001951 int32_t orientation, const std::string& uniqueId,
1952 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001953 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001954 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001955 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1956 }
1957
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001958 void clearViewports() {
1959 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001960 }
1961
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001962 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001963 int32_t code, int32_t value) {
1964 RawEvent event;
1965 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001966 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001967 event.type = type;
1968 event.code = code;
1969 event.value = value;
1970 mapper->process(&event);
1971 }
1972
1973 static void assertMotionRange(const InputDeviceInfo& info,
1974 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1975 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001976 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001977 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1978 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1979 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1980 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1981 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1982 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1983 }
1984
1985 static void assertPointerCoords(const PointerCoords& coords,
1986 float x, float y, float pressure, float size,
1987 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1988 float orientation, float distance) {
1989 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1990 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1991 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1992 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1993 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1994 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1995 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1996 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1997 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1998 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1999 }
2000
2001 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2002 float actualX, actualY;
2003 controller->getPosition(&actualX, &actualY);
2004 ASSERT_NEAR(x, actualX, 1);
2005 ASSERT_NEAR(y, actualY, 1);
2006 }
2007};
2008
2009const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002010const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002011const int32_t InputMapperTest::DEVICE_ID = 1;
2012const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2013const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2014const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
2015
2016
2017// --- SwitchInputMapperTest ---
2018
2019class SwitchInputMapperTest : public InputMapperTest {
2020protected:
2021};
2022
2023TEST_F(SwitchInputMapperTest, GetSources) {
2024 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2025 addMapperAndConfigure(mapper);
2026
2027 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
2028}
2029
2030TEST_F(SwitchInputMapperTest, GetSwitchState) {
2031 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2032 addMapperAndConfigure(mapper);
2033
2034 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
2035 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2036
2037 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
2038 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2039}
2040
2041TEST_F(SwitchInputMapperTest, Process) {
2042 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2043 addMapperAndConfigure(mapper);
2044
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002045 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2046 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2047 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2048 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002049
2050 NotifySwitchArgs args;
2051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2052 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002053 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2054 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002055 args.switchMask);
2056 ASSERT_EQ(uint32_t(0), args.policyFlags);
2057}
2058
2059
2060// --- KeyboardInputMapperTest ---
2061
2062class KeyboardInputMapperTest : public InputMapperTest {
2063protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002064 const std::string UNIQUE_ID = "local:0";
2065
2066 void prepareDisplay(int32_t orientation);
2067
Arthur Hung2c9a3342019-07-23 14:18:59 +08002068 void testDPadKeyRotation(KeyboardInputMapper* mapper, int32_t originalScanCode,
2069 int32_t originalKeyCode, int32_t rotatedKeyCode,
2070 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002071};
2072
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002073/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2074 * orientation.
2075 */
2076void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2077 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002078 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002079}
2080
Michael Wrightd02c5b62014-02-10 15:10:22 -08002081void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002082 int32_t originalScanCode, int32_t originalKeyCode,
2083 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002084 NotifyKeyArgs args;
2085
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002086 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2088 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2089 ASSERT_EQ(originalScanCode, args.scanCode);
2090 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002091 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002092
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002093 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2095 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2096 ASSERT_EQ(originalScanCode, args.scanCode);
2097 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002098 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002099}
2100
Michael Wrightd02c5b62014-02-10 15:10:22 -08002101TEST_F(KeyboardInputMapperTest, GetSources) {
2102 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2103 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2104 addMapperAndConfigure(mapper);
2105
2106 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
2107}
2108
2109TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2110 const int32_t USAGE_A = 0x070004;
2111 const int32_t USAGE_UNKNOWN = 0x07ffff;
2112 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2113 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2114
2115 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2116 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2117 addMapperAndConfigure(mapper);
2118
2119 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002120 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002121 NotifyKeyArgs args;
2122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2123 ASSERT_EQ(DEVICE_ID, args.deviceId);
2124 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2125 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2126 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2127 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2128 ASSERT_EQ(KEY_HOME, args.scanCode);
2129 ASSERT_EQ(AMETA_NONE, args.metaState);
2130 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2131 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2132 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2133
2134 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002135 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2137 ASSERT_EQ(DEVICE_ID, args.deviceId);
2138 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2139 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2140 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2141 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2142 ASSERT_EQ(KEY_HOME, args.scanCode);
2143 ASSERT_EQ(AMETA_NONE, args.metaState);
2144 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2145 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2146 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2147
2148 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002149 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2150 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2152 ASSERT_EQ(DEVICE_ID, args.deviceId);
2153 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2154 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2155 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2156 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2157 ASSERT_EQ(0, args.scanCode);
2158 ASSERT_EQ(AMETA_NONE, args.metaState);
2159 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2160 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2161 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2162
2163 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002164 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2165 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2167 ASSERT_EQ(DEVICE_ID, args.deviceId);
2168 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2169 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2170 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2171 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2172 ASSERT_EQ(0, args.scanCode);
2173 ASSERT_EQ(AMETA_NONE, args.metaState);
2174 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2175 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2176 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2177
2178 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002179 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2180 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2182 ASSERT_EQ(DEVICE_ID, args.deviceId);
2183 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2184 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2185 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2186 ASSERT_EQ(0, args.keyCode);
2187 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2188 ASSERT_EQ(AMETA_NONE, args.metaState);
2189 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2190 ASSERT_EQ(0U, args.policyFlags);
2191 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2192
2193 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002194 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2195 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2197 ASSERT_EQ(DEVICE_ID, args.deviceId);
2198 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2199 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2200 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2201 ASSERT_EQ(0, args.keyCode);
2202 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2203 ASSERT_EQ(AMETA_NONE, args.metaState);
2204 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2205 ASSERT_EQ(0U, args.policyFlags);
2206 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2207}
2208
2209TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2210 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2211 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2212
2213 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2214 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2215 addMapperAndConfigure(mapper);
2216
2217 // Initial metastate.
2218 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2219
2220 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002221 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002222 NotifyKeyArgs args;
2223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2224 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2225 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2226 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2227
2228 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002229 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2231 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2232 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2233
2234 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002235 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2237 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2238 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2239
2240 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002241 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2243 ASSERT_EQ(AMETA_NONE, args.metaState);
2244 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2245 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2246}
2247
2248TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2249 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2250 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2251 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2252 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2253
2254 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2255 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2256 addMapperAndConfigure(mapper);
2257
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002258 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002259 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2260 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2261 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2262 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2263 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2264 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2265 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2266 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2267}
2268
2269TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2270 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2271 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2272 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2273 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2274
2275 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2276 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2277 addConfigurationProperty("keyboard.orientationAware", "1");
2278 addMapperAndConfigure(mapper);
2279
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002280 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002281 ASSERT_NO_FATAL_FAILURE(
2282 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2283 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2284 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2285 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2286 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2287 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2288 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002290 clearViewports();
2291 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002292 ASSERT_NO_FATAL_FAILURE(
2293 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2294 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2295 AKEYCODE_DPAD_UP, DISPLAY_ID));
2296 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2297 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2298 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2299 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002300
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002301 clearViewports();
2302 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002303 ASSERT_NO_FATAL_FAILURE(
2304 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2305 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2306 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2307 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2308 AKEYCODE_DPAD_UP, DISPLAY_ID));
2309 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2310 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002311
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002312 clearViewports();
2313 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002314 ASSERT_NO_FATAL_FAILURE(
2315 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2316 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2317 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2318 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2319 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2320 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2321 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002322
2323 // Special case: if orientation changes while key is down, we still emit the same keycode
2324 // in the key up as we did in the key down.
2325 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002326 clearViewports();
2327 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002328 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2330 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2331 ASSERT_EQ(KEY_UP, args.scanCode);
2332 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2333
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002334 clearViewports();
2335 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002336 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2338 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2339 ASSERT_EQ(KEY_UP, args.scanCode);
2340 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2341}
2342
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002343TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2344 // If the keyboard is not orientation aware,
2345 // key events should not be associated with a specific display id
2346 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2347
2348 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2349 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2350 addMapperAndConfigure(mapper);
2351 NotifyKeyArgs args;
2352
2353 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002354 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002356 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2358 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2359
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002360 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002361 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002363 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2365 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2366}
2367
2368TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2369 // If the keyboard is orientation aware,
2370 // key events should be associated with the internal viewport
2371 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2372
2373 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2374 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2375 addConfigurationProperty("keyboard.orientationAware", "1");
2376 addMapperAndConfigure(mapper);
2377 NotifyKeyArgs args;
2378
2379 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2380 // ^--- already checked by the previous test
2381
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002382 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002383 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002384 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002386 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2388 ASSERT_EQ(DISPLAY_ID, args.displayId);
2389
2390 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002391 clearViewports();
2392 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002393 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002394 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002396 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2398 ASSERT_EQ(newDisplayId, args.displayId);
2399}
2400
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2402 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2403 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2404 addMapperAndConfigure(mapper);
2405
2406 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2407 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2408
2409 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2410 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2411}
2412
2413TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2414 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2415 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2416 addMapperAndConfigure(mapper);
2417
2418 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2419 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2420
2421 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2422 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2423}
2424
2425TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2426 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2427 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2428 addMapperAndConfigure(mapper);
2429
2430 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2431
2432 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2433 uint8_t flags[2] = { 0, 0 };
2434 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2435 ASSERT_TRUE(flags[0]);
2436 ASSERT_FALSE(flags[1]);
2437}
2438
2439TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2440 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2441 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2442 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2443 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2444 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2445 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2446
2447 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2448 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2449 addMapperAndConfigure(mapper);
2450
2451 // Initialization should have turned all of the lights off.
2452 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2453 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2454 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2455
2456 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002457 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2458 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2460 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2461 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2462 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2463
2464 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002465 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2466 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2468 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2469 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2470 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2471
2472 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002473 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2474 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002475 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2476 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2477 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2478 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2479
2480 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002481 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2482 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2484 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2485 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2486 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2487
2488 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002489 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2490 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2492 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2493 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2494 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2495
2496 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002497 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2498 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002499 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2500 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2501 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2502 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2503}
2504
Arthur Hung2c9a3342019-07-23 14:18:59 +08002505TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2506 // keyboard 1.
2507 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2508 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2509 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2510 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2511
2512 // keyboard 2.
2513 const std::string USB2 = "USB2";
2514 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2515 InputDeviceIdentifier identifier;
2516 identifier.name = "KEYBOARD2";
2517 identifier.location = USB2;
2518 std::unique_ptr<InputDevice> device2 =
2519 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2520 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2521 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2522 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2523 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2524 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2525 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2526
2527 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2528 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2529 addMapperAndConfigure(mapper);
2530
2531 KeyboardInputMapper* mapper2 = new KeyboardInputMapper(device2.get(), AINPUT_SOURCE_KEYBOARD,
2532 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2533 device2->addMapper(mapper2);
2534 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2535 device2->reset(ARBITRARY_TIME);
2536
2537 // Prepared displays and associated info.
2538 constexpr uint8_t hdmi1 = 0;
2539 constexpr uint8_t hdmi2 = 1;
2540 const std::string SECONDARY_UNIQUE_ID = "local:1";
2541
2542 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2543 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2544
2545 // No associated display viewport found, should disable the device.
2546 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2547 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2548 ASSERT_FALSE(device2->isEnabled());
2549
2550 // Prepare second display.
2551 constexpr int32_t newDisplayId = 2;
2552 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2553 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2554 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2555 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2556 // Default device will reconfigure above, need additional reconfiguration for another device.
2557 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2558 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2559
2560 // Device should be enabled after the associated display is found.
2561 ASSERT_TRUE(mDevice->isEnabled());
2562 ASSERT_TRUE(device2->isEnabled());
2563
2564 // Test pad key events
2565 ASSERT_NO_FATAL_FAILURE(
2566 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2567 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2568 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2569 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2570 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2571 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2572 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2573
2574 ASSERT_NO_FATAL_FAILURE(
2575 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2576 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2577 AKEYCODE_DPAD_RIGHT, newDisplayId));
2578 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2579 AKEYCODE_DPAD_DOWN, newDisplayId));
2580 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2581 AKEYCODE_DPAD_LEFT, newDisplayId));
2582}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583
2584// --- CursorInputMapperTest ---
2585
2586class CursorInputMapperTest : public InputMapperTest {
2587protected:
2588 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2589
2590 sp<FakePointerController> mFakePointerController;
2591
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002592 virtual void SetUp() {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002593 InputMapperTest::SetUp();
2594
2595 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002596 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002597 }
2598
2599 void testMotionRotation(CursorInputMapper* mapper,
2600 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002601
2602 void prepareDisplay(int32_t orientation) {
2603 const std::string uniqueId = "local:0";
2604 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2605 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2606 orientation, uniqueId, NO_PORT, viewportType);
2607 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002608};
2609
2610const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2611
2612void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2613 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2614 NotifyMotionArgs args;
2615
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002616 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2617 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2618 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2622 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2623 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2624 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2625}
2626
2627TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2628 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2629 addConfigurationProperty("cursor.mode", "pointer");
2630 addMapperAndConfigure(mapper);
2631
2632 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2633}
2634
2635TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2636 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2637 addConfigurationProperty("cursor.mode", "navigation");
2638 addMapperAndConfigure(mapper);
2639
2640 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2641}
2642
2643TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2644 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2645 addConfigurationProperty("cursor.mode", "pointer");
2646 addMapperAndConfigure(mapper);
2647
2648 InputDeviceInfo info;
2649 mapper->populateDeviceInfo(&info);
2650
2651 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002652 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2653 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2655 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2656
2657 // When the bounds are set, then there should be a valid motion range.
2658 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2659
2660 InputDeviceInfo info2;
2661 mapper->populateDeviceInfo(&info2);
2662
2663 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2664 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2665 1, 800 - 1, 0.0f, 0.0f));
2666 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2667 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2668 2, 480 - 1, 0.0f, 0.0f));
2669 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2670 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2671 0.0f, 1.0f, 0.0f, 0.0f));
2672}
2673
2674TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2675 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2676 addConfigurationProperty("cursor.mode", "navigation");
2677 addMapperAndConfigure(mapper);
2678
2679 InputDeviceInfo info;
2680 mapper->populateDeviceInfo(&info);
2681
2682 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2683 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2684 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2685 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2686 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2687 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2688 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2689 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2690 0.0f, 1.0f, 0.0f, 0.0f));
2691}
2692
2693TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2694 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2695 addConfigurationProperty("cursor.mode", "navigation");
2696 addMapperAndConfigure(mapper);
2697
2698 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2699
2700 NotifyMotionArgs args;
2701
2702 // Button press.
2703 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002704 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2705 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2707 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2708 ASSERT_EQ(DEVICE_ID, args.deviceId);
2709 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2710 ASSERT_EQ(uint32_t(0), args.policyFlags);
2711 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2712 ASSERT_EQ(0, args.flags);
2713 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2714 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2715 ASSERT_EQ(0, args.edgeFlags);
2716 ASSERT_EQ(uint32_t(1), args.pointerCount);
2717 ASSERT_EQ(0, args.pointerProperties[0].id);
2718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2720 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2721 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2722 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2723 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2724
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2726 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2727 ASSERT_EQ(DEVICE_ID, args.deviceId);
2728 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2729 ASSERT_EQ(uint32_t(0), args.policyFlags);
2730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2731 ASSERT_EQ(0, args.flags);
2732 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2733 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2734 ASSERT_EQ(0, args.edgeFlags);
2735 ASSERT_EQ(uint32_t(1), args.pointerCount);
2736 ASSERT_EQ(0, args.pointerProperties[0].id);
2737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2739 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2740 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2741 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2742 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2743
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002745 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2746 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2748 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2749 ASSERT_EQ(DEVICE_ID, args.deviceId);
2750 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2751 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002752 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2753 ASSERT_EQ(0, args.flags);
2754 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2755 ASSERT_EQ(0, args.buttonState);
2756 ASSERT_EQ(0, args.edgeFlags);
2757 ASSERT_EQ(uint32_t(1), args.pointerCount);
2758 ASSERT_EQ(0, args.pointerProperties[0].id);
2759 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2761 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2762 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2763 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2764 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2765
2766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2767 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2768 ASSERT_EQ(DEVICE_ID, args.deviceId);
2769 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2770 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2772 ASSERT_EQ(0, args.flags);
2773 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2774 ASSERT_EQ(0, args.buttonState);
2775 ASSERT_EQ(0, args.edgeFlags);
2776 ASSERT_EQ(uint32_t(1), args.pointerCount);
2777 ASSERT_EQ(0, args.pointerProperties[0].id);
2778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2780 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2781 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2782 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2783 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2784}
2785
2786TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2787 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2788 addConfigurationProperty("cursor.mode", "navigation");
2789 addMapperAndConfigure(mapper);
2790
2791 NotifyMotionArgs args;
2792
2793 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002794 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2795 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2799 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2800
2801 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002802 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2803 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2807 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2808}
2809
2810TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2811 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2812 addConfigurationProperty("cursor.mode", "navigation");
2813 addMapperAndConfigure(mapper);
2814
2815 NotifyMotionArgs args;
2816
2817 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002818 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2819 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2821 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2823 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2824
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2826 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2828 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2829
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002831 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2832 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002834 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2836 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2837
2838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2841 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2842}
2843
2844TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2845 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2846 addConfigurationProperty("cursor.mode", "navigation");
2847 addMapperAndConfigure(mapper);
2848
2849 NotifyMotionArgs args;
2850
2851 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002852 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2853 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2854 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2855 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2857 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2859 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2860 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2861
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2863 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2865 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2866 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2867
Michael Wrightd02c5b62014-02-10 15:10:22 -08002868 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002869 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2870 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2871 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2875 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2876 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2877
2878 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002879 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2880 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002882 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2884 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2885
2886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2889 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2890}
2891
2892TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2893 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2894 addConfigurationProperty("cursor.mode", "navigation");
2895 addMapperAndConfigure(mapper);
2896
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002897 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002898 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2899 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2900 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2901 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2902 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2903 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2904 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2905 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2906}
2907
2908TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2909 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2910 addConfigurationProperty("cursor.mode", "navigation");
2911 addConfigurationProperty("cursor.orientationAware", "1");
2912 addMapperAndConfigure(mapper);
2913
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002914 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2916 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2917 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2918 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2919 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2920 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2921 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2922 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2923
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002924 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002925 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2926 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2927 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2928 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2929 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2930 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2931 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2932 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2933
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002934 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002935 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2936 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2937 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2938 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2939 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2940 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2941 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2942 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2943
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002944 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2946 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2947 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2948 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2949 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2950 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2951 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2952 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2953}
2954
2955TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2956 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2957 addConfigurationProperty("cursor.mode", "pointer");
2958 addMapperAndConfigure(mapper);
2959
2960 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2961 mFakePointerController->setPosition(100, 200);
2962 mFakePointerController->setButtonState(0);
2963
2964 NotifyMotionArgs motionArgs;
2965 NotifyKeyArgs keyArgs;
2966
2967 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002968 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2969 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2971 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2972 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2973 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2975 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2976
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2979 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2980 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2982 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2983
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002984 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2985 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 ASSERT_EQ(0, motionArgs.buttonState);
2989 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2991 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2992
2993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002994 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995 ASSERT_EQ(0, motionArgs.buttonState);
2996 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2998 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2999
3000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003002 ASSERT_EQ(0, motionArgs.buttonState);
3003 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3005 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3006
3007 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003008 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3009 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3010 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3012 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3013 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3014 motionArgs.buttonState);
3015 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3016 mFakePointerController->getButtonState());
3017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3018 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3019
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3022 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3023 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3024 mFakePointerController->getButtonState());
3025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3026 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3027
3028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3030 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3031 motionArgs.buttonState);
3032 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3033 mFakePointerController->getButtonState());
3034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3035 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3036
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003037 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3038 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3042 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3044 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3045
3046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003048 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3049 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3051 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3052
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003053 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3054 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3057 ASSERT_EQ(0, motionArgs.buttonState);
3058 ASSERT_EQ(0, mFakePointerController->getButtonState());
3059 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3060 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 -08003061 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3062 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003063
3064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 ASSERT_EQ(0, motionArgs.buttonState);
3066 ASSERT_EQ(0, mFakePointerController->getButtonState());
3067 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3069 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 -08003070
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3072 ASSERT_EQ(0, motionArgs.buttonState);
3073 ASSERT_EQ(0, mFakePointerController->getButtonState());
3074 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3076 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3077
3078 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003079 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3080 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3082 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3083 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003084
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003086 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3088 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3090 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3091
3092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3094 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3095 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3097 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3098
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003099 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3100 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003102 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 ASSERT_EQ(0, motionArgs.buttonState);
3104 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3106 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3107
3108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003110 ASSERT_EQ(0, motionArgs.buttonState);
3111 ASSERT_EQ(0, mFakePointerController->getButtonState());
3112
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3114 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3116 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3117 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3118
3119 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003120 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3121 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3123 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3124 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003125
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003127 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3129 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3131 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3132
3133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3135 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3136 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3138 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3139
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003140 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3141 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 ASSERT_EQ(0, motionArgs.buttonState);
3145 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3147 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 -08003148
3149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3150 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3151 ASSERT_EQ(0, motionArgs.buttonState);
3152 ASSERT_EQ(0, mFakePointerController->getButtonState());
3153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3154 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3155
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3157 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3158 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3159
3160 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003161 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3162 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3164 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3165 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003166
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003168 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003169 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3170 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3172 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3173
3174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3176 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3177 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3179 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3180
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003181 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3182 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185 ASSERT_EQ(0, motionArgs.buttonState);
3186 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3188 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 -08003189
3190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3191 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3192 ASSERT_EQ(0, motionArgs.buttonState);
3193 ASSERT_EQ(0, mFakePointerController->getButtonState());
3194 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3198 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3199 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3200
3201 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003202 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3203 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3205 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3206 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003207
Michael Wrightd02c5b62014-02-10 15:10:22 -08003208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003209 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3211 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3213 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3214
3215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3217 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3218 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3220 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3221
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003222 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3223 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226 ASSERT_EQ(0, motionArgs.buttonState);
3227 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3229 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 -08003230
3231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3232 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3233 ASSERT_EQ(0, motionArgs.buttonState);
3234 ASSERT_EQ(0, mFakePointerController->getButtonState());
3235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3236 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3237
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3239 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3240 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3241}
3242
3243TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3244 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3245 addConfigurationProperty("cursor.mode", "pointer");
3246 addMapperAndConfigure(mapper);
3247
3248 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3249 mFakePointerController->setPosition(100, 200);
3250 mFakePointerController->setButtonState(0);
3251
3252 NotifyMotionArgs args;
3253
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003254 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3255 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3256 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003258 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3259 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3261 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3262 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3263}
3264
3265TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3266 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3267 addConfigurationProperty("cursor.mode", "pointer");
3268 mFakePolicy->setPointerCapture(true);
3269 addMapperAndConfigure(mapper);
3270
3271 NotifyDeviceResetArgs resetArgs;
3272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3273 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3274 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3275
3276 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3277 mFakePointerController->setPosition(100, 200);
3278 mFakePointerController->setButtonState(0);
3279
3280 NotifyMotionArgs args;
3281
3282 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003283 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3284 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3285 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3287 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3290 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3291 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3292
3293 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003294 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3295 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3297 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3298 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3300 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3302 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3305 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3306
3307 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003308 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3309 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3311 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3314 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3316 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3317 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3319 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3320
3321 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003322 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3323 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3324 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3326 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3329 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3330 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3331
3332 // Disable pointer capture and check that the device generation got bumped
3333 // and events are generated the usual way.
3334 const uint32_t generation = mFakeContext->getGeneration();
3335 mFakePolicy->setPointerCapture(false);
3336 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3337 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3338
3339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3340 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3341 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3342
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003343 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3344 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3345 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3347 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3350 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3351 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3352}
3353
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003354TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3355 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3356 addMapperAndConfigure(mapper);
3357
3358 // Setup PointerController for second display.
3359 constexpr int32_t SECOND_DISPLAY_ID = 1;
3360 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3361 mFakePointerController->setPosition(100, 200);
3362 mFakePointerController->setButtonState(0);
3363 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3364
3365 NotifyMotionArgs args;
3366 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3367 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3368 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3370 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3371 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3373 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3374 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3375 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3376}
3377
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378
3379// --- TouchInputMapperTest ---
3380
3381class TouchInputMapperTest : public InputMapperTest {
3382protected:
3383 static const int32_t RAW_X_MIN;
3384 static const int32_t RAW_X_MAX;
3385 static const int32_t RAW_Y_MIN;
3386 static const int32_t RAW_Y_MAX;
3387 static const int32_t RAW_TOUCH_MIN;
3388 static const int32_t RAW_TOUCH_MAX;
3389 static const int32_t RAW_TOOL_MIN;
3390 static const int32_t RAW_TOOL_MAX;
3391 static const int32_t RAW_PRESSURE_MIN;
3392 static const int32_t RAW_PRESSURE_MAX;
3393 static const int32_t RAW_ORIENTATION_MIN;
3394 static const int32_t RAW_ORIENTATION_MAX;
3395 static const int32_t RAW_DISTANCE_MIN;
3396 static const int32_t RAW_DISTANCE_MAX;
3397 static const int32_t RAW_TILT_MIN;
3398 static const int32_t RAW_TILT_MAX;
3399 static const int32_t RAW_ID_MIN;
3400 static const int32_t RAW_ID_MAX;
3401 static const int32_t RAW_SLOT_MIN;
3402 static const int32_t RAW_SLOT_MAX;
3403 static const float X_PRECISION;
3404 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003405 static const float X_PRECISION_VIRTUAL;
3406 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407
3408 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003409 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410
3411 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3412
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003413 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003414 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003415
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416 enum Axes {
3417 POSITION = 1 << 0,
3418 TOUCH = 1 << 1,
3419 TOOL = 1 << 2,
3420 PRESSURE = 1 << 3,
3421 ORIENTATION = 1 << 4,
3422 MINOR = 1 << 5,
3423 ID = 1 << 6,
3424 DISTANCE = 1 << 7,
3425 TILT = 1 << 8,
3426 SLOT = 1 << 9,
3427 TOOL_TYPE = 1 << 10,
3428 };
3429
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003430 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3431 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003432 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003434 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435 int32_t toRawX(float displayX);
3436 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003437 float toCookedX(float rawX, float rawY);
3438 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003440 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003442 float toDisplayY(int32_t rawY, int32_t displayHeight);
3443
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444};
3445
3446const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3447const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3448const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3449const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3450const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3451const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3452const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3453const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003454const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3455const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3457const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3458const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3459const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3460const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3461const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3462const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3463const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3464const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3465const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3466const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3467const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003468const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3469 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3470const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3471 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003472const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3473 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474
3475const float TouchInputMapperTest::GEOMETRIC_SCALE =
3476 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3477 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3478
3479const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3480 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3481 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3482};
3483
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003484void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003485 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003486 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3487}
3488
3489void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3490 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3491 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492}
3493
Santos Cordonfa5cf462017-04-05 10:37:00 -07003494void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003495 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3496 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003497 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003498}
3499
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500void TouchInputMapperTest::prepareVirtualKeys() {
3501 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3502 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3503 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3504 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3505}
3506
Jason Gerecke489fda82012-09-07 17:19:40 -07003507void TouchInputMapperTest::prepareLocationCalibration() {
3508 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3509}
3510
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511int32_t TouchInputMapperTest::toRawX(float displayX) {
3512 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3513}
3514
3515int32_t TouchInputMapperTest::toRawY(float displayY) {
3516 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3517}
3518
Jason Gerecke489fda82012-09-07 17:19:40 -07003519float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3520 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3521 return rawX;
3522}
3523
3524float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3525 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3526 return rawY;
3527}
3528
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003530 return toDisplayX(rawX, DISPLAY_WIDTH);
3531}
3532
3533float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3534 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535}
3536
3537float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003538 return toDisplayY(rawY, DISPLAY_HEIGHT);
3539}
3540
3541float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3542 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543}
3544
3545
3546// --- SingleTouchInputMapperTest ---
3547
3548class SingleTouchInputMapperTest : public TouchInputMapperTest {
3549protected:
3550 void prepareButtons();
3551 void prepareAxes(int axes);
3552
3553 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3554 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3555 void processUp(SingleTouchInputMapper* mappery);
3556 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3557 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3558 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3559 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3560 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3561 void processSync(SingleTouchInputMapper* mapper);
3562};
3563
3564void SingleTouchInputMapperTest::prepareButtons() {
3565 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3566}
3567
3568void SingleTouchInputMapperTest::prepareAxes(int axes) {
3569 if (axes & POSITION) {
3570 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3571 RAW_X_MIN, RAW_X_MAX, 0, 0);
3572 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3573 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3574 }
3575 if (axes & PRESSURE) {
3576 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3577 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3578 }
3579 if (axes & TOOL) {
3580 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3581 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3582 }
3583 if (axes & DISTANCE) {
3584 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3585 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3586 }
3587 if (axes & TILT) {
3588 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3589 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3590 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3591 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3592 }
3593}
3594
3595void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003596 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3597 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3598 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003599}
3600
3601void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003602 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3603 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604}
3605
3606void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003607 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608}
3609
3610void SingleTouchInputMapperTest::processPressure(
3611 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003612 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613}
3614
3615void SingleTouchInputMapperTest::processToolMajor(
3616 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003617 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618}
3619
3620void SingleTouchInputMapperTest::processDistance(
3621 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003622 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623}
3624
3625void SingleTouchInputMapperTest::processTilt(
3626 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003627 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3628 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629}
3630
3631void SingleTouchInputMapperTest::processKey(
3632 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003633 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634}
3635
3636void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003637 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638}
3639
3640
3641TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3642 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3643 prepareButtons();
3644 prepareAxes(POSITION);
3645 addMapperAndConfigure(mapper);
3646
3647 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3648}
3649
3650TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3651 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3652 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3653 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3654 prepareButtons();
3655 prepareAxes(POSITION);
3656 addMapperAndConfigure(mapper);
3657
3658 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3659}
3660
3661TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3662 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3663 prepareButtons();
3664 prepareAxes(POSITION);
3665 addConfigurationProperty("touch.deviceType", "touchPad");
3666 addMapperAndConfigure(mapper);
3667
3668 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3669}
3670
3671TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3672 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3673 prepareButtons();
3674 prepareAxes(POSITION);
3675 addConfigurationProperty("touch.deviceType", "touchScreen");
3676 addMapperAndConfigure(mapper);
3677
3678 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3679}
3680
3681TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3682 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3683 addConfigurationProperty("touch.deviceType", "touchScreen");
3684 prepareDisplay(DISPLAY_ORIENTATION_0);
3685 prepareButtons();
3686 prepareAxes(POSITION);
3687 prepareVirtualKeys();
3688 addMapperAndConfigure(mapper);
3689
3690 // Unknown key.
3691 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3692
3693 // Virtual key is down.
3694 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3695 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3696 processDown(mapper, x, y);
3697 processSync(mapper);
3698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3699
3700 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3701
3702 // Virtual key is up.
3703 processUp(mapper);
3704 processSync(mapper);
3705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3706
3707 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3708}
3709
3710TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3711 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3712 addConfigurationProperty("touch.deviceType", "touchScreen");
3713 prepareDisplay(DISPLAY_ORIENTATION_0);
3714 prepareButtons();
3715 prepareAxes(POSITION);
3716 prepareVirtualKeys();
3717 addMapperAndConfigure(mapper);
3718
3719 // Unknown key.
3720 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3721
3722 // Virtual key is down.
3723 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3724 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3725 processDown(mapper, x, y);
3726 processSync(mapper);
3727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3728
3729 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3730
3731 // Virtual key is up.
3732 processUp(mapper);
3733 processSync(mapper);
3734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3735
3736 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3737}
3738
3739TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3740 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3741 addConfigurationProperty("touch.deviceType", "touchScreen");
3742 prepareDisplay(DISPLAY_ORIENTATION_0);
3743 prepareButtons();
3744 prepareAxes(POSITION);
3745 prepareVirtualKeys();
3746 addMapperAndConfigure(mapper);
3747
3748 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3749 uint8_t flags[2] = { 0, 0 };
3750 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3751 ASSERT_TRUE(flags[0]);
3752 ASSERT_FALSE(flags[1]);
3753}
3754
3755TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3756 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3757 addConfigurationProperty("touch.deviceType", "touchScreen");
3758 prepareDisplay(DISPLAY_ORIENTATION_0);
3759 prepareButtons();
3760 prepareAxes(POSITION);
3761 prepareVirtualKeys();
3762 addMapperAndConfigure(mapper);
3763
3764 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3765
3766 NotifyKeyArgs args;
3767
3768 // Press virtual key.
3769 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3770 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3771 processDown(mapper, x, y);
3772 processSync(mapper);
3773
3774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3775 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3776 ASSERT_EQ(DEVICE_ID, args.deviceId);
3777 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3778 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3779 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3780 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3781 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3782 ASSERT_EQ(KEY_HOME, args.scanCode);
3783 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3784 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3785
3786 // Release virtual key.
3787 processUp(mapper);
3788 processSync(mapper);
3789
3790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3791 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3792 ASSERT_EQ(DEVICE_ID, args.deviceId);
3793 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3794 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3795 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3796 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3797 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3798 ASSERT_EQ(KEY_HOME, args.scanCode);
3799 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3800 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3801
3802 // Should not have sent any motions.
3803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3804}
3805
3806TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3807 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3808 addConfigurationProperty("touch.deviceType", "touchScreen");
3809 prepareDisplay(DISPLAY_ORIENTATION_0);
3810 prepareButtons();
3811 prepareAxes(POSITION);
3812 prepareVirtualKeys();
3813 addMapperAndConfigure(mapper);
3814
3815 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3816
3817 NotifyKeyArgs keyArgs;
3818
3819 // Press virtual key.
3820 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3821 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3822 processDown(mapper, x, y);
3823 processSync(mapper);
3824
3825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3826 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3827 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3828 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3829 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3830 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3831 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3832 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3833 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3834 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3835 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3836
3837 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3838 // into the display area.
3839 y -= 100;
3840 processMove(mapper, x, y);
3841 processSync(mapper);
3842
3843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3844 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3845 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3846 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3847 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3848 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3849 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3850 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3851 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3852 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3853 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3854 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3855
3856 NotifyMotionArgs motionArgs;
3857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3858 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3859 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3860 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3861 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3862 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3863 ASSERT_EQ(0, motionArgs.flags);
3864 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3865 ASSERT_EQ(0, motionArgs.buttonState);
3866 ASSERT_EQ(0, motionArgs.edgeFlags);
3867 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3868 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3871 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3872 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3873 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3874 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3875
3876 // Keep moving out of bounds. Should generate a pointer move.
3877 y -= 50;
3878 processMove(mapper, x, y);
3879 processSync(mapper);
3880
3881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3882 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3883 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3884 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3885 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3887 ASSERT_EQ(0, motionArgs.flags);
3888 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3889 ASSERT_EQ(0, motionArgs.buttonState);
3890 ASSERT_EQ(0, motionArgs.edgeFlags);
3891 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3892 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3895 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3896 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3897 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3898 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3899
3900 // Release out of bounds. Should generate a pointer up.
3901 processUp(mapper);
3902 processSync(mapper);
3903
3904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3906 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3907 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3908 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3909 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3910 ASSERT_EQ(0, motionArgs.flags);
3911 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3912 ASSERT_EQ(0, motionArgs.buttonState);
3913 ASSERT_EQ(0, motionArgs.edgeFlags);
3914 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3915 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3918 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3919 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3920 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3921 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3922
3923 // Should not have sent any more keys or motions.
3924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3926}
3927
3928TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3929 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3930 addConfigurationProperty("touch.deviceType", "touchScreen");
3931 prepareDisplay(DISPLAY_ORIENTATION_0);
3932 prepareButtons();
3933 prepareAxes(POSITION);
3934 prepareVirtualKeys();
3935 addMapperAndConfigure(mapper);
3936
3937 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3938
3939 NotifyMotionArgs motionArgs;
3940
3941 // Initially go down out of bounds.
3942 int32_t x = -10;
3943 int32_t y = -10;
3944 processDown(mapper, x, y);
3945 processSync(mapper);
3946
3947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3948
3949 // Move into the display area. Should generate a pointer down.
3950 x = 50;
3951 y = 75;
3952 processMove(mapper, x, y);
3953 processSync(mapper);
3954
3955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3956 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3957 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3958 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3959 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3960 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3961 ASSERT_EQ(0, motionArgs.flags);
3962 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3963 ASSERT_EQ(0, motionArgs.buttonState);
3964 ASSERT_EQ(0, motionArgs.edgeFlags);
3965 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3966 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3969 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3970 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3971 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3972 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3973
3974 // Release. Should generate a pointer up.
3975 processUp(mapper);
3976 processSync(mapper);
3977
3978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3979 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3980 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3981 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3982 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3983 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3984 ASSERT_EQ(0, motionArgs.flags);
3985 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3986 ASSERT_EQ(0, motionArgs.buttonState);
3987 ASSERT_EQ(0, motionArgs.edgeFlags);
3988 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3989 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3992 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3993 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3994 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3995 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3996
3997 // Should not have sent any more keys or motions.
3998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4000}
4001
Santos Cordonfa5cf462017-04-05 10:37:00 -07004002TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
4003 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4004 addConfigurationProperty("touch.deviceType", "touchScreen");
4005 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4006
4007 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4008 prepareButtons();
4009 prepareAxes(POSITION);
4010 prepareVirtualKeys();
4011 addMapperAndConfigure(mapper);
4012
4013 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4014
4015 NotifyMotionArgs motionArgs;
4016
4017 // Down.
4018 int32_t x = 100;
4019 int32_t y = 125;
4020 processDown(mapper, x, y);
4021 processSync(mapper);
4022
4023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4024 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4025 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4026 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4027 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4028 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4029 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4030 ASSERT_EQ(0, motionArgs.flags);
4031 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4032 ASSERT_EQ(0, motionArgs.buttonState);
4033 ASSERT_EQ(0, motionArgs.edgeFlags);
4034 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4035 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4038 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4039 1, 0, 0, 0, 0, 0, 0, 0));
4040 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4041 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4042 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4043
4044 // Move.
4045 x += 50;
4046 y += 75;
4047 processMove(mapper, x, y);
4048 processSync(mapper);
4049
4050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4051 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4052 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4053 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4054 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4055 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4057 ASSERT_EQ(0, motionArgs.flags);
4058 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4059 ASSERT_EQ(0, motionArgs.buttonState);
4060 ASSERT_EQ(0, motionArgs.edgeFlags);
4061 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4062 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4065 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4066 1, 0, 0, 0, 0, 0, 0, 0));
4067 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4068 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4069 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4070
4071 // Up.
4072 processUp(mapper);
4073 processSync(mapper);
4074
4075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4076 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4077 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4078 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4079 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4080 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4081 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4082 ASSERT_EQ(0, motionArgs.flags);
4083 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4084 ASSERT_EQ(0, motionArgs.buttonState);
4085 ASSERT_EQ(0, motionArgs.edgeFlags);
4086 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4087 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4088 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4090 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4091 1, 0, 0, 0, 0, 0, 0, 0));
4092 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4093 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4094 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4095
4096 // Should not have sent any more keys or motions.
4097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4099}
4100
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
4102 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4103 addConfigurationProperty("touch.deviceType", "touchScreen");
4104 prepareDisplay(DISPLAY_ORIENTATION_0);
4105 prepareButtons();
4106 prepareAxes(POSITION);
4107 prepareVirtualKeys();
4108 addMapperAndConfigure(mapper);
4109
4110 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4111
4112 NotifyMotionArgs motionArgs;
4113
4114 // Down.
4115 int32_t x = 100;
4116 int32_t y = 125;
4117 processDown(mapper, x, y);
4118 processSync(mapper);
4119
4120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4121 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4122 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4123 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4124 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4125 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4126 ASSERT_EQ(0, motionArgs.flags);
4127 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4128 ASSERT_EQ(0, motionArgs.buttonState);
4129 ASSERT_EQ(0, motionArgs.edgeFlags);
4130 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4131 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4134 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4135 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4136 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4137 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4138
4139 // Move.
4140 x += 50;
4141 y += 75;
4142 processMove(mapper, x, y);
4143 processSync(mapper);
4144
4145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4146 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4147 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4148 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4149 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4151 ASSERT_EQ(0, motionArgs.flags);
4152 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4153 ASSERT_EQ(0, motionArgs.buttonState);
4154 ASSERT_EQ(0, motionArgs.edgeFlags);
4155 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4156 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4159 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4160 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4161 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4162 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4163
4164 // Up.
4165 processUp(mapper);
4166 processSync(mapper);
4167
4168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4169 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4170 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4171 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4172 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4173 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4174 ASSERT_EQ(0, motionArgs.flags);
4175 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4176 ASSERT_EQ(0, motionArgs.buttonState);
4177 ASSERT_EQ(0, motionArgs.edgeFlags);
4178 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4179 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4182 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4183 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4184 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4186
4187 // Should not have sent any more keys or motions.
4188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4190}
4191
4192TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
4193 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4194 addConfigurationProperty("touch.deviceType", "touchScreen");
4195 prepareButtons();
4196 prepareAxes(POSITION);
4197 addConfigurationProperty("touch.orientationAware", "0");
4198 addMapperAndConfigure(mapper);
4199
4200 NotifyMotionArgs args;
4201
4202 // Rotation 90.
4203 prepareDisplay(DISPLAY_ORIENTATION_90);
4204 processDown(mapper, toRawX(50), toRawY(75));
4205 processSync(mapper);
4206
4207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4208 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4209 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4210
4211 processUp(mapper);
4212 processSync(mapper);
4213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4214}
4215
4216TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4217 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4218 addConfigurationProperty("touch.deviceType", "touchScreen");
4219 prepareButtons();
4220 prepareAxes(POSITION);
4221 addMapperAndConfigure(mapper);
4222
4223 NotifyMotionArgs args;
4224
4225 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004226 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227 prepareDisplay(DISPLAY_ORIENTATION_0);
4228 processDown(mapper, toRawX(50), toRawY(75));
4229 processSync(mapper);
4230
4231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4232 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4233 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4234
4235 processUp(mapper);
4236 processSync(mapper);
4237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4238
4239 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004240 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241 prepareDisplay(DISPLAY_ORIENTATION_90);
4242 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4243 processSync(mapper);
4244
4245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4246 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4247 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4248
4249 processUp(mapper);
4250 processSync(mapper);
4251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4252
4253 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004254 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 prepareDisplay(DISPLAY_ORIENTATION_180);
4256 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4257 processSync(mapper);
4258
4259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4260 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4261 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4262
4263 processUp(mapper);
4264 processSync(mapper);
4265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4266
4267 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004268 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 prepareDisplay(DISPLAY_ORIENTATION_270);
4270 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4271 processSync(mapper);
4272
4273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4274 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4275 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4276
4277 processUp(mapper);
4278 processSync(mapper);
4279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4280}
4281
4282TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4283 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4284 addConfigurationProperty("touch.deviceType", "touchScreen");
4285 prepareDisplay(DISPLAY_ORIENTATION_0);
4286 prepareButtons();
4287 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4288 addMapperAndConfigure(mapper);
4289
4290 // These calculations are based on the input device calibration documentation.
4291 int32_t rawX = 100;
4292 int32_t rawY = 200;
4293 int32_t rawPressure = 10;
4294 int32_t rawToolMajor = 12;
4295 int32_t rawDistance = 2;
4296 int32_t rawTiltX = 30;
4297 int32_t rawTiltY = 110;
4298
4299 float x = toDisplayX(rawX);
4300 float y = toDisplayY(rawY);
4301 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4302 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4303 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4304 float distance = float(rawDistance);
4305
4306 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4307 float tiltScale = M_PI / 180;
4308 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4309 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4310 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4311 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4312
4313 processDown(mapper, rawX, rawY);
4314 processPressure(mapper, rawPressure);
4315 processToolMajor(mapper, rawToolMajor);
4316 processDistance(mapper, rawDistance);
4317 processTilt(mapper, rawTiltX, rawTiltY);
4318 processSync(mapper);
4319
4320 NotifyMotionArgs args;
4321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4323 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4324 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4325}
4326
Jason Gerecke489fda82012-09-07 17:19:40 -07004327TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4328 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4329 addConfigurationProperty("touch.deviceType", "touchScreen");
4330 prepareDisplay(DISPLAY_ORIENTATION_0);
4331 prepareLocationCalibration();
4332 prepareButtons();
4333 prepareAxes(POSITION);
4334 addMapperAndConfigure(mapper);
4335
4336 int32_t rawX = 100;
4337 int32_t rawY = 200;
4338
4339 float x = toDisplayX(toCookedX(rawX, rawY));
4340 float y = toDisplayY(toCookedY(rawX, rawY));
4341
4342 processDown(mapper, rawX, rawY);
4343 processSync(mapper);
4344
4345 NotifyMotionArgs args;
4346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4348 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4349}
4350
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4352 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4353 addConfigurationProperty("touch.deviceType", "touchScreen");
4354 prepareDisplay(DISPLAY_ORIENTATION_0);
4355 prepareButtons();
4356 prepareAxes(POSITION);
4357 addMapperAndConfigure(mapper);
4358
4359 NotifyMotionArgs motionArgs;
4360 NotifyKeyArgs keyArgs;
4361
4362 processDown(mapper, 100, 200);
4363 processSync(mapper);
4364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4365 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4366 ASSERT_EQ(0, motionArgs.buttonState);
4367
4368 // press BTN_LEFT, release BTN_LEFT
4369 processKey(mapper, BTN_LEFT, 1);
4370 processSync(mapper);
4371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4373 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4374
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4377 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4378
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 processKey(mapper, BTN_LEFT, 0);
4380 processSync(mapper);
4381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004382 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004384
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004387 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388
4389 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4390 processKey(mapper, BTN_RIGHT, 1);
4391 processKey(mapper, BTN_MIDDLE, 1);
4392 processSync(mapper);
4393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4395 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4396 motionArgs.buttonState);
4397
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4400 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4401
4402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4404 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4405 motionArgs.buttonState);
4406
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 processKey(mapper, BTN_RIGHT, 0);
4408 processSync(mapper);
4409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412
4413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416
4417 processKey(mapper, BTN_MIDDLE, 0);
4418 processSync(mapper);
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004422
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004425 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426
4427 // press BTN_BACK, release BTN_BACK
4428 processKey(mapper, BTN_BACK, 1);
4429 processSync(mapper);
4430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4431 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4432 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004436 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4437
4438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4440 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441
4442 processKey(mapper, BTN_BACK, 0);
4443 processSync(mapper);
4444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004445 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004447
4448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004450 ASSERT_EQ(0, motionArgs.buttonState);
4451
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4453 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4454 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4455
4456 // press BTN_SIDE, release BTN_SIDE
4457 processKey(mapper, BTN_SIDE, 1);
4458 processSync(mapper);
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4460 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4461 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004465 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4466
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4469 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470
4471 processKey(mapper, BTN_SIDE, 0);
4472 processSync(mapper);
4473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004474 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004476
4477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004479 ASSERT_EQ(0, motionArgs.buttonState);
4480
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4482 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4483 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4484
4485 // press BTN_FORWARD, release BTN_FORWARD
4486 processKey(mapper, BTN_FORWARD, 1);
4487 processSync(mapper);
4488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4489 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4490 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004491
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004494 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4495
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4498 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499
4500 processKey(mapper, BTN_FORWARD, 0);
4501 processSync(mapper);
4502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004505
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004508 ASSERT_EQ(0, motionArgs.buttonState);
4509
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4511 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4512 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4513
4514 // press BTN_EXTRA, release BTN_EXTRA
4515 processKey(mapper, BTN_EXTRA, 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_FORWARD, 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_FORWARD, 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_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528
4529 processKey(mapper, BTN_EXTRA, 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_FORWARD, keyArgs.keyCode);
4542
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4544
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 // press BTN_STYLUS, release BTN_STYLUS
4546 processKey(mapper, BTN_STYLUS, 1);
4547 processSync(mapper);
4548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004550 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4551
4552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4553 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4554 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555
4556 processKey(mapper, BTN_STYLUS, 0);
4557 processSync(mapper);
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004561
4562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004564 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565
4566 // press BTN_STYLUS2, release BTN_STYLUS2
4567 processKey(mapper, BTN_STYLUS2, 1);
4568 processSync(mapper);
4569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004571 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4572
4573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4574 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4575 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576
4577 processKey(mapper, BTN_STYLUS2, 0);
4578 processSync(mapper);
4579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004580 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004582
4583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004585 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586
4587 // release touch
4588 processUp(mapper);
4589 processSync(mapper);
4590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4591 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4592 ASSERT_EQ(0, motionArgs.buttonState);
4593}
4594
4595TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4596 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4597 addConfigurationProperty("touch.deviceType", "touchScreen");
4598 prepareDisplay(DISPLAY_ORIENTATION_0);
4599 prepareButtons();
4600 prepareAxes(POSITION);
4601 addMapperAndConfigure(mapper);
4602
4603 NotifyMotionArgs motionArgs;
4604
4605 // default tool type is finger
4606 processDown(mapper, 100, 200);
4607 processSync(mapper);
4608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4609 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4611
4612 // eraser
4613 processKey(mapper, BTN_TOOL_RUBBER, 1);
4614 processSync(mapper);
4615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4618
4619 // stylus
4620 processKey(mapper, BTN_TOOL_RUBBER, 0);
4621 processKey(mapper, BTN_TOOL_PEN, 1);
4622 processSync(mapper);
4623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4626
4627 // brush
4628 processKey(mapper, BTN_TOOL_PEN, 0);
4629 processKey(mapper, BTN_TOOL_BRUSH, 1);
4630 processSync(mapper);
4631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4634
4635 // pencil
4636 processKey(mapper, BTN_TOOL_BRUSH, 0);
4637 processKey(mapper, BTN_TOOL_PENCIL, 1);
4638 processSync(mapper);
4639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4642
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004643 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644 processKey(mapper, BTN_TOOL_PENCIL, 0);
4645 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4646 processSync(mapper);
4647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4650
4651 // mouse
4652 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4653 processKey(mapper, BTN_TOOL_MOUSE, 1);
4654 processSync(mapper);
4655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4658
4659 // lens
4660 processKey(mapper, BTN_TOOL_MOUSE, 0);
4661 processKey(mapper, BTN_TOOL_LENS, 1);
4662 processSync(mapper);
4663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4666
4667 // double-tap
4668 processKey(mapper, BTN_TOOL_LENS, 0);
4669 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4670 processSync(mapper);
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4674
4675 // triple-tap
4676 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4677 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4678 processSync(mapper);
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4681 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4682
4683 // quad-tap
4684 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4685 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4686 processSync(mapper);
4687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4690
4691 // finger
4692 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4693 processKey(mapper, BTN_TOOL_FINGER, 1);
4694 processSync(mapper);
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4698
4699 // stylus trumps finger
4700 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
4705
4706 // eraser trumps stylus
4707 processKey(mapper, BTN_TOOL_RUBBER, 1);
4708 processSync(mapper);
4709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4712
4713 // mouse trumps eraser
4714 processKey(mapper, BTN_TOOL_MOUSE, 1);
4715 processSync(mapper);
4716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4719
4720 // back to default tool type
4721 processKey(mapper, BTN_TOOL_MOUSE, 0);
4722 processKey(mapper, BTN_TOOL_RUBBER, 0);
4723 processKey(mapper, BTN_TOOL_PEN, 0);
4724 processKey(mapper, BTN_TOOL_FINGER, 0);
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_FINGER, motionArgs.pointerProperties[0].toolType);
4729}
4730
4731TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4732 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4733 addConfigurationProperty("touch.deviceType", "touchScreen");
4734 prepareDisplay(DISPLAY_ORIENTATION_0);
4735 prepareButtons();
4736 prepareAxes(POSITION);
4737 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4738 addMapperAndConfigure(mapper);
4739
4740 NotifyMotionArgs motionArgs;
4741
4742 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4743 processKey(mapper, BTN_TOOL_FINGER, 1);
4744 processMove(mapper, 100, 200);
4745 processSync(mapper);
4746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4747 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4749 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4750
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4754 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4755
4756 // move a little
4757 processMove(mapper, 150, 250);
4758 processSync(mapper);
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4760 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4762 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4763
4764 // down when BTN_TOUCH is pressed, pressure defaults to 1
4765 processKey(mapper, BTN_TOUCH, 1);
4766 processSync(mapper);
4767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4768 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4770 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4771
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4773 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4775 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4776
4777 // up when BTN_TOUCH is released, hover restored
4778 processKey(mapper, BTN_TOUCH, 0);
4779 processSync(mapper);
4780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4781 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4783 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4784
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4786 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4788 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4789
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4791 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4793 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4794
4795 // exit hover when pointer goes away
4796 processKey(mapper, BTN_TOOL_FINGER, 0);
4797 processSync(mapper);
4798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4799 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4801 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4802}
4803
4804TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4805 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4806 addConfigurationProperty("touch.deviceType", "touchScreen");
4807 prepareDisplay(DISPLAY_ORIENTATION_0);
4808 prepareButtons();
4809 prepareAxes(POSITION | PRESSURE);
4810 addMapperAndConfigure(mapper);
4811
4812 NotifyMotionArgs motionArgs;
4813
4814 // initially hovering because pressure is 0
4815 processDown(mapper, 100, 200);
4816 processPressure(mapper, 0);
4817 processSync(mapper);
4818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4819 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4821 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4822
4823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4824 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4826 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4827
4828 // move a little
4829 processMove(mapper, 150, 250);
4830 processSync(mapper);
4831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4832 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4834 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4835
4836 // down when pressure is non-zero
4837 processPressure(mapper, RAW_PRESSURE_MAX);
4838 processSync(mapper);
4839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4840 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4842 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4843
4844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4845 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4847 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4848
4849 // up when pressure becomes 0, hover restored
4850 processPressure(mapper, 0);
4851 processSync(mapper);
4852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4853 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4855 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4856
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4860 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4861
4862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4863 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4865 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4866
4867 // exit hover when pointer goes away
4868 processUp(mapper);
4869 processSync(mapper);
4870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4871 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4873 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4874}
4875
Dan Harmsaca28402018-12-17 13:55:20 -08004876
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877// --- MultiTouchInputMapperTest ---
4878
4879class MultiTouchInputMapperTest : public TouchInputMapperTest {
4880protected:
4881 void prepareAxes(int axes);
4882
4883 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4884 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4885 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4886 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4887 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4888 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4889 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4890 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4891 void processId(MultiTouchInputMapper* mapper, int32_t id);
4892 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4893 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4894 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4895 void processMTSync(MultiTouchInputMapper* mapper);
4896 void processSync(MultiTouchInputMapper* mapper);
4897};
4898
4899void MultiTouchInputMapperTest::prepareAxes(int axes) {
4900 if (axes & POSITION) {
4901 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4902 RAW_X_MIN, RAW_X_MAX, 0, 0);
4903 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4904 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4905 }
4906 if (axes & TOUCH) {
4907 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4908 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4909 if (axes & MINOR) {
4910 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4911 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4912 }
4913 }
4914 if (axes & TOOL) {
4915 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4916 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4917 if (axes & MINOR) {
4918 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4919 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4920 }
4921 }
4922 if (axes & ORIENTATION) {
4923 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4924 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4925 }
4926 if (axes & PRESSURE) {
4927 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4928 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4929 }
4930 if (axes & DISTANCE) {
4931 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4932 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4933 }
4934 if (axes & ID) {
4935 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4936 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4937 }
4938 if (axes & SLOT) {
4939 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4940 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4941 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4942 }
4943 if (axes & TOOL_TYPE) {
4944 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4945 0, MT_TOOL_MAX, 0, 0);
4946 }
4947}
4948
4949void MultiTouchInputMapperTest::processPosition(
4950 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004951 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4952 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953}
4954
4955void MultiTouchInputMapperTest::processTouchMajor(
4956 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004957 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958}
4959
4960void MultiTouchInputMapperTest::processTouchMinor(
4961 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004962 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963}
4964
4965void MultiTouchInputMapperTest::processToolMajor(
4966 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004967 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968}
4969
4970void MultiTouchInputMapperTest::processToolMinor(
4971 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004972 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973}
4974
4975void MultiTouchInputMapperTest::processOrientation(
4976 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004977 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978}
4979
4980void MultiTouchInputMapperTest::processPressure(
4981 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004982 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983}
4984
4985void MultiTouchInputMapperTest::processDistance(
4986 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004987 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988}
4989
4990void MultiTouchInputMapperTest::processId(
4991 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004992 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993}
4994
4995void MultiTouchInputMapperTest::processSlot(
4996 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004997 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998}
4999
5000void MultiTouchInputMapperTest::processToolType(
5001 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005002 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003}
5004
5005void MultiTouchInputMapperTest::processKey(
5006 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005007 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008}
5009
5010void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005011 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012}
5013
5014void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005015 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016}
5017
5018
5019TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
5020 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5021 addConfigurationProperty("touch.deviceType", "touchScreen");
5022 prepareDisplay(DISPLAY_ORIENTATION_0);
5023 prepareAxes(POSITION);
5024 prepareVirtualKeys();
5025 addMapperAndConfigure(mapper);
5026
5027 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5028
5029 NotifyMotionArgs motionArgs;
5030
5031 // Two fingers down at once.
5032 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5033 processPosition(mapper, x1, y1);
5034 processMTSync(mapper);
5035 processPosition(mapper, x2, y2);
5036 processMTSync(mapper);
5037 processSync(mapper);
5038
5039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5040 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5041 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5042 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5043 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5044 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5045 ASSERT_EQ(0, motionArgs.flags);
5046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5047 ASSERT_EQ(0, motionArgs.buttonState);
5048 ASSERT_EQ(0, motionArgs.edgeFlags);
5049 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5050 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5053 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5054 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5055 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5056 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5057
5058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5059 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5060 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5061 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5062 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5063 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5064 motionArgs.action);
5065 ASSERT_EQ(0, motionArgs.flags);
5066 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5067 ASSERT_EQ(0, motionArgs.buttonState);
5068 ASSERT_EQ(0, motionArgs.edgeFlags);
5069 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5070 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5072 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5073 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5075 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5077 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5078 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5079 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5081
5082 // Move.
5083 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5084 processPosition(mapper, x1, y1);
5085 processMTSync(mapper);
5086 processPosition(mapper, x2, y2);
5087 processMTSync(mapper);
5088 processSync(mapper);
5089
5090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5091 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5092 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5093 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5094 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5096 ASSERT_EQ(0, motionArgs.flags);
5097 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5098 ASSERT_EQ(0, motionArgs.buttonState);
5099 ASSERT_EQ(0, motionArgs.edgeFlags);
5100 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5101 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5103 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5104 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5106 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5108 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5109 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5110 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5111 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5112
5113 // First finger up.
5114 x2 += 15; y2 -= 20;
5115 processPosition(mapper, x2, y2);
5116 processMTSync(mapper);
5117 processSync(mapper);
5118
5119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5120 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5121 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5122 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5123 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5124 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5125 motionArgs.action);
5126 ASSERT_EQ(0, motionArgs.flags);
5127 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5128 ASSERT_EQ(0, motionArgs.buttonState);
5129 ASSERT_EQ(0, motionArgs.edgeFlags);
5130 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5131 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5133 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5136 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5138 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5139 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5140 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5141 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5142
5143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5144 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5145 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5146 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5147 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5149 ASSERT_EQ(0, motionArgs.flags);
5150 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5151 ASSERT_EQ(0, motionArgs.buttonState);
5152 ASSERT_EQ(0, motionArgs.edgeFlags);
5153 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5154 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5157 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5158 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5159 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5160 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5161
5162 // Move.
5163 x2 += 20; y2 -= 25;
5164 processPosition(mapper, x2, y2);
5165 processMTSync(mapper);
5166 processSync(mapper);
5167
5168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5169 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5170 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5171 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5172 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5174 ASSERT_EQ(0, motionArgs.flags);
5175 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5176 ASSERT_EQ(0, motionArgs.buttonState);
5177 ASSERT_EQ(0, motionArgs.edgeFlags);
5178 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5179 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5182 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5183 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5184 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5186
5187 // New finger down.
5188 int32_t x3 = 700, y3 = 300;
5189 processPosition(mapper, x2, y2);
5190 processMTSync(mapper);
5191 processPosition(mapper, x3, y3);
5192 processMTSync(mapper);
5193 processSync(mapper);
5194
5195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5196 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5197 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5198 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5199 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5200 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5201 motionArgs.action);
5202 ASSERT_EQ(0, motionArgs.flags);
5203 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5204 ASSERT_EQ(0, motionArgs.buttonState);
5205 ASSERT_EQ(0, motionArgs.edgeFlags);
5206 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5207 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5209 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5210 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5212 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5214 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5215 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5216 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5217 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5218
5219 // Second finger up.
5220 x3 += 30; y3 -= 20;
5221 processPosition(mapper, x3, y3);
5222 processMTSync(mapper);
5223 processSync(mapper);
5224
5225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5226 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5227 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5228 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5229 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5230 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5231 motionArgs.action);
5232 ASSERT_EQ(0, motionArgs.flags);
5233 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5234 ASSERT_EQ(0, motionArgs.buttonState);
5235 ASSERT_EQ(0, motionArgs.edgeFlags);
5236 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5237 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5239 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5242 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5250 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5251 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5252 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5253 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5255 ASSERT_EQ(0, motionArgs.flags);
5256 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5257 ASSERT_EQ(0, motionArgs.buttonState);
5258 ASSERT_EQ(0, motionArgs.edgeFlags);
5259 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5260 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5263 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5264 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5265 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5266 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5267
5268 // Last finger up.
5269 processMTSync(mapper);
5270 processSync(mapper);
5271
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5277 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5278 ASSERT_EQ(0, motionArgs.flags);
5279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5280 ASSERT_EQ(0, motionArgs.buttonState);
5281 ASSERT_EQ(0, motionArgs.edgeFlags);
5282 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5286 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5287 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5288 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5290
5291 // Should not have sent any more keys or motions.
5292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5294}
5295
5296TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5297 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5298 addConfigurationProperty("touch.deviceType", "touchScreen");
5299 prepareDisplay(DISPLAY_ORIENTATION_0);
5300 prepareAxes(POSITION | ID);
5301 prepareVirtualKeys();
5302 addMapperAndConfigure(mapper);
5303
5304 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5305
5306 NotifyMotionArgs motionArgs;
5307
5308 // Two fingers down at once.
5309 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5310 processPosition(mapper, x1, y1);
5311 processId(mapper, 1);
5312 processMTSync(mapper);
5313 processPosition(mapper, x2, y2);
5314 processId(mapper, 2);
5315 processMTSync(mapper);
5316 processSync(mapper);
5317
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5319 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5320 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5321 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5324 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5325
5326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5327 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5328 motionArgs.action);
5329 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5330 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5332 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5335 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5337 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5338
5339 // Move.
5340 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5341 processPosition(mapper, x1, y1);
5342 processId(mapper, 1);
5343 processMTSync(mapper);
5344 processPosition(mapper, x2, y2);
5345 processId(mapper, 2);
5346 processMTSync(mapper);
5347 processSync(mapper);
5348
5349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5351 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5352 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5354 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5357 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5359 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5360
5361 // First finger up.
5362 x2 += 15; y2 -= 20;
5363 processPosition(mapper, x2, y2);
5364 processId(mapper, 2);
5365 processMTSync(mapper);
5366 processSync(mapper);
5367
5368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5369 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5370 motionArgs.action);
5371 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5372 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5374 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5377 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5379 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5380
5381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5383 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5384 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5387 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5388
5389 // Move.
5390 x2 += 20; y2 -= 25;
5391 processPosition(mapper, x2, y2);
5392 processId(mapper, 2);
5393 processMTSync(mapper);
5394 processSync(mapper);
5395
5396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5398 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5399 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5402 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5403
5404 // New finger down.
5405 int32_t x3 = 700, y3 = 300;
5406 processPosition(mapper, x2, y2);
5407 processId(mapper, 2);
5408 processMTSync(mapper);
5409 processPosition(mapper, x3, y3);
5410 processId(mapper, 3);
5411 processMTSync(mapper);
5412 processSync(mapper);
5413
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5415 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5416 motionArgs.action);
5417 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5418 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5420 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5423 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5425 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5426
5427 // Second finger up.
5428 x3 += 30; y3 -= 20;
5429 processPosition(mapper, x3, y3);
5430 processId(mapper, 3);
5431 processMTSync(mapper);
5432 processSync(mapper);
5433
5434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5435 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5436 motionArgs.action);
5437 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5438 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5440 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5443 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5445 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5446
5447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5449 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5453 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5454
5455 // Last finger up.
5456 processMTSync(mapper);
5457 processSync(mapper);
5458
5459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5460 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5461 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5465 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5466
5467 // Should not have sent any more keys or motions.
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5470}
5471
5472TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5473 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5474 addConfigurationProperty("touch.deviceType", "touchScreen");
5475 prepareDisplay(DISPLAY_ORIENTATION_0);
5476 prepareAxes(POSITION | ID | SLOT);
5477 prepareVirtualKeys();
5478 addMapperAndConfigure(mapper);
5479
5480 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5481
5482 NotifyMotionArgs motionArgs;
5483
5484 // Two fingers down at once.
5485 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5486 processPosition(mapper, x1, y1);
5487 processId(mapper, 1);
5488 processSlot(mapper, 1);
5489 processPosition(mapper, x2, y2);
5490 processId(mapper, 2);
5491 processSync(mapper);
5492
5493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5494 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5495 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5496 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5499 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5500
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5502 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << 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(x1), toDisplayY(y1), 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 // Move.
5515 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5516 processSlot(mapper, 0);
5517 processPosition(mapper, x1, y1);
5518 processSlot(mapper, 1);
5519 processPosition(mapper, x2, y2);
5520 processSync(mapper);
5521
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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(x1), toDisplayY(y1), 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 // First finger up.
5535 x2 += 15; y2 -= 20;
5536 processSlot(mapper, 0);
5537 processId(mapper, -1);
5538 processSlot(mapper, 1);
5539 processPosition(mapper, x2, y2);
5540 processSync(mapper);
5541
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5543 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5544 motionArgs.action);
5545 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5546 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5548 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5551 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5553 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5554
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5557 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5558 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5561 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5562
5563 // Move.
5564 x2 += 20; y2 -= 25;
5565 processPosition(mapper, x2, y2);
5566 processSync(mapper);
5567
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5570 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5571 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5574 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5575
5576 // New finger down.
5577 int32_t x3 = 700, y3 = 300;
5578 processPosition(mapper, x2, y2);
5579 processSlot(mapper, 0);
5580 processId(mapper, 3);
5581 processPosition(mapper, x3, y3);
5582 processSync(mapper);
5583
5584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5585 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5586 motionArgs.action);
5587 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5588 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5590 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5593 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5595 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5596
5597 // Second finger up.
5598 x3 += 30; y3 -= 20;
5599 processSlot(mapper, 1);
5600 processId(mapper, -1);
5601 processSlot(mapper, 0);
5602 processPosition(mapper, x3, y3);
5603 processSync(mapper);
5604
5605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5606 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5607 motionArgs.action);
5608 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5609 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5611 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5614 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5616 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5617
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5620 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5621 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5624 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5625
5626 // Last finger up.
5627 processId(mapper, -1);
5628 processSync(mapper);
5629
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5631 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5632 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5636 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5637
5638 // Should not have sent any more keys or motions.
5639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5641}
5642
5643TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5644 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5645 addConfigurationProperty("touch.deviceType", "touchScreen");
5646 prepareDisplay(DISPLAY_ORIENTATION_0);
5647 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5648 addMapperAndConfigure(mapper);
5649
5650 // These calculations are based on the input device calibration documentation.
5651 int32_t rawX = 100;
5652 int32_t rawY = 200;
5653 int32_t rawTouchMajor = 7;
5654 int32_t rawTouchMinor = 6;
5655 int32_t rawToolMajor = 9;
5656 int32_t rawToolMinor = 8;
5657 int32_t rawPressure = 11;
5658 int32_t rawDistance = 0;
5659 int32_t rawOrientation = 3;
5660 int32_t id = 5;
5661
5662 float x = toDisplayX(rawX);
5663 float y = toDisplayY(rawY);
5664 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5665 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5666 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5667 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5668 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5669 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5670 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5671 float distance = float(rawDistance);
5672
5673 processPosition(mapper, rawX, rawY);
5674 processTouchMajor(mapper, rawTouchMajor);
5675 processTouchMinor(mapper, rawTouchMinor);
5676 processToolMajor(mapper, rawToolMajor);
5677 processToolMinor(mapper, rawToolMinor);
5678 processPressure(mapper, rawPressure);
5679 processOrientation(mapper, rawOrientation);
5680 processDistance(mapper, rawDistance);
5681 processId(mapper, id);
5682 processMTSync(mapper);
5683 processSync(mapper);
5684
5685 NotifyMotionArgs args;
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5687 ASSERT_EQ(0, args.pointerProperties[0].id);
5688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5689 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5690 orientation, distance));
5691}
5692
5693TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5694 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5695 addConfigurationProperty("touch.deviceType", "touchScreen");
5696 prepareDisplay(DISPLAY_ORIENTATION_0);
5697 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5698 addConfigurationProperty("touch.size.calibration", "geometric");
5699 addMapperAndConfigure(mapper);
5700
5701 // These calculations are based on the input device calibration documentation.
5702 int32_t rawX = 100;
5703 int32_t rawY = 200;
5704 int32_t rawTouchMajor = 140;
5705 int32_t rawTouchMinor = 120;
5706 int32_t rawToolMajor = 180;
5707 int32_t rawToolMinor = 160;
5708
5709 float x = toDisplayX(rawX);
5710 float y = toDisplayY(rawY);
5711 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5712 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5713 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5714 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5715 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5716
5717 processPosition(mapper, rawX, rawY);
5718 processTouchMajor(mapper, rawTouchMajor);
5719 processTouchMinor(mapper, rawTouchMinor);
5720 processToolMajor(mapper, rawToolMajor);
5721 processToolMinor(mapper, rawToolMinor);
5722 processMTSync(mapper);
5723 processSync(mapper);
5724
5725 NotifyMotionArgs args;
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5728 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5729}
5730
5731TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5732 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5733 addConfigurationProperty("touch.deviceType", "touchScreen");
5734 prepareDisplay(DISPLAY_ORIENTATION_0);
5735 prepareAxes(POSITION | TOUCH | TOOL);
5736 addConfigurationProperty("touch.size.calibration", "diameter");
5737 addConfigurationProperty("touch.size.scale", "10");
5738 addConfigurationProperty("touch.size.bias", "160");
5739 addConfigurationProperty("touch.size.isSummed", "1");
5740 addMapperAndConfigure(mapper);
5741
5742 // These calculations are based on the input device calibration documentation.
5743 // Note: We only provide a single common touch/tool value because the device is assumed
5744 // not to emit separate values for each pointer (isSummed = 1).
5745 int32_t rawX = 100;
5746 int32_t rawY = 200;
5747 int32_t rawX2 = 150;
5748 int32_t rawY2 = 250;
5749 int32_t rawTouchMajor = 5;
5750 int32_t rawToolMajor = 8;
5751
5752 float x = toDisplayX(rawX);
5753 float y = toDisplayY(rawY);
5754 float x2 = toDisplayX(rawX2);
5755 float y2 = toDisplayY(rawY2);
5756 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5757 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5758 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5759
5760 processPosition(mapper, rawX, rawY);
5761 processTouchMajor(mapper, rawTouchMajor);
5762 processToolMajor(mapper, rawToolMajor);
5763 processMTSync(mapper);
5764 processPosition(mapper, rawX2, rawY2);
5765 processTouchMajor(mapper, rawTouchMajor);
5766 processToolMajor(mapper, rawToolMajor);
5767 processMTSync(mapper);
5768 processSync(mapper);
5769
5770 NotifyMotionArgs args;
5771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5772 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5773
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5775 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5776 args.action);
5777 ASSERT_EQ(size_t(2), args.pointerCount);
5778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5779 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5781 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5782}
5783
5784TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5785 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5786 addConfigurationProperty("touch.deviceType", "touchScreen");
5787 prepareDisplay(DISPLAY_ORIENTATION_0);
5788 prepareAxes(POSITION | TOUCH | TOOL);
5789 addConfigurationProperty("touch.size.calibration", "area");
5790 addConfigurationProperty("touch.size.scale", "43");
5791 addConfigurationProperty("touch.size.bias", "3");
5792 addMapperAndConfigure(mapper);
5793
5794 // These calculations are based on the input device calibration documentation.
5795 int32_t rawX = 100;
5796 int32_t rawY = 200;
5797 int32_t rawTouchMajor = 5;
5798 int32_t rawToolMajor = 8;
5799
5800 float x = toDisplayX(rawX);
5801 float y = toDisplayY(rawY);
5802 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5803 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5804 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5805
5806 processPosition(mapper, rawX, rawY);
5807 processTouchMajor(mapper, rawTouchMajor);
5808 processToolMajor(mapper, rawToolMajor);
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, touch, touch, tool, tool, 0, 0));
5816}
5817
5818TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5819 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5820 addConfigurationProperty("touch.deviceType", "touchScreen");
5821 prepareDisplay(DISPLAY_ORIENTATION_0);
5822 prepareAxes(POSITION | PRESSURE);
5823 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5824 addConfigurationProperty("touch.pressure.scale", "0.01");
5825 addMapperAndConfigure(mapper);
5826
Michael Wrightaa449c92017-12-13 21:21:43 +00005827 InputDeviceInfo info;
5828 mapper->populateDeviceInfo(&info);
5829 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5830 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5831 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5832
Michael Wrightd02c5b62014-02-10 15:10:22 -08005833 // These calculations are based on the input device calibration documentation.
5834 int32_t rawX = 100;
5835 int32_t rawY = 200;
5836 int32_t rawPressure = 60;
5837
5838 float x = toDisplayX(rawX);
5839 float y = toDisplayY(rawY);
5840 float pressure = float(rawPressure) * 0.01f;
5841
5842 processPosition(mapper, rawX, rawY);
5843 processPressure(mapper, rawPressure);
5844 processMTSync(mapper);
5845 processSync(mapper);
5846
5847 NotifyMotionArgs args;
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5850 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5851}
5852
5853TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5854 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5855 addConfigurationProperty("touch.deviceType", "touchScreen");
5856 prepareDisplay(DISPLAY_ORIENTATION_0);
5857 prepareAxes(POSITION | ID | SLOT);
5858 addMapperAndConfigure(mapper);
5859
5860 NotifyMotionArgs motionArgs;
5861 NotifyKeyArgs keyArgs;
5862
5863 processId(mapper, 1);
5864 processPosition(mapper, 100, 200);
5865 processSync(mapper);
5866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5867 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5868 ASSERT_EQ(0, motionArgs.buttonState);
5869
5870 // press BTN_LEFT, release BTN_LEFT
5871 processKey(mapper, BTN_LEFT, 1);
5872 processSync(mapper);
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5875 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5876
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5879 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5880
Michael Wrightd02c5b62014-02-10 15:10:22 -08005881 processKey(mapper, BTN_LEFT, 0);
5882 processSync(mapper);
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005884 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005886
5887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005889 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890
5891 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5892 processKey(mapper, BTN_RIGHT, 1);
5893 processKey(mapper, BTN_MIDDLE, 1);
5894 processSync(mapper);
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5897 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5898 motionArgs.buttonState);
5899
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5901 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5902 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5903
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5905 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5906 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5907 motionArgs.buttonState);
5908
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909 processKey(mapper, BTN_RIGHT, 0);
5910 processSync(mapper);
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005914
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005917 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918
5919 processKey(mapper, BTN_MIDDLE, 0);
5920 processSync(mapper);
5921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005922 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005924
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005927 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928
5929 // press BTN_BACK, release BTN_BACK
5930 processKey(mapper, BTN_BACK, 1);
5931 processSync(mapper);
5932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5933 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5934 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005935
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005938 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5939
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5942 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943
5944 processKey(mapper, BTN_BACK, 0);
5945 processSync(mapper);
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005948 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005949
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005952 ASSERT_EQ(0, motionArgs.buttonState);
5953
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5955 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5956 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5957
5958 // press BTN_SIDE, release BTN_SIDE
5959 processKey(mapper, BTN_SIDE, 1);
5960 processSync(mapper);
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5962 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5963 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005964
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005967 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5968
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5970 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5971 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972
5973 processKey(mapper, BTN_SIDE, 0);
5974 processSync(mapper);
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005976 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005978
5979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005981 ASSERT_EQ(0, motionArgs.buttonState);
5982
Michael Wrightd02c5b62014-02-10 15:10:22 -08005983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5984 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5985 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5986
5987 // press BTN_FORWARD, release BTN_FORWARD
5988 processKey(mapper, BTN_FORWARD, 1);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5991 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5992 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005993
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005996 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5997
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6000 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006001
6002 processKey(mapper, BTN_FORWARD, 0);
6003 processSync(mapper);
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006006 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006007
6008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006010 ASSERT_EQ(0, motionArgs.buttonState);
6011
Michael Wrightd02c5b62014-02-10 15:10:22 -08006012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6013 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6014 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6015
6016 // press BTN_EXTRA, release BTN_EXTRA
6017 processKey(mapper, BTN_EXTRA, 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_FORWARD, 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_FORWARD, 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_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030
6031 processKey(mapper, BTN_EXTRA, 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_FORWARD, keyArgs.keyCode);
6044
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6046
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047 // press BTN_STYLUS, release BTN_STYLUS
6048 processKey(mapper, BTN_STYLUS, 1);
6049 processSync(mapper);
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006052 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6053
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6056 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057
6058 processKey(mapper, BTN_STYLUS, 0);
6059 processSync(mapper);
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006066 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067
6068 // press BTN_STYLUS2, release BTN_STYLUS2
6069 processKey(mapper, BTN_STYLUS2, 1);
6070 processSync(mapper);
6071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006073 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6074
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6077 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078
6079 processKey(mapper, BTN_STYLUS2, 0);
6080 processSync(mapper);
6081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006083 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006084
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006087 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088
6089 // release touch
6090 processId(mapper, -1);
6091 processSync(mapper);
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6093 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6094 ASSERT_EQ(0, motionArgs.buttonState);
6095}
6096
6097TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
6098 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6099 addConfigurationProperty("touch.deviceType", "touchScreen");
6100 prepareDisplay(DISPLAY_ORIENTATION_0);
6101 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6102 addMapperAndConfigure(mapper);
6103
6104 NotifyMotionArgs motionArgs;
6105
6106 // default tool type is finger
6107 processId(mapper, 1);
6108 processPosition(mapper, 100, 200);
6109 processSync(mapper);
6110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6111 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6113
6114 // eraser
6115 processKey(mapper, BTN_TOOL_RUBBER, 1);
6116 processSync(mapper);
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6120
6121 // stylus
6122 processKey(mapper, BTN_TOOL_RUBBER, 0);
6123 processKey(mapper, BTN_TOOL_PEN, 1);
6124 processSync(mapper);
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6128
6129 // brush
6130 processKey(mapper, BTN_TOOL_PEN, 0);
6131 processKey(mapper, BTN_TOOL_BRUSH, 1);
6132 processSync(mapper);
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6136
6137 // pencil
6138 processKey(mapper, BTN_TOOL_BRUSH, 0);
6139 processKey(mapper, BTN_TOOL_PENCIL, 1);
6140 processSync(mapper);
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6144
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006145 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146 processKey(mapper, BTN_TOOL_PENCIL, 0);
6147 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6148 processSync(mapper);
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6152
6153 // mouse
6154 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6155 processKey(mapper, BTN_TOOL_MOUSE, 1);
6156 processSync(mapper);
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6160
6161 // lens
6162 processKey(mapper, BTN_TOOL_MOUSE, 0);
6163 processKey(mapper, BTN_TOOL_LENS, 1);
6164 processSync(mapper);
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6168
6169 // double-tap
6170 processKey(mapper, BTN_TOOL_LENS, 0);
6171 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6172 processSync(mapper);
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6176
6177 // triple-tap
6178 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6179 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6180 processSync(mapper);
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6184
6185 // quad-tap
6186 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6187 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6188 processSync(mapper);
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6192
6193 // finger
6194 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6195 processKey(mapper, BTN_TOOL_FINGER, 1);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6200
6201 // stylus trumps finger
6202 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
6207
6208 // eraser trumps stylus
6209 processKey(mapper, BTN_TOOL_RUBBER, 1);
6210 processSync(mapper);
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6214
6215 // mouse trumps eraser
6216 processKey(mapper, BTN_TOOL_MOUSE, 1);
6217 processSync(mapper);
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6221
6222 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6223 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6224 processSync(mapper);
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6228
6229 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6230 processToolType(mapper, MT_TOOL_PEN);
6231 processSync(mapper);
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6235
6236 // back to default tool type
6237 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6238 processKey(mapper, BTN_TOOL_MOUSE, 0);
6239 processKey(mapper, BTN_TOOL_RUBBER, 0);
6240 processKey(mapper, BTN_TOOL_PEN, 0);
6241 processKey(mapper, BTN_TOOL_FINGER, 0);
6242 processSync(mapper);
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6246}
6247
6248TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6249 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6250 addConfigurationProperty("touch.deviceType", "touchScreen");
6251 prepareDisplay(DISPLAY_ORIENTATION_0);
6252 prepareAxes(POSITION | ID | SLOT);
6253 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6254 addMapperAndConfigure(mapper);
6255
6256 NotifyMotionArgs motionArgs;
6257
6258 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6259 processId(mapper, 1);
6260 processPosition(mapper, 100, 200);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6265 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6266
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6270 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6271
6272 // move a little
6273 processPosition(mapper, 150, 250);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6278 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6279
6280 // down when BTN_TOUCH is pressed, pressure defaults to 1
6281 processKey(mapper, BTN_TOUCH, 1);
6282 processSync(mapper);
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6284 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6286 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6287
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6291 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6292
6293 // up when BTN_TOUCH is released, hover restored
6294 processKey(mapper, BTN_TOUCH, 0);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6299 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6300
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6302 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6304 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6305
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6309 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6310
6311 // exit hover when pointer goes away
6312 processId(mapper, -1);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6315 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6317 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6318}
6319
6320TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6321 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6322 addConfigurationProperty("touch.deviceType", "touchScreen");
6323 prepareDisplay(DISPLAY_ORIENTATION_0);
6324 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6325 addMapperAndConfigure(mapper);
6326
6327 NotifyMotionArgs motionArgs;
6328
6329 // initially hovering because pressure is 0
6330 processId(mapper, 1);
6331 processPosition(mapper, 100, 200);
6332 processPressure(mapper, 0);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6337 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6338
6339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6340 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6342 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6343
6344 // move a little
6345 processPosition(mapper, 150, 250);
6346 processSync(mapper);
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6350 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6351
6352 // down when pressure becomes non-zero
6353 processPressure(mapper, RAW_PRESSURE_MAX);
6354 processSync(mapper);
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6356 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6358 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6359
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6363 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6364
6365 // up when pressure becomes 0, hover restored
6366 processPressure(mapper, 0);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6371 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6372
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6376 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6377
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6379 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6381 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6382
6383 // exit hover when pointer goes away
6384 processId(mapper, -1);
6385 processSync(mapper);
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6389 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6390}
6391
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006392/**
6393 * Set the input device port <--> display port associations, and check that the
6394 * events are routed to the display that matches the display port.
6395 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6396 */
6397TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6398 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6399 const std::string usb2 = "USB2";
6400 const uint8_t hdmi1 = 0;
6401 const uint8_t hdmi2 = 1;
6402 const std::string secondaryUniqueId = "uniqueId2";
6403 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6404
6405 addConfigurationProperty("touch.deviceType", "touchScreen");
6406 prepareAxes(POSITION);
6407 addMapperAndConfigure(mapper);
6408
6409 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6410 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6411
6412 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6413 // for this input device is specified, and the matching viewport is not present,
6414 // the input device should be disabled (at the mapper level).
6415
6416 // Add viewport for display 2 on hdmi2
6417 prepareSecondaryDisplay(type, hdmi2);
6418 // Send a touch event
6419 processPosition(mapper, 100, 100);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6422
6423 // Add viewport for display 1 on hdmi1
6424 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6425 // Send a touch event again
6426 processPosition(mapper, 100, 100);
6427 processSync(mapper);
6428
6429 NotifyMotionArgs args;
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6431 ASSERT_EQ(DISPLAY_ID, args.displayId);
6432}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433
Arthur Hung41a712e2018-11-22 19:41:03 +08006434/**
6435 * Expect fallback to internal viewport if device is external and external viewport is not present.
6436 */
6437TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6438 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6439 prepareAxes(POSITION);
6440 addConfigurationProperty("touch.deviceType", "touchScreen");
6441 prepareDisplay(DISPLAY_ORIENTATION_0);
6442 mDevice->setExternal(true);
6443 addMapperAndConfigure(mapper);
6444
6445 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6446
6447 NotifyMotionArgs motionArgs;
6448
6449 // Expect the event to be sent to the internal viewport,
6450 // because an external viewport is not present.
6451 processPosition(mapper, 100, 100);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6454 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6455
6456 // Expect the event to be sent to the external viewport if it is present.
6457 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6458 processPosition(mapper, 100, 100);
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6462}
6463
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006464TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6465 // Setup PointerController for second display.
6466 sp<FakePointerController> fakePointerController = new FakePointerController();
6467 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6468 fakePointerController->setPosition(100, 200);
6469 fakePointerController->setButtonState(0);
6470 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6471 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6472
6473 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6474 prepareDisplay(DISPLAY_ORIENTATION_0);
6475 prepareAxes(POSITION);
6476 addMapperAndConfigure(mapper);
6477
6478 // Check source is mouse that would obtain the PointerController.
6479 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6480
6481 NotifyMotionArgs motionArgs;
6482 processPosition(mapper, 100, 100);
6483 processSync(mapper);
6484
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6487 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6488}
6489
Arthur Hung7c645402019-01-25 17:45:42 +08006490TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6491 // Setup the first touch screen device.
6492 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6493 prepareAxes(POSITION | ID | SLOT);
6494 addConfigurationProperty("touch.deviceType", "touchScreen");
6495 addMapperAndConfigure(mapper);
6496
6497 // Create the second touch screen device, and enable multi fingers.
6498 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006499 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006500 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006501 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006502 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006503 std::unique_ptr<InputDevice> device2 =
6504 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6505 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006506 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6507 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6508 0 /*flat*/, 0 /*fuzz*/);
6509 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6510 0 /*flat*/, 0 /*fuzz*/);
6511 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6512 0 /*flat*/, 0 /*fuzz*/);
6513 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6514 0 /*flat*/, 0 /*fuzz*/);
6515 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6516 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6517 String8("touchScreen"));
6518
6519 // Setup the second touch screen device.
Arthur Hung2c9a3342019-07-23 14:18:59 +08006520 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2.get());
Arthur Hung7c645402019-01-25 17:45:42 +08006521 device2->addMapper(mapper2);
6522 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6523 device2->reset(ARBITRARY_TIME);
6524
6525 // Setup PointerController.
6526 sp<FakePointerController> fakePointerController = new FakePointerController();
6527 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6528 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6529
6530 // Setup policy for associated displays and show touches.
6531 const uint8_t hdmi1 = 0;
6532 const uint8_t hdmi2 = 1;
6533 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6534 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6535 mFakePolicy->setShowTouches(true);
6536
6537 // Create displays.
6538 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6539 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6540
6541 // Default device will reconfigure above, need additional reconfiguration for another device.
6542 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6543 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6544
6545 // Two fingers down at default display.
6546 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6547 processPosition(mapper, x1, y1);
6548 processId(mapper, 1);
6549 processSlot(mapper, 1);
6550 processPosition(mapper, x2, y2);
6551 processId(mapper, 2);
6552 processSync(mapper);
6553
6554 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6555 fakePointerController->getSpots().find(DISPLAY_ID);
6556 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6557 ASSERT_EQ(size_t(2), iter->second.size());
6558
6559 // Two fingers down at second display.
6560 processPosition(mapper2, x1, y1);
6561 processId(mapper2, 1);
6562 processSlot(mapper2, 1);
6563 processPosition(mapper2, x2, y2);
6564 processId(mapper2, 2);
6565 processSync(mapper2);
6566
6567 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6568 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6569 ASSERT_EQ(size_t(2), iter->second.size());
6570}
6571
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006572TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6573 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6574 prepareAxes(POSITION);
6575 addConfigurationProperty("touch.deviceType", "touchScreen");
6576 prepareDisplay(DISPLAY_ORIENTATION_0);
6577 addMapperAndConfigure(mapper);
6578
6579 NotifyMotionArgs motionArgs;
6580 // Unrotated video frame
6581 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6582 std::vector<TouchVideoFrame> frames{frame};
6583 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6584 processPosition(mapper, 100, 200);
6585 processSync(mapper);
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6587 ASSERT_EQ(frames, motionArgs.videoFrames);
6588
6589 // Subsequent touch events should not have any videoframes
6590 // This is implemented separately in FakeEventHub,
6591 // but that should match the behaviour of TouchVideoDevice.
6592 processPosition(mapper, 200, 200);
6593 processSync(mapper);
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6595 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6596}
6597
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006598TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6599 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6600 prepareAxes(POSITION);
6601 addConfigurationProperty("touch.deviceType", "touchScreen");
6602 addMapperAndConfigure(mapper);
6603 // Unrotated video frame
6604 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6605 NotifyMotionArgs motionArgs;
6606
6607 // Test all 4 orientations
6608 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6609 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6610 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6611 clearViewports();
6612 prepareDisplay(orientation);
6613 std::vector<TouchVideoFrame> frames{frame};
6614 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6615 processPosition(mapper, 100, 200);
6616 processSync(mapper);
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6618 frames[0].rotate(orientation);
6619 ASSERT_EQ(frames, motionArgs.videoFrames);
6620 }
6621}
6622
6623TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6624 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6625 prepareAxes(POSITION);
6626 addConfigurationProperty("touch.deviceType", "touchScreen");
6627 addMapperAndConfigure(mapper);
6628 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6629 // so mix these.
6630 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6631 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6632 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6633 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6634 NotifyMotionArgs motionArgs;
6635
6636 prepareDisplay(DISPLAY_ORIENTATION_90);
6637 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6638 processPosition(mapper, 100, 200);
6639 processSync(mapper);
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6641 std::for_each(frames.begin(), frames.end(),
6642 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6643 ASSERT_EQ(frames, motionArgs.videoFrames);
6644}
6645
Arthur Hung9da14732019-09-02 16:16:58 +08006646/**
6647 * If we had defined port associations, but the viewport is not ready, the touch device would be
6648 * expected to be disabled, and it should be enabled after the viewport has found.
6649 */
6650TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
6651 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6652 constexpr uint8_t hdmi2 = 1;
6653 const std::string secondaryUniqueId = "uniqueId2";
6654 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6655
6656 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6657
6658 addConfigurationProperty("touch.deviceType", "touchScreen");
6659 prepareAxes(POSITION);
6660 addMapperAndConfigure(mapper);
6661
6662 ASSERT_EQ(mDevice->isEnabled(), false);
6663
6664 // Add display on hdmi2, the device should be enabled and can receive touch event.
6665 prepareSecondaryDisplay(type, hdmi2);
6666 ASSERT_EQ(mDevice->isEnabled(), true);
6667
6668 // Send a touch event.
6669 processPosition(mapper, 100, 100);
6670 processSync(mapper);
6671
6672 NotifyMotionArgs args;
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6674 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6675}
6676
Arthur Hung6cd19a42019-08-30 19:04:12 +08006677/**
6678 * Test touch should not work if outside of surface.
6679 */
6680TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
6681 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6682 addConfigurationProperty("touch.deviceType", "touchScreen");
6683 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006684 prepareAxes(POSITION);
6685 addMapperAndConfigure(mapper);
6686
Arthur Hung05de5772019-09-26 18:31:26 +08006687 // Touch on left-top area should work.
6688 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6689 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6690 processPosition(mapper, rawX, rawY);
6691 processSync(mapper);
6692
6693 NotifyMotionArgs args;
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6695
6696 // Reset.
6697 mapper->reset(ARBITRARY_TIME);
6698
6699 // Let logical display be different to physical display and rotate 90-degrees.
6700 std::optional<DisplayViewport> internalViewport =
6701 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6702 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6703 internalViewport->logicalLeft = 0;
6704 internalViewport->logicalTop = 0;
6705 internalViewport->logicalRight = DISPLAY_HEIGHT;
6706 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6707
6708 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6709 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6710 internalViewport->physicalRight = DISPLAY_HEIGHT;
6711 internalViewport->physicalBottom = DISPLAY_WIDTH;
6712
6713 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6714 internalViewport->deviceHeight = DISPLAY_WIDTH;
6715 mFakePolicy->updateViewport(internalViewport.value());
6716 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6717
6718 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006719 processPosition(mapper, rawX, rawY);
6720 processSync(mapper);
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6722}
6723
Michael Wrightd02c5b62014-02-10 15:10:22 -08006724} // namespace android