blob: 59f275f58dd83ac99b6fa8156692ce37ffddba90 [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
Prabir Pradhanba266f22019-09-03 17:11:27 -07001136 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1137 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001138};
1139
1140/**
1141 * Check that empty set of viewports is an acceptable configuration.
1142 * Also try to get internal viewport two different ways - by type and by uniqueId.
1143 *
1144 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1145 * Such configuration is not currently allowed.
1146 */
1147TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001148 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001149
1150 // We didn't add any viewports yet, so there shouldn't be any.
1151 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001152 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001153 ASSERT_FALSE(internalViewport);
1154
1155 // Add an internal viewport, then clear it
1156 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001157 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001158
1159 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001160 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001161 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001162 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001163
1164 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001165 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001166 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001167 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001168
1169 mFakePolicy->clearViewports();
1170 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001171 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001172 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001173 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001174 ASSERT_FALSE(internalViewport);
1175}
1176
1177TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1178 const std::string internalUniqueId = "local:0";
1179 const std::string externalUniqueId = "local:1";
1180 const std::string virtualUniqueId1 = "virtual:2";
1181 const std::string virtualUniqueId2 = "virtual:3";
1182 constexpr int32_t virtualDisplayId1 = 2;
1183 constexpr int32_t virtualDisplayId2 = 3;
1184
1185 // Add an internal viewport
1186 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001187 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001188 // Add an external viewport
1189 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001190 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001191 // Add an virtual viewport
1192 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001193 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001194 // Add another virtual viewport
1195 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001196 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001197
1198 // Check matching by type for internal
1199 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001200 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001201 ASSERT_TRUE(internalViewport);
1202 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1203
1204 // Check matching by type for external
1205 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001206 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001207 ASSERT_TRUE(externalViewport);
1208 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1209
1210 // Check matching by uniqueId for virtual viewport #1
1211 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001212 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001213 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001214 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001215 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1216 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1217
1218 // Check matching by uniqueId for virtual viewport #2
1219 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001220 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001221 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001222 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001223 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1224 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1225}
1226
1227
1228/**
1229 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1230 * that lookup works by checking display id.
1231 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1232 */
1233TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1234 const std::string uniqueId1 = "uniqueId1";
1235 const std::string uniqueId2 = "uniqueId2";
1236 constexpr int32_t displayId1 = 2;
1237 constexpr int32_t displayId2 = 3;
1238
1239 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1240 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1241 for (const ViewportType& type : types) {
1242 mFakePolicy->clearViewports();
1243 // Add a viewport
1244 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001245 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001246 // Add another viewport
1247 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001248 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001249
1250 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001251 std::optional<DisplayViewport> viewport1 =
1252 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001253 ASSERT_TRUE(viewport1);
1254 ASSERT_EQ(displayId1, viewport1->displayId);
1255 ASSERT_EQ(type, viewport1->type);
1256
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001257 std::optional<DisplayViewport> viewport2 =
1258 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001259 ASSERT_TRUE(viewport2);
1260 ASSERT_EQ(displayId2, viewport2->displayId);
1261 ASSERT_EQ(type, viewport2->type);
1262
1263 // When there are multiple viewports of the same kind, and uniqueId is not specified
1264 // in the call to getDisplayViewport, then that situation is not supported.
1265 // The viewports can be stored in any order, so we cannot rely on the order, since that
1266 // is just implementation detail.
1267 // However, we can check that it still returns *a* viewport, we just cannot assert
1268 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001269 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001270 ASSERT_TRUE(someViewport);
1271 }
1272}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001274/**
1275 * Check getDisplayViewportByPort
1276 */
1277TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1278 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1279 const std::string uniqueId1 = "uniqueId1";
1280 const std::string uniqueId2 = "uniqueId2";
1281 constexpr int32_t displayId1 = 1;
1282 constexpr int32_t displayId2 = 2;
1283 const uint8_t hdmi1 = 0;
1284 const uint8_t hdmi2 = 1;
1285 const uint8_t hdmi3 = 2;
1286
1287 mFakePolicy->clearViewports();
1288 // Add a viewport that's associated with some display port that's not of interest.
1289 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1290 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1291 // Add another viewport, connected to HDMI1 port
1292 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1293 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1294
1295 // Check that correct display viewport was returned by comparing the display ports.
1296 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1297 ASSERT_TRUE(hdmi1Viewport);
1298 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1299 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1300
1301 // Check that we can still get the same viewport using the uniqueId
1302 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1303 ASSERT_TRUE(hdmi1Viewport);
1304 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1305 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1306 ASSERT_EQ(type, hdmi1Viewport->type);
1307
1308 // Check that we cannot find a port with "HDMI2", because we never added one
1309 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1310 ASSERT_FALSE(hdmi2Viewport);
1311}
1312
Michael Wrightd02c5b62014-02-10 15:10:22 -08001313// --- InputReaderTest ---
1314
1315class InputReaderTest : public testing::Test {
1316protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001317 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001319 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhanba266f22019-09-03 17:11:27 -07001320 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321
Prabir Pradhanba266f22019-09-03 17:11:27 -07001322 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001323 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001325 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001326
Prabir Pradhanba266f22019-09-03 17:11:27 -07001327 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1328 mFakeListener);
1329 ASSERT_EQ(OK, mReader->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001330 }
1331
Prabir Pradhanba266f22019-09-03 17:11:27 -07001332 virtual void TearDown() override {
1333 ASSERT_EQ(OK, mReader->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001334
1335 mFakeListener.clear();
1336 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001337 }
1338
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001339 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001340 const PropertyMap* configuration) {
1341 mFakeEventHub->addDevice(deviceId, name, classes);
1342
1343 if (configuration) {
1344 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1345 }
1346 mFakeEventHub->finishDeviceScan();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001347 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1348 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001349 }
1350
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001351 void disableDevice(int32_t deviceId, InputDevice* device) {
1352 mFakePolicy->addDisabledDevice(deviceId);
1353 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1354 }
1355
1356 void enableDevice(int32_t deviceId, InputDevice* device) {
1357 mFakePolicy->removeDisabledDevice(deviceId);
1358 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1359 }
1360
1361 void configureDevice(uint32_t changes, InputDevice* device) {
1362 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1363 }
1364
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001366 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001367 const PropertyMap* configuration) {
1368 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1369 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1370 device->addMapper(mapper);
1371 mReader->setNextDevice(device);
1372 addDevice(deviceId, name, classes, configuration);
1373 return mapper;
1374 }
1375};
1376
1377TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001378 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001379 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001380 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001381 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001382
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001383 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001385 ASSERT_EQ(1U, inputDevices.size());
1386 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001387 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001388 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1389 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1390 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1391
1392 // Should also have received a notification describing the new input devices.
1393 inputDevices = mFakePolicy->getInputDevices();
1394 ASSERT_EQ(1U, inputDevices.size());
1395 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001396 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001397 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1398 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1399 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1400}
1401
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001402TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1403 constexpr int32_t deviceId = 1;
1404 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001405 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001406 // Must add at least one mapper or the device will be ignored!
1407 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1408 device->addMapper(mapper);
1409 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001410 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001411
Yi Kong9b14ac62018-07-17 13:48:38 -07001412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001413
1414 NotifyDeviceResetArgs resetArgs;
1415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1416 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1417 ASSERT_EQ(deviceId, resetArgs.deviceId);
1418
1419 ASSERT_EQ(device->isEnabled(), true);
1420 disableDevice(deviceId, device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001421
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001423 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1424 ASSERT_EQ(deviceId, resetArgs.deviceId);
1425 ASSERT_EQ(device->isEnabled(), false);
1426
1427 disableDevice(deviceId, device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001430 ASSERT_EQ(device->isEnabled(), false);
1431
1432 enableDevice(deviceId, device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001434 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1435 ASSERT_EQ(deviceId, resetArgs.deviceId);
1436 ASSERT_EQ(device->isEnabled(), true);
1437}
1438
Michael Wrightd02c5b62014-02-10 15:10:22 -08001439TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001440 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001441 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001442 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1444
1445 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1446 AINPUT_SOURCE_ANY, AKEYCODE_A))
1447 << "Should return unknown when the device id is >= 0 but unknown.";
1448
1449 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1450 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1451 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1452
1453 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1454 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1455 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1456
1457 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1458 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1459 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1460
1461 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1462 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1463 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1464}
1465
1466TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001467 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001468 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001469 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001470 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1471
1472 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1473 AINPUT_SOURCE_ANY, KEY_A))
1474 << "Should return unknown when the device id is >= 0 but unknown.";
1475
1476 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1477 AINPUT_SOURCE_TRACKBALL, KEY_A))
1478 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1479
1480 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1481 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1482 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1483
1484 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1485 AINPUT_SOURCE_TRACKBALL, KEY_A))
1486 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1487
1488 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1489 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1490 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1491}
1492
1493TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001494 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001495 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001496 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1498
1499 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1500 AINPUT_SOURCE_ANY, SW_LID))
1501 << "Should return unknown when the device id is >= 0 but unknown.";
1502
1503 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1504 AINPUT_SOURCE_TRACKBALL, SW_LID))
1505 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1506
1507 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1508 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1509 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1510
1511 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1512 AINPUT_SOURCE_TRACKBALL, SW_LID))
1513 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1514
1515 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1516 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1517 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1518}
1519
1520TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001521 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001522 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001523 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001524
Michael Wrightd02c5b62014-02-10 15:10:22 -08001525 mapper->addSupportedKeyCode(AKEYCODE_A);
1526 mapper->addSupportedKeyCode(AKEYCODE_B);
1527
1528 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1529 uint8_t flags[4] = { 0, 0, 0, 1 };
1530
1531 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1532 << "Should return false when device id is >= 0 but unknown.";
1533 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1534
1535 flags[3] = 1;
1536 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1537 << "Should return false when device id is valid but the sources are not supported by the device.";
1538 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1539
1540 flags[3] = 1;
1541 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1542 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1543 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1544
1545 flags[3] = 1;
1546 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1547 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1548 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1549
1550 flags[3] = 1;
1551 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1552 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1553 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1554}
1555
Prabir Pradhanba266f22019-09-03 17:11:27 -07001556TEST_F(InputReaderTest, WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001557 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558
1559 NotifyConfigurationChangedArgs args;
1560
1561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1562 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1563}
1564
Prabir Pradhanba266f22019-09-03 17:11:27 -07001565TEST_F(InputReaderTest, ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001566 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001567 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001568 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569
1570 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1572
1573 RawEvent event;
1574 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1575 ASSERT_EQ(0, event.when);
1576 ASSERT_EQ(1, event.deviceId);
1577 ASSERT_EQ(EV_KEY, event.type);
1578 ASSERT_EQ(KEY_A, event.code);
1579 ASSERT_EQ(1, event.value);
1580}
1581
Prabir Pradhan42611e02018-11-27 14:04:02 -08001582TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1583 constexpr int32_t deviceId = 1;
1584 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001585 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001586 // Must add at least one mapper or the device will be ignored!
1587 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1588 device->addMapper(mapper);
1589 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001590 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001591
1592 NotifyDeviceResetArgs resetArgs;
1593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1594 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1595
1596 disableDevice(deviceId, device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001598 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1599 prevSequenceNum = resetArgs.sequenceNum;
1600
1601 enableDevice(deviceId, device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001603 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1604 prevSequenceNum = resetArgs.sequenceNum;
1605
1606 disableDevice(deviceId, device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001608 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1609 prevSequenceNum = resetArgs.sequenceNum;
1610}
1611
Arthur Hungc23540e2018-11-29 20:42:11 +08001612TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1613 constexpr int32_t deviceId = 1;
1614 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1615 const char* DEVICE_LOCATION = "USB1";
1616 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1617 DEVICE_LOCATION);
1618 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1619 device->addMapper(mapper);
1620 mReader->setNextDevice(device);
1621 addDevice(deviceId, "fake", deviceClass, nullptr);
1622
1623 const uint8_t hdmi1 = 1;
1624
1625 // Associated touch screen with second display.
1626 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1627
1628 // Add default and second display.
1629 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1630 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1631 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1632 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1633 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001635
Arthur Hung2c9a3342019-07-23 14:18:59 +08001636 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001637 ASSERT_EQ(deviceId, device->getId());
1638 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1639 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001640
1641 // Can't dispatch event from a disabled device.
1642 disableDevice(deviceId, device);
1643 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001644}
1645
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646
1647// --- InputDeviceTest ---
1648
1649class InputDeviceTest : public testing::Test {
1650protected:
1651 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001652 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001653 static const int32_t DEVICE_ID;
1654 static const int32_t DEVICE_GENERATION;
1655 static const int32_t DEVICE_CONTROLLER_NUMBER;
1656 static const uint32_t DEVICE_CLASSES;
1657
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001658 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001659 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001660 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001661 FakeInputReaderContext* mFakeContext;
1662
1663 InputDevice* mDevice;
1664
Prabir Pradhanba266f22019-09-03 17:11:27 -07001665 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001666 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001667 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001668 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001669 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1670
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001671 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001672 InputDeviceIdentifier identifier;
1673 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001674 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001675 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1676 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1677 }
1678
Prabir Pradhanba266f22019-09-03 17:11:27 -07001679 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001680 delete mDevice;
1681
1682 delete mFakeContext;
1683 mFakeListener.clear();
1684 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001685 }
1686};
1687
1688const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001689const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001690const int32_t InputDeviceTest::DEVICE_ID = 1;
1691const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1692const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1693const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1694 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1695
1696TEST_F(InputDeviceTest, ImmutableProperties) {
1697 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001698 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001699 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1700}
1701
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001702TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1703 ASSERT_EQ(mDevice->isEnabled(), true);
1704}
1705
Michael Wrightd02c5b62014-02-10 15:10:22 -08001706TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1707 // Configuration.
1708 InputReaderConfiguration config;
1709 mDevice->configure(ARBITRARY_TIME, &config, 0);
1710
1711 // Reset.
1712 mDevice->reset(ARBITRARY_TIME);
1713
1714 NotifyDeviceResetArgs resetArgs;
1715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1716 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1717 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1718
1719 // Metadata.
1720 ASSERT_TRUE(mDevice->isIgnored());
1721 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1722
1723 InputDeviceInfo info;
1724 mDevice->getDeviceInfo(&info);
1725 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001726 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001727 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1728 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1729
1730 // State queries.
1731 ASSERT_EQ(0, mDevice->getMetaState());
1732
1733 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1734 << "Ignored device should return unknown key code state.";
1735 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1736 << "Ignored device should return unknown scan code state.";
1737 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1738 << "Ignored device should return unknown switch state.";
1739
1740 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1741 uint8_t flags[2] = { 0, 1 };
1742 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1743 << "Ignored device should never mark any key codes.";
1744 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1745 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1746}
1747
1748TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1749 // Configuration.
1750 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1751
1752 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1753 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1754 mapper1->setMetaState(AMETA_ALT_ON);
1755 mapper1->addSupportedKeyCode(AKEYCODE_A);
1756 mapper1->addSupportedKeyCode(AKEYCODE_B);
1757 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1758 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1759 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1760 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1761 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1762 mDevice->addMapper(mapper1);
1763
1764 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1765 mapper2->setMetaState(AMETA_SHIFT_ON);
1766 mDevice->addMapper(mapper2);
1767
1768 InputReaderConfiguration config;
1769 mDevice->configure(ARBITRARY_TIME, &config, 0);
1770
1771 String8 propertyValue;
1772 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1773 << "Device should have read configuration during configuration phase.";
1774 ASSERT_STREQ("value", propertyValue.string());
1775
1776 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1777 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1778
1779 // Reset
1780 mDevice->reset(ARBITRARY_TIME);
1781 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1782 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1783
1784 NotifyDeviceResetArgs resetArgs;
1785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1786 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1787 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1788
1789 // Metadata.
1790 ASSERT_FALSE(mDevice->isIgnored());
1791 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1792
1793 InputDeviceInfo info;
1794 mDevice->getDeviceInfo(&info);
1795 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001796 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001797 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1798 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1799
1800 // State queries.
1801 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1802 << "Should query mappers and combine meta states.";
1803
1804 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1805 << "Should return unknown key code state when source not supported.";
1806 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1807 << "Should return unknown scan code state when source not supported.";
1808 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1809 << "Should return unknown switch state when source not supported.";
1810
1811 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1812 << "Should query mapper when source is supported.";
1813 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1814 << "Should query mapper when source is supported.";
1815 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1816 << "Should query mapper when source is supported.";
1817
1818 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1819 uint8_t flags[4] = { 0, 0, 0, 1 };
1820 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1821 << "Should do nothing when source is unsupported.";
1822 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1823 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1824 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1825 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1826
1827 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1828 << "Should query mapper when source is supported.";
1829 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1830 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1831 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1832 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1833
1834 // Event handling.
1835 RawEvent event;
1836 mDevice->process(&event, 1);
1837
1838 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1839 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1840}
1841
Arthur Hung2c9a3342019-07-23 14:18:59 +08001842// A single input device is associated with a specific display. Check that:
1843// 1. Device is disabled if the viewport corresponding to the associated display is not found
1844// 2. Device is disabled when setEnabled API is called
1845TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
1846 FakeInputMapper* mapper = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1847 mDevice->addMapper(mapper);
1848
1849 // First Configuration.
1850 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1851
1852 // Device should be enabled by default.
1853 ASSERT_TRUE(mDevice->isEnabled());
1854
1855 // Prepare associated info.
1856 constexpr uint8_t hdmi = 1;
1857 const std::string UNIQUE_ID = "local:1";
1858
1859 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1860 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1861 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1862 // Device should be disabled because it is associated with a specific display via
1863 // input port <-> display port association, but the corresponding display is not found
1864 ASSERT_FALSE(mDevice->isEnabled());
1865
1866 // Prepare displays.
1867 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1868 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1869 ViewportType::VIEWPORT_INTERNAL);
1870 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1871 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1872 ASSERT_TRUE(mDevice->isEnabled());
1873
1874 // Device should be disabled after set disable.
1875 mFakePolicy->addDisabledDevice(mDevice->getId());
1876 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1877 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1878 ASSERT_FALSE(mDevice->isEnabled());
1879
1880 // Device should still be disabled even found the associated display.
1881 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1882 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1883 ASSERT_FALSE(mDevice->isEnabled());
1884}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001885
1886// --- InputMapperTest ---
1887
1888class InputMapperTest : public testing::Test {
1889protected:
1890 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001891 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001892 static const int32_t DEVICE_ID;
1893 static const int32_t DEVICE_GENERATION;
1894 static const int32_t DEVICE_CONTROLLER_NUMBER;
1895 static const uint32_t DEVICE_CLASSES;
1896
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001897 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001898 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001899 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001900 FakeInputReaderContext* mFakeContext;
1901 InputDevice* mDevice;
1902
Prabir Pradhanba266f22019-09-03 17:11:27 -07001903 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001904 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001905 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001906 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001907 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1908 InputDeviceIdentifier identifier;
1909 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001910 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001911 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1912 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1913
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001914 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915 }
1916
Prabir Pradhanba266f22019-09-03 17:11:27 -07001917 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 delete mDevice;
1919 delete mFakeContext;
1920 mFakeListener.clear();
1921 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001922 }
1923
1924 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001925 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001926 }
1927
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001928 void configureDevice(uint32_t changes) {
1929 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1930 }
1931
Michael Wrightd02c5b62014-02-10 15:10:22 -08001932 void addMapperAndConfigure(InputMapper* mapper) {
1933 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001934 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001935 mDevice->reset(ARBITRARY_TIME);
1936 }
1937
1938 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001939 int32_t orientation, const std::string& uniqueId,
1940 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001941 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001942 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001943 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1944 }
1945
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001946 void clearViewports() {
1947 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001948 }
1949
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001950 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001951 int32_t code, int32_t value) {
1952 RawEvent event;
1953 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001954 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001955 event.type = type;
1956 event.code = code;
1957 event.value = value;
1958 mapper->process(&event);
1959 }
1960
1961 static void assertMotionRange(const InputDeviceInfo& info,
1962 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1963 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001964 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001965 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1966 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1967 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1968 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1969 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1970 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1971 }
1972
1973 static void assertPointerCoords(const PointerCoords& coords,
1974 float x, float y, float pressure, float size,
1975 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1976 float orientation, float distance) {
1977 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1978 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1979 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1980 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1981 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1982 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1983 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1984 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1985 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1986 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1987 }
1988
1989 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1990 float actualX, actualY;
1991 controller->getPosition(&actualX, &actualY);
1992 ASSERT_NEAR(x, actualX, 1);
1993 ASSERT_NEAR(y, actualY, 1);
1994 }
1995};
1996
1997const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001998const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001999const int32_t InputMapperTest::DEVICE_ID = 1;
2000const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2001const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2002const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
2003
2004
2005// --- SwitchInputMapperTest ---
2006
2007class SwitchInputMapperTest : public InputMapperTest {
2008protected:
2009};
2010
2011TEST_F(SwitchInputMapperTest, GetSources) {
2012 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2013 addMapperAndConfigure(mapper);
2014
2015 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
2016}
2017
2018TEST_F(SwitchInputMapperTest, GetSwitchState) {
2019 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2020 addMapperAndConfigure(mapper);
2021
2022 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
2023 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2024
2025 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
2026 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2027}
2028
2029TEST_F(SwitchInputMapperTest, Process) {
2030 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2031 addMapperAndConfigure(mapper);
2032
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002033 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2034 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2035 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2036 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002037
2038 NotifySwitchArgs args;
2039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2040 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002041 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2042 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002043 args.switchMask);
2044 ASSERT_EQ(uint32_t(0), args.policyFlags);
2045}
2046
2047
2048// --- KeyboardInputMapperTest ---
2049
2050class KeyboardInputMapperTest : public InputMapperTest {
2051protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002052 const std::string UNIQUE_ID = "local:0";
2053
2054 void prepareDisplay(int32_t orientation);
2055
Arthur Hung2c9a3342019-07-23 14:18:59 +08002056 void testDPadKeyRotation(KeyboardInputMapper* mapper, int32_t originalScanCode,
2057 int32_t originalKeyCode, int32_t rotatedKeyCode,
2058 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002059};
2060
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002061/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2062 * orientation.
2063 */
2064void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2065 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002066 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002067}
2068
Michael Wrightd02c5b62014-02-10 15:10:22 -08002069void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002070 int32_t originalScanCode, int32_t originalKeyCode,
2071 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002072 NotifyKeyArgs args;
2073
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002074 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2076 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2077 ASSERT_EQ(originalScanCode, args.scanCode);
2078 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002079 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002080
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002081 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2083 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2084 ASSERT_EQ(originalScanCode, args.scanCode);
2085 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002086 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002087}
2088
Michael Wrightd02c5b62014-02-10 15:10:22 -08002089TEST_F(KeyboardInputMapperTest, GetSources) {
2090 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2091 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2092 addMapperAndConfigure(mapper);
2093
2094 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
2095}
2096
2097TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2098 const int32_t USAGE_A = 0x070004;
2099 const int32_t USAGE_UNKNOWN = 0x07ffff;
2100 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2101 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2102
2103 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2104 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2105 addMapperAndConfigure(mapper);
2106
2107 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002108 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002109 NotifyKeyArgs args;
2110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2111 ASSERT_EQ(DEVICE_ID, args.deviceId);
2112 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2113 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2114 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2115 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2116 ASSERT_EQ(KEY_HOME, args.scanCode);
2117 ASSERT_EQ(AMETA_NONE, args.metaState);
2118 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2119 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2120 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2121
2122 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002123 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2125 ASSERT_EQ(DEVICE_ID, args.deviceId);
2126 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2127 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2128 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2129 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2130 ASSERT_EQ(KEY_HOME, args.scanCode);
2131 ASSERT_EQ(AMETA_NONE, args.metaState);
2132 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2133 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2134 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2135
2136 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002137 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2138 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2140 ASSERT_EQ(DEVICE_ID, args.deviceId);
2141 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2142 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2143 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2144 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2145 ASSERT_EQ(0, args.scanCode);
2146 ASSERT_EQ(AMETA_NONE, args.metaState);
2147 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2148 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2149 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2150
2151 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002152 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2153 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2155 ASSERT_EQ(DEVICE_ID, args.deviceId);
2156 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2157 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2158 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2159 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2160 ASSERT_EQ(0, args.scanCode);
2161 ASSERT_EQ(AMETA_NONE, args.metaState);
2162 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2163 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2164 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2165
2166 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002167 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2168 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2170 ASSERT_EQ(DEVICE_ID, args.deviceId);
2171 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2172 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2173 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2174 ASSERT_EQ(0, args.keyCode);
2175 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2176 ASSERT_EQ(AMETA_NONE, args.metaState);
2177 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2178 ASSERT_EQ(0U, args.policyFlags);
2179 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2180
2181 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002182 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2183 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2185 ASSERT_EQ(DEVICE_ID, args.deviceId);
2186 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2187 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2188 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2189 ASSERT_EQ(0, args.keyCode);
2190 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2191 ASSERT_EQ(AMETA_NONE, args.metaState);
2192 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2193 ASSERT_EQ(0U, args.policyFlags);
2194 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2195}
2196
2197TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2198 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2199 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2200
2201 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2202 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2203 addMapperAndConfigure(mapper);
2204
2205 // Initial metastate.
2206 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2207
2208 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002209 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002210 NotifyKeyArgs args;
2211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2213 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2214 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2215
2216 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002217 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2220 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2221
2222 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002223 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2225 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2226 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2227
2228 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002229 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2231 ASSERT_EQ(AMETA_NONE, args.metaState);
2232 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2233 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2234}
2235
2236TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2237 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2238 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2239 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2240 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2241
2242 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2243 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2244 addMapperAndConfigure(mapper);
2245
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002246 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002247 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2248 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2249 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2250 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2251 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2252 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2253 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2254 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2255}
2256
2257TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2258 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2259 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2260 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2261 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2262
2263 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2264 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2265 addConfigurationProperty("keyboard.orientationAware", "1");
2266 addMapperAndConfigure(mapper);
2267
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002268 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002269 ASSERT_NO_FATAL_FAILURE(
2270 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2271 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2272 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2273 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2274 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2275 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2276 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002277
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002278 clearViewports();
2279 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002280 ASSERT_NO_FATAL_FAILURE(
2281 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2282 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2283 AKEYCODE_DPAD_UP, DISPLAY_ID));
2284 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2285 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2286 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2287 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002289 clearViewports();
2290 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002291 ASSERT_NO_FATAL_FAILURE(
2292 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2293 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2294 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2295 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2296 AKEYCODE_DPAD_UP, DISPLAY_ID));
2297 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2298 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002299
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002300 clearViewports();
2301 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002302 ASSERT_NO_FATAL_FAILURE(
2303 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2304 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2305 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2306 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2307 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2308 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2309 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002310
2311 // Special case: if orientation changes while key is down, we still emit the same keycode
2312 // in the key up as we did in the key down.
2313 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002314 clearViewports();
2315 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002316 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2318 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2319 ASSERT_EQ(KEY_UP, args.scanCode);
2320 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2321
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002322 clearViewports();
2323 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002324 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2326 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2327 ASSERT_EQ(KEY_UP, args.scanCode);
2328 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2329}
2330
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002331TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2332 // If the keyboard is not orientation aware,
2333 // key events should not be associated with a specific display id
2334 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2335
2336 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2337 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2338 addMapperAndConfigure(mapper);
2339 NotifyKeyArgs args;
2340
2341 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002342 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002344 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2346 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2347
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002348 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002349 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002351 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2353 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2354}
2355
2356TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2357 // If the keyboard is orientation aware,
2358 // key events should be associated with the internal viewport
2359 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2360
2361 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2362 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2363 addConfigurationProperty("keyboard.orientationAware", "1");
2364 addMapperAndConfigure(mapper);
2365 NotifyKeyArgs args;
2366
2367 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2368 // ^--- already checked by the previous test
2369
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002370 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002371 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002372 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002374 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2376 ASSERT_EQ(DISPLAY_ID, args.displayId);
2377
2378 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002379 clearViewports();
2380 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002381 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002382 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002384 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2386 ASSERT_EQ(newDisplayId, args.displayId);
2387}
2388
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2390 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2391 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2392 addMapperAndConfigure(mapper);
2393
2394 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2395 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2396
2397 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2398 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2399}
2400
2401TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2402 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2403 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2404 addMapperAndConfigure(mapper);
2405
2406 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2407 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2408
2409 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2410 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2411}
2412
2413TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2414 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2415 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2416 addMapperAndConfigure(mapper);
2417
2418 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2419
2420 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2421 uint8_t flags[2] = { 0, 0 };
2422 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2423 ASSERT_TRUE(flags[0]);
2424 ASSERT_FALSE(flags[1]);
2425}
2426
2427TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2428 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2429 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2430 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2431 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2432 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2433 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2434
2435 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2436 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2437 addMapperAndConfigure(mapper);
2438
2439 // Initialization should have turned all of the lights off.
2440 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2441 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2442 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2443
2444 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002445 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2446 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002447 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2448 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2449 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2450 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2451
2452 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002453 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2454 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2456 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2457 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2458 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2459
2460 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002461 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2462 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2464 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2465 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2466 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2467
2468 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002469 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2470 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2472 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2473 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2474 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2475
2476 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002477 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2478 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002479 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2480 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2481 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2482 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2483
2484 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002485 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2486 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002487 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2488 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2489 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2490 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2491}
2492
Arthur Hung2c9a3342019-07-23 14:18:59 +08002493TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2494 // keyboard 1.
2495 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2496 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2497 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2498 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2499
2500 // keyboard 2.
2501 const std::string USB2 = "USB2";
2502 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2503 InputDeviceIdentifier identifier;
2504 identifier.name = "KEYBOARD2";
2505 identifier.location = USB2;
2506 std::unique_ptr<InputDevice> device2 =
2507 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2508 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2509 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2510 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2511 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2512 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2513 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2514
2515 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2516 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2517 addMapperAndConfigure(mapper);
2518
2519 KeyboardInputMapper* mapper2 = new KeyboardInputMapper(device2.get(), AINPUT_SOURCE_KEYBOARD,
2520 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2521 device2->addMapper(mapper2);
2522 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2523 device2->reset(ARBITRARY_TIME);
2524
2525 // Prepared displays and associated info.
2526 constexpr uint8_t hdmi1 = 0;
2527 constexpr uint8_t hdmi2 = 1;
2528 const std::string SECONDARY_UNIQUE_ID = "local:1";
2529
2530 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2531 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2532
2533 // No associated display viewport found, should disable the device.
2534 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2535 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2536 ASSERT_FALSE(device2->isEnabled());
2537
2538 // Prepare second display.
2539 constexpr int32_t newDisplayId = 2;
2540 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2541 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2542 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2543 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2544 // Default device will reconfigure above, need additional reconfiguration for another device.
2545 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2546 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2547
2548 // Device should be enabled after the associated display is found.
2549 ASSERT_TRUE(mDevice->isEnabled());
2550 ASSERT_TRUE(device2->isEnabled());
2551
2552 // Test pad key events
2553 ASSERT_NO_FATAL_FAILURE(
2554 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2555 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2556 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2557 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2558 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2559 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2560 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2561
2562 ASSERT_NO_FATAL_FAILURE(
2563 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2564 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2565 AKEYCODE_DPAD_RIGHT, newDisplayId));
2566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2567 AKEYCODE_DPAD_DOWN, newDisplayId));
2568 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2569 AKEYCODE_DPAD_LEFT, newDisplayId));
2570}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002571
2572// --- CursorInputMapperTest ---
2573
2574class CursorInputMapperTest : public InputMapperTest {
2575protected:
2576 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2577
2578 sp<FakePointerController> mFakePointerController;
2579
Prabir Pradhanba266f22019-09-03 17:11:27 -07002580 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002581 InputMapperTest::SetUp();
2582
2583 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002584 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002585 }
2586
2587 void testMotionRotation(CursorInputMapper* mapper,
2588 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002589
2590 void prepareDisplay(int32_t orientation) {
2591 const std::string uniqueId = "local:0";
2592 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2593 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2594 orientation, uniqueId, NO_PORT, viewportType);
2595 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002596};
2597
2598const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2599
2600void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2601 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2602 NotifyMotionArgs args;
2603
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002604 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2605 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2606 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2610 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2611 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2612 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2613}
2614
2615TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2616 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2617 addConfigurationProperty("cursor.mode", "pointer");
2618 addMapperAndConfigure(mapper);
2619
2620 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2621}
2622
2623TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2624 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2625 addConfigurationProperty("cursor.mode", "navigation");
2626 addMapperAndConfigure(mapper);
2627
2628 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2629}
2630
2631TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2632 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2633 addConfigurationProperty("cursor.mode", "pointer");
2634 addMapperAndConfigure(mapper);
2635
2636 InputDeviceInfo info;
2637 mapper->populateDeviceInfo(&info);
2638
2639 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002640 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2641 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2643 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2644
2645 // When the bounds are set, then there should be a valid motion range.
2646 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2647
2648 InputDeviceInfo info2;
2649 mapper->populateDeviceInfo(&info2);
2650
2651 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2652 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2653 1, 800 - 1, 0.0f, 0.0f));
2654 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2655 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2656 2, 480 - 1, 0.0f, 0.0f));
2657 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2658 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2659 0.0f, 1.0f, 0.0f, 0.0f));
2660}
2661
2662TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2663 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2664 addConfigurationProperty("cursor.mode", "navigation");
2665 addMapperAndConfigure(mapper);
2666
2667 InputDeviceInfo info;
2668 mapper->populateDeviceInfo(&info);
2669
2670 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2671 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2672 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2673 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2674 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2675 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2676 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2677 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2678 0.0f, 1.0f, 0.0f, 0.0f));
2679}
2680
2681TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2682 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2683 addConfigurationProperty("cursor.mode", "navigation");
2684 addMapperAndConfigure(mapper);
2685
2686 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2687
2688 NotifyMotionArgs args;
2689
2690 // Button press.
2691 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002692 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2693 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2695 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2696 ASSERT_EQ(DEVICE_ID, args.deviceId);
2697 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2698 ASSERT_EQ(uint32_t(0), args.policyFlags);
2699 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2700 ASSERT_EQ(0, args.flags);
2701 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2702 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2703 ASSERT_EQ(0, args.edgeFlags);
2704 ASSERT_EQ(uint32_t(1), args.pointerCount);
2705 ASSERT_EQ(0, args.pointerProperties[0].id);
2706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2708 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2709 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2710 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2711 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2712
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2714 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2715 ASSERT_EQ(DEVICE_ID, args.deviceId);
2716 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2717 ASSERT_EQ(uint32_t(0), args.policyFlags);
2718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2719 ASSERT_EQ(0, args.flags);
2720 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2721 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2722 ASSERT_EQ(0, args.edgeFlags);
2723 ASSERT_EQ(uint32_t(1), args.pointerCount);
2724 ASSERT_EQ(0, args.pointerProperties[0].id);
2725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2727 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2728 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2729 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2730 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2731
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002733 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2734 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2736 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2737 ASSERT_EQ(DEVICE_ID, args.deviceId);
2738 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2739 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002740 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2741 ASSERT_EQ(0, args.flags);
2742 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2743 ASSERT_EQ(0, args.buttonState);
2744 ASSERT_EQ(0, args.edgeFlags);
2745 ASSERT_EQ(uint32_t(1), args.pointerCount);
2746 ASSERT_EQ(0, args.pointerProperties[0].id);
2747 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2749 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2750 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2751 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2752 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2753
2754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2755 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2756 ASSERT_EQ(DEVICE_ID, args.deviceId);
2757 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2758 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2760 ASSERT_EQ(0, args.flags);
2761 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2762 ASSERT_EQ(0, args.buttonState);
2763 ASSERT_EQ(0, args.edgeFlags);
2764 ASSERT_EQ(uint32_t(1), args.pointerCount);
2765 ASSERT_EQ(0, args.pointerProperties[0].id);
2766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2768 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2769 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2770 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2771 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2772}
2773
2774TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2775 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2776 addConfigurationProperty("cursor.mode", "navigation");
2777 addMapperAndConfigure(mapper);
2778
2779 NotifyMotionArgs args;
2780
2781 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002782 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2783 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2787 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2788
2789 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002790 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2791 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2795 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2796}
2797
2798TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2799 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2800 addConfigurationProperty("cursor.mode", "navigation");
2801 addMapperAndConfigure(mapper);
2802
2803 NotifyMotionArgs args;
2804
2805 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002806 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2807 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2811 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2812
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2814 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2816 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2817
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002819 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2820 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002822 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2824 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2825
2826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2829 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2830}
2831
2832TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2833 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2834 addConfigurationProperty("cursor.mode", "navigation");
2835 addMapperAndConfigure(mapper);
2836
2837 NotifyMotionArgs args;
2838
2839 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002840 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2841 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2842 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2843 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2845 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2847 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2848 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2849
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2851 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2853 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2854 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2855
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002857 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2858 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2859 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2863 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2864 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2865
2866 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002867 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2868 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002870 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2872 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2873
2874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002875 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2877 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2878}
2879
2880TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2881 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2882 addConfigurationProperty("cursor.mode", "navigation");
2883 addMapperAndConfigure(mapper);
2884
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002885 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002886 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2887 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2888 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2889 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2890 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2891 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2892 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2893 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2894}
2895
2896TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2897 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2898 addConfigurationProperty("cursor.mode", "navigation");
2899 addConfigurationProperty("cursor.orientationAware", "1");
2900 addMapperAndConfigure(mapper);
2901
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002902 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2904 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2905 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2906 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2907 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2908 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2909 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2910 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2911
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002912 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002913 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2914 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2915 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2916 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2917 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2918 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2919 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2920 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2921
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002922 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2924 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2925 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2926 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2927 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2928 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2929 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2930 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2931
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002932 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2934 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2935 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2936 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2937 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2938 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2939 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2940 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2941}
2942
2943TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2944 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2945 addConfigurationProperty("cursor.mode", "pointer");
2946 addMapperAndConfigure(mapper);
2947
2948 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2949 mFakePointerController->setPosition(100, 200);
2950 mFakePointerController->setButtonState(0);
2951
2952 NotifyMotionArgs motionArgs;
2953 NotifyKeyArgs keyArgs;
2954
2955 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002956 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2957 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2959 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2960 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2961 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2963 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2964
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2967 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2968 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2970 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2971
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002972 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2973 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002975 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002976 ASSERT_EQ(0, motionArgs.buttonState);
2977 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2979 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2980
2981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002982 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002983 ASSERT_EQ(0, motionArgs.buttonState);
2984 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2986 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2987
2988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002990 ASSERT_EQ(0, motionArgs.buttonState);
2991 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2993 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2994
2995 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002996 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
2997 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
2998 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3000 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3001 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3002 motionArgs.buttonState);
3003 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3004 mFakePointerController->getButtonState());
3005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3006 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3007
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3009 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3010 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3011 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3012 mFakePointerController->getButtonState());
3013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3014 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3015
3016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3017 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3018 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3019 motionArgs.buttonState);
3020 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3021 mFakePointerController->getButtonState());
3022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3023 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3024
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003025 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3026 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3030 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3032 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3033
3034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003036 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3037 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3039 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3040
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003041 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3042 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003044 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3045 ASSERT_EQ(0, motionArgs.buttonState);
3046 ASSERT_EQ(0, mFakePointerController->getButtonState());
3047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3048 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 -08003049 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3050 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003051
3052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 ASSERT_EQ(0, motionArgs.buttonState);
3054 ASSERT_EQ(0, mFakePointerController->getButtonState());
3055 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3057 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 -08003058
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3060 ASSERT_EQ(0, motionArgs.buttonState);
3061 ASSERT_EQ(0, mFakePointerController->getButtonState());
3062 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3064 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3065
3066 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003067 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3068 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3070 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3071 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003072
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003074 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3076 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3078 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3079
3080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3082 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3083 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3085 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3086
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003087 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3088 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091 ASSERT_EQ(0, motionArgs.buttonState);
3092 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3094 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3095
3096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003098 ASSERT_EQ(0, motionArgs.buttonState);
3099 ASSERT_EQ(0, mFakePointerController->getButtonState());
3100
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3102 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3104 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3105 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3106
3107 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003108 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3109 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3111 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3112 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003113
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003115 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3117 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3119 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3120
3121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3123 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3124 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3126 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3127
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003128 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3129 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003131 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132 ASSERT_EQ(0, motionArgs.buttonState);
3133 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3135 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 -08003136
3137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3138 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3139 ASSERT_EQ(0, motionArgs.buttonState);
3140 ASSERT_EQ(0, mFakePointerController->getButtonState());
3141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3142 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3143
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3145 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3146 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3147
3148 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003149 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3150 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3152 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3153 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003154
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003156 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3158 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3160 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3161
3162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3163 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3164 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3165 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3167 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3168
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003169 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3170 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173 ASSERT_EQ(0, motionArgs.buttonState);
3174 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3176 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 -08003177
3178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3179 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3180 ASSERT_EQ(0, motionArgs.buttonState);
3181 ASSERT_EQ(0, mFakePointerController->getButtonState());
3182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3183 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3184
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3186 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3187 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3188
3189 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003190 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3191 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3193 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3194 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003195
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3199 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3201 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3202
3203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3205 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3206 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3208 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3209
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003210 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3211 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214 ASSERT_EQ(0, motionArgs.buttonState);
3215 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3217 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 -08003218
3219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3220 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3221 ASSERT_EQ(0, motionArgs.buttonState);
3222 ASSERT_EQ(0, mFakePointerController->getButtonState());
3223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3224 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3225
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3227 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3228 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3229}
3230
3231TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3232 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3233 addConfigurationProperty("cursor.mode", "pointer");
3234 addMapperAndConfigure(mapper);
3235
3236 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3237 mFakePointerController->setPosition(100, 200);
3238 mFakePointerController->setButtonState(0);
3239
3240 NotifyMotionArgs args;
3241
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003242 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3243 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3244 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003246 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3247 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3249 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3250 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3251}
3252
3253TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3254 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3255 addConfigurationProperty("cursor.mode", "pointer");
3256 mFakePolicy->setPointerCapture(true);
3257 addMapperAndConfigure(mapper);
3258
3259 NotifyDeviceResetArgs resetArgs;
3260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3261 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3262 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3263
3264 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3265 mFakePointerController->setPosition(100, 200);
3266 mFakePointerController->setButtonState(0);
3267
3268 NotifyMotionArgs args;
3269
3270 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003271 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3272 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3273 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3275 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3278 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3279 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3280
3281 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003282 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3283 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3285 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3286 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3288 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3290 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3291 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3293 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3294
3295 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003296 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3297 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3299 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3302 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3304 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3305 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3307 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3308
3309 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003310 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3311 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3312 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3314 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3317 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3318 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3319
3320 // Disable pointer capture and check that the device generation got bumped
3321 // and events are generated the usual way.
3322 const uint32_t generation = mFakeContext->getGeneration();
3323 mFakePolicy->setPointerCapture(false);
3324 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3325 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3326
3327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3328 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3329 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3330
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003331 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3332 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3333 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3335 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3338 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3339 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3340}
3341
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003342TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3343 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3344 addMapperAndConfigure(mapper);
3345
3346 // Setup PointerController for second display.
3347 constexpr int32_t SECOND_DISPLAY_ID = 1;
3348 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3349 mFakePointerController->setPosition(100, 200);
3350 mFakePointerController->setButtonState(0);
3351 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3352
3353 NotifyMotionArgs args;
3354 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3355 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3356 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3358 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3359 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3361 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3362 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3363 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3364}
3365
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366
3367// --- TouchInputMapperTest ---
3368
3369class TouchInputMapperTest : public InputMapperTest {
3370protected:
3371 static const int32_t RAW_X_MIN;
3372 static const int32_t RAW_X_MAX;
3373 static const int32_t RAW_Y_MIN;
3374 static const int32_t RAW_Y_MAX;
3375 static const int32_t RAW_TOUCH_MIN;
3376 static const int32_t RAW_TOUCH_MAX;
3377 static const int32_t RAW_TOOL_MIN;
3378 static const int32_t RAW_TOOL_MAX;
3379 static const int32_t RAW_PRESSURE_MIN;
3380 static const int32_t RAW_PRESSURE_MAX;
3381 static const int32_t RAW_ORIENTATION_MIN;
3382 static const int32_t RAW_ORIENTATION_MAX;
3383 static const int32_t RAW_DISTANCE_MIN;
3384 static const int32_t RAW_DISTANCE_MAX;
3385 static const int32_t RAW_TILT_MIN;
3386 static const int32_t RAW_TILT_MAX;
3387 static const int32_t RAW_ID_MIN;
3388 static const int32_t RAW_ID_MAX;
3389 static const int32_t RAW_SLOT_MIN;
3390 static const int32_t RAW_SLOT_MAX;
3391 static const float X_PRECISION;
3392 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003393 static const float X_PRECISION_VIRTUAL;
3394 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395
3396 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003397 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398
3399 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3400
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003401 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003402 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003403
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404 enum Axes {
3405 POSITION = 1 << 0,
3406 TOUCH = 1 << 1,
3407 TOOL = 1 << 2,
3408 PRESSURE = 1 << 3,
3409 ORIENTATION = 1 << 4,
3410 MINOR = 1 << 5,
3411 ID = 1 << 6,
3412 DISTANCE = 1 << 7,
3413 TILT = 1 << 8,
3414 SLOT = 1 << 9,
3415 TOOL_TYPE = 1 << 10,
3416 };
3417
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003418 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3419 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003420 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003422 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423 int32_t toRawX(float displayX);
3424 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003425 float toCookedX(float rawX, float rawY);
3426 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003428 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003430 float toDisplayY(int32_t rawY, int32_t displayHeight);
3431
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432};
3433
3434const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3435const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3436const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3437const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3438const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3439const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3440const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3441const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003442const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3443const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3445const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3446const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3447const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3448const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3449const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3450const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3451const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3452const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3453const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3454const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3455const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003456const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3457 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3458const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3459 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003460const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3461 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462
3463const float TouchInputMapperTest::GEOMETRIC_SCALE =
3464 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3465 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3466
3467const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3468 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3469 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3470};
3471
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003472void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003473 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003474 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3475}
3476
3477void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3478 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3479 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480}
3481
Santos Cordonfa5cf462017-04-05 10:37:00 -07003482void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003483 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3484 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003485 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003486}
3487
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488void TouchInputMapperTest::prepareVirtualKeys() {
3489 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3490 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3491 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3492 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3493}
3494
Jason Gerecke489fda82012-09-07 17:19:40 -07003495void TouchInputMapperTest::prepareLocationCalibration() {
3496 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3497}
3498
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499int32_t TouchInputMapperTest::toRawX(float displayX) {
3500 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3501}
3502
3503int32_t TouchInputMapperTest::toRawY(float displayY) {
3504 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3505}
3506
Jason Gerecke489fda82012-09-07 17:19:40 -07003507float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3508 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3509 return rawX;
3510}
3511
3512float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3513 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3514 return rawY;
3515}
3516
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003518 return toDisplayX(rawX, DISPLAY_WIDTH);
3519}
3520
3521float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3522 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523}
3524
3525float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003526 return toDisplayY(rawY, DISPLAY_HEIGHT);
3527}
3528
3529float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3530 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531}
3532
3533
3534// --- SingleTouchInputMapperTest ---
3535
3536class SingleTouchInputMapperTest : public TouchInputMapperTest {
3537protected:
3538 void prepareButtons();
3539 void prepareAxes(int axes);
3540
3541 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3542 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3543 void processUp(SingleTouchInputMapper* mappery);
3544 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3545 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3546 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3547 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3548 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3549 void processSync(SingleTouchInputMapper* mapper);
3550};
3551
3552void SingleTouchInputMapperTest::prepareButtons() {
3553 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3554}
3555
3556void SingleTouchInputMapperTest::prepareAxes(int axes) {
3557 if (axes & POSITION) {
3558 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3559 RAW_X_MIN, RAW_X_MAX, 0, 0);
3560 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3561 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3562 }
3563 if (axes & PRESSURE) {
3564 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3565 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3566 }
3567 if (axes & TOOL) {
3568 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3569 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3570 }
3571 if (axes & DISTANCE) {
3572 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3573 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3574 }
3575 if (axes & TILT) {
3576 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3577 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3578 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3579 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3580 }
3581}
3582
3583void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003584 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3585 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3586 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587}
3588
3589void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003590 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3591 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003592}
3593
3594void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003595 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003596}
3597
3598void SingleTouchInputMapperTest::processPressure(
3599 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003600 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003601}
3602
3603void SingleTouchInputMapperTest::processToolMajor(
3604 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003605 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606}
3607
3608void SingleTouchInputMapperTest::processDistance(
3609 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003610 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611}
3612
3613void SingleTouchInputMapperTest::processTilt(
3614 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003615 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3616 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617}
3618
3619void SingleTouchInputMapperTest::processKey(
3620 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003621 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622}
3623
3624void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003625 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626}
3627
3628
3629TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3630 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3631 prepareButtons();
3632 prepareAxes(POSITION);
3633 addMapperAndConfigure(mapper);
3634
3635 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3636}
3637
3638TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3639 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3640 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3641 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3642 prepareButtons();
3643 prepareAxes(POSITION);
3644 addMapperAndConfigure(mapper);
3645
3646 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3647}
3648
3649TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3650 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3651 prepareButtons();
3652 prepareAxes(POSITION);
3653 addConfigurationProperty("touch.deviceType", "touchPad");
3654 addMapperAndConfigure(mapper);
3655
3656 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3657}
3658
3659TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3660 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3661 prepareButtons();
3662 prepareAxes(POSITION);
3663 addConfigurationProperty("touch.deviceType", "touchScreen");
3664 addMapperAndConfigure(mapper);
3665
3666 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3667}
3668
3669TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3670 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3671 addConfigurationProperty("touch.deviceType", "touchScreen");
3672 prepareDisplay(DISPLAY_ORIENTATION_0);
3673 prepareButtons();
3674 prepareAxes(POSITION);
3675 prepareVirtualKeys();
3676 addMapperAndConfigure(mapper);
3677
3678 // Unknown key.
3679 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3680
3681 // Virtual key is down.
3682 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3683 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3684 processDown(mapper, x, y);
3685 processSync(mapper);
3686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3687
3688 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3689
3690 // Virtual key is up.
3691 processUp(mapper);
3692 processSync(mapper);
3693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3694
3695 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3696}
3697
3698TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3699 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3700 addConfigurationProperty("touch.deviceType", "touchScreen");
3701 prepareDisplay(DISPLAY_ORIENTATION_0);
3702 prepareButtons();
3703 prepareAxes(POSITION);
3704 prepareVirtualKeys();
3705 addMapperAndConfigure(mapper);
3706
3707 // Unknown key.
3708 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3709
3710 // Virtual key is down.
3711 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3712 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3713 processDown(mapper, x, y);
3714 processSync(mapper);
3715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3716
3717 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3718
3719 // Virtual key is up.
3720 processUp(mapper);
3721 processSync(mapper);
3722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3723
3724 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3725}
3726
3727TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3728 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3729 addConfigurationProperty("touch.deviceType", "touchScreen");
3730 prepareDisplay(DISPLAY_ORIENTATION_0);
3731 prepareButtons();
3732 prepareAxes(POSITION);
3733 prepareVirtualKeys();
3734 addMapperAndConfigure(mapper);
3735
3736 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3737 uint8_t flags[2] = { 0, 0 };
3738 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3739 ASSERT_TRUE(flags[0]);
3740 ASSERT_FALSE(flags[1]);
3741}
3742
3743TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3744 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3745 addConfigurationProperty("touch.deviceType", "touchScreen");
3746 prepareDisplay(DISPLAY_ORIENTATION_0);
3747 prepareButtons();
3748 prepareAxes(POSITION);
3749 prepareVirtualKeys();
3750 addMapperAndConfigure(mapper);
3751
3752 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3753
3754 NotifyKeyArgs args;
3755
3756 // Press virtual key.
3757 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3758 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3759 processDown(mapper, x, y);
3760 processSync(mapper);
3761
3762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3763 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3764 ASSERT_EQ(DEVICE_ID, args.deviceId);
3765 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3766 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3767 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3768 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3769 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3770 ASSERT_EQ(KEY_HOME, args.scanCode);
3771 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3772 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3773
3774 // Release virtual key.
3775 processUp(mapper);
3776 processSync(mapper);
3777
3778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3779 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3780 ASSERT_EQ(DEVICE_ID, args.deviceId);
3781 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3782 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3783 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3784 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3785 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3786 ASSERT_EQ(KEY_HOME, args.scanCode);
3787 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3788 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3789
3790 // Should not have sent any motions.
3791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3792}
3793
3794TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3795 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3796 addConfigurationProperty("touch.deviceType", "touchScreen");
3797 prepareDisplay(DISPLAY_ORIENTATION_0);
3798 prepareButtons();
3799 prepareAxes(POSITION);
3800 prepareVirtualKeys();
3801 addMapperAndConfigure(mapper);
3802
3803 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3804
3805 NotifyKeyArgs keyArgs;
3806
3807 // Press virtual key.
3808 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3809 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3810 processDown(mapper, x, y);
3811 processSync(mapper);
3812
3813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3814 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3815 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3816 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3817 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3818 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3819 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3820 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3821 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3822 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3823 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3824
3825 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3826 // into the display area.
3827 y -= 100;
3828 processMove(mapper, x, y);
3829 processSync(mapper);
3830
3831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3832 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3833 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3834 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3835 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3836 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3837 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3838 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3839 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3840 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3841 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3842 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3843
3844 NotifyMotionArgs motionArgs;
3845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3846 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3847 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3848 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3849 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3850 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3851 ASSERT_EQ(0, motionArgs.flags);
3852 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3853 ASSERT_EQ(0, motionArgs.buttonState);
3854 ASSERT_EQ(0, motionArgs.edgeFlags);
3855 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3856 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3859 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3860 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3861 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3862 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3863
3864 // Keep moving out of bounds. Should generate a pointer move.
3865 y -= 50;
3866 processMove(mapper, x, y);
3867 processSync(mapper);
3868
3869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3870 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3871 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3872 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3873 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3875 ASSERT_EQ(0, motionArgs.flags);
3876 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3877 ASSERT_EQ(0, motionArgs.buttonState);
3878 ASSERT_EQ(0, motionArgs.edgeFlags);
3879 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3880 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3881 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3883 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3884 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3885 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3886 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3887
3888 // Release out of bounds. Should generate a pointer up.
3889 processUp(mapper);
3890 processSync(mapper);
3891
3892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3893 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3894 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3895 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3896 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3897 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3898 ASSERT_EQ(0, motionArgs.flags);
3899 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3900 ASSERT_EQ(0, motionArgs.buttonState);
3901 ASSERT_EQ(0, motionArgs.edgeFlags);
3902 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3903 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3906 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3907 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3908 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3909 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3910
3911 // Should not have sent any more keys or motions.
3912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3914}
3915
3916TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3917 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3918 addConfigurationProperty("touch.deviceType", "touchScreen");
3919 prepareDisplay(DISPLAY_ORIENTATION_0);
3920 prepareButtons();
3921 prepareAxes(POSITION);
3922 prepareVirtualKeys();
3923 addMapperAndConfigure(mapper);
3924
3925 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3926
3927 NotifyMotionArgs motionArgs;
3928
3929 // Initially go down out of bounds.
3930 int32_t x = -10;
3931 int32_t y = -10;
3932 processDown(mapper, x, y);
3933 processSync(mapper);
3934
3935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3936
3937 // Move into the display area. Should generate a pointer down.
3938 x = 50;
3939 y = 75;
3940 processMove(mapper, x, y);
3941 processSync(mapper);
3942
3943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3944 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3945 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3946 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3947 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3948 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3949 ASSERT_EQ(0, motionArgs.flags);
3950 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3951 ASSERT_EQ(0, motionArgs.buttonState);
3952 ASSERT_EQ(0, motionArgs.edgeFlags);
3953 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3954 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3955 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3957 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3958 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3959 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3960 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3961
3962 // Release. Should generate a pointer up.
3963 processUp(mapper);
3964 processSync(mapper);
3965
3966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3967 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3968 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3969 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3970 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3971 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3972 ASSERT_EQ(0, motionArgs.flags);
3973 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3974 ASSERT_EQ(0, motionArgs.buttonState);
3975 ASSERT_EQ(0, motionArgs.edgeFlags);
3976 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3977 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3980 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3981 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3982 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3983 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3984
3985 // Should not have sent any more keys or motions.
3986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3988}
3989
Santos Cordonfa5cf462017-04-05 10:37:00 -07003990TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3991 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3992 addConfigurationProperty("touch.deviceType", "touchScreen");
3993 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3994
3995 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
3996 prepareButtons();
3997 prepareAxes(POSITION);
3998 prepareVirtualKeys();
3999 addMapperAndConfigure(mapper);
4000
4001 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4002
4003 NotifyMotionArgs motionArgs;
4004
4005 // Down.
4006 int32_t x = 100;
4007 int32_t y = 125;
4008 processDown(mapper, x, y);
4009 processSync(mapper);
4010
4011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4012 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4013 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4014 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4015 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4016 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4017 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4018 ASSERT_EQ(0, motionArgs.flags);
4019 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4020 ASSERT_EQ(0, motionArgs.buttonState);
4021 ASSERT_EQ(0, motionArgs.edgeFlags);
4022 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4023 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4024 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4026 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4027 1, 0, 0, 0, 0, 0, 0, 0));
4028 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4029 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4030 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4031
4032 // Move.
4033 x += 50;
4034 y += 75;
4035 processMove(mapper, x, y);
4036 processSync(mapper);
4037
4038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4039 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4040 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4041 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4042 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4043 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4045 ASSERT_EQ(0, motionArgs.flags);
4046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4047 ASSERT_EQ(0, motionArgs.buttonState);
4048 ASSERT_EQ(0, motionArgs.edgeFlags);
4049 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4050 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4053 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4054 1, 0, 0, 0, 0, 0, 0, 0));
4055 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4056 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4057 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4058
4059 // Up.
4060 processUp(mapper);
4061 processSync(mapper);
4062
4063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4064 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4065 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4066 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4067 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4068 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4069 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4070 ASSERT_EQ(0, motionArgs.flags);
4071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4072 ASSERT_EQ(0, motionArgs.buttonState);
4073 ASSERT_EQ(0, motionArgs.edgeFlags);
4074 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4075 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4078 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4079 1, 0, 0, 0, 0, 0, 0, 0));
4080 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4081 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4082 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4083
4084 // Should not have sent any more keys or motions.
4085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4087}
4088
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
4090 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4091 addConfigurationProperty("touch.deviceType", "touchScreen");
4092 prepareDisplay(DISPLAY_ORIENTATION_0);
4093 prepareButtons();
4094 prepareAxes(POSITION);
4095 prepareVirtualKeys();
4096 addMapperAndConfigure(mapper);
4097
4098 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4099
4100 NotifyMotionArgs motionArgs;
4101
4102 // Down.
4103 int32_t x = 100;
4104 int32_t y = 125;
4105 processDown(mapper, x, y);
4106 processSync(mapper);
4107
4108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4109 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4110 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4111 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4112 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4113 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4114 ASSERT_EQ(0, motionArgs.flags);
4115 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4116 ASSERT_EQ(0, motionArgs.buttonState);
4117 ASSERT_EQ(0, motionArgs.edgeFlags);
4118 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4119 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4122 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4123 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4124 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4125 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4126
4127 // Move.
4128 x += 50;
4129 y += 75;
4130 processMove(mapper, x, y);
4131 processSync(mapper);
4132
4133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4134 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4135 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4136 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4137 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4139 ASSERT_EQ(0, motionArgs.flags);
4140 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4141 ASSERT_EQ(0, motionArgs.buttonState);
4142 ASSERT_EQ(0, motionArgs.edgeFlags);
4143 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4144 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4147 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4148 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4149 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4150 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4151
4152 // Up.
4153 processUp(mapper);
4154 processSync(mapper);
4155
4156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4157 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4158 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4159 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4160 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4161 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4162 ASSERT_EQ(0, motionArgs.flags);
4163 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4164 ASSERT_EQ(0, motionArgs.buttonState);
4165 ASSERT_EQ(0, motionArgs.edgeFlags);
4166 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4167 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4170 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4171 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4172 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4173 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4174
4175 // Should not have sent any more keys or motions.
4176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4178}
4179
4180TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
4181 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4182 addConfigurationProperty("touch.deviceType", "touchScreen");
4183 prepareButtons();
4184 prepareAxes(POSITION);
4185 addConfigurationProperty("touch.orientationAware", "0");
4186 addMapperAndConfigure(mapper);
4187
4188 NotifyMotionArgs args;
4189
4190 // Rotation 90.
4191 prepareDisplay(DISPLAY_ORIENTATION_90);
4192 processDown(mapper, toRawX(50), toRawY(75));
4193 processSync(mapper);
4194
4195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4196 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4197 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4198
4199 processUp(mapper);
4200 processSync(mapper);
4201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4202}
4203
4204TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4205 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4206 addConfigurationProperty("touch.deviceType", "touchScreen");
4207 prepareButtons();
4208 prepareAxes(POSITION);
4209 addMapperAndConfigure(mapper);
4210
4211 NotifyMotionArgs args;
4212
4213 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004214 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 prepareDisplay(DISPLAY_ORIENTATION_0);
4216 processDown(mapper, toRawX(50), toRawY(75));
4217 processSync(mapper);
4218
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4220 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4221 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4222
4223 processUp(mapper);
4224 processSync(mapper);
4225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4226
4227 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004228 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 prepareDisplay(DISPLAY_ORIENTATION_90);
4230 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4231 processSync(mapper);
4232
4233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4234 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4235 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4236
4237 processUp(mapper);
4238 processSync(mapper);
4239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4240
4241 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004242 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 prepareDisplay(DISPLAY_ORIENTATION_180);
4244 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4245 processSync(mapper);
4246
4247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4248 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4249 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4250
4251 processUp(mapper);
4252 processSync(mapper);
4253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4254
4255 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004256 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 prepareDisplay(DISPLAY_ORIENTATION_270);
4258 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4259 processSync(mapper);
4260
4261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4262 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4263 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4264
4265 processUp(mapper);
4266 processSync(mapper);
4267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4268}
4269
4270TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4271 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4272 addConfigurationProperty("touch.deviceType", "touchScreen");
4273 prepareDisplay(DISPLAY_ORIENTATION_0);
4274 prepareButtons();
4275 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4276 addMapperAndConfigure(mapper);
4277
4278 // These calculations are based on the input device calibration documentation.
4279 int32_t rawX = 100;
4280 int32_t rawY = 200;
4281 int32_t rawPressure = 10;
4282 int32_t rawToolMajor = 12;
4283 int32_t rawDistance = 2;
4284 int32_t rawTiltX = 30;
4285 int32_t rawTiltY = 110;
4286
4287 float x = toDisplayX(rawX);
4288 float y = toDisplayY(rawY);
4289 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4290 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4291 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4292 float distance = float(rawDistance);
4293
4294 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4295 float tiltScale = M_PI / 180;
4296 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4297 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4298 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4299 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4300
4301 processDown(mapper, rawX, rawY);
4302 processPressure(mapper, rawPressure);
4303 processToolMajor(mapper, rawToolMajor);
4304 processDistance(mapper, rawDistance);
4305 processTilt(mapper, rawTiltX, rawTiltY);
4306 processSync(mapper);
4307
4308 NotifyMotionArgs args;
4309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4311 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4312 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4313}
4314
Jason Gerecke489fda82012-09-07 17:19:40 -07004315TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4316 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4317 addConfigurationProperty("touch.deviceType", "touchScreen");
4318 prepareDisplay(DISPLAY_ORIENTATION_0);
4319 prepareLocationCalibration();
4320 prepareButtons();
4321 prepareAxes(POSITION);
4322 addMapperAndConfigure(mapper);
4323
4324 int32_t rawX = 100;
4325 int32_t rawY = 200;
4326
4327 float x = toDisplayX(toCookedX(rawX, rawY));
4328 float y = toDisplayY(toCookedY(rawX, rawY));
4329
4330 processDown(mapper, rawX, rawY);
4331 processSync(mapper);
4332
4333 NotifyMotionArgs args;
4334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4336 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4337}
4338
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4340 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4341 addConfigurationProperty("touch.deviceType", "touchScreen");
4342 prepareDisplay(DISPLAY_ORIENTATION_0);
4343 prepareButtons();
4344 prepareAxes(POSITION);
4345 addMapperAndConfigure(mapper);
4346
4347 NotifyMotionArgs motionArgs;
4348 NotifyKeyArgs keyArgs;
4349
4350 processDown(mapper, 100, 200);
4351 processSync(mapper);
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4353 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4354 ASSERT_EQ(0, motionArgs.buttonState);
4355
4356 // press BTN_LEFT, release BTN_LEFT
4357 processKey(mapper, BTN_LEFT, 1);
4358 processSync(mapper);
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4361 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4362
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4365 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4366
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 processKey(mapper, BTN_LEFT, 0);
4368 processSync(mapper);
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004372
4373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376
4377 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4378 processKey(mapper, BTN_RIGHT, 1);
4379 processKey(mapper, BTN_MIDDLE, 1);
4380 processSync(mapper);
4381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4383 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4384 motionArgs.buttonState);
4385
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4388 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4389
4390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4392 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4393 motionArgs.buttonState);
4394
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395 processKey(mapper, BTN_RIGHT, 0);
4396 processSync(mapper);
4397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004400
4401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004403 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404
4405 processKey(mapper, BTN_MIDDLE, 0);
4406 processSync(mapper);
4407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410
4411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004413 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414
4415 // press BTN_BACK, release BTN_BACK
4416 processKey(mapper, BTN_BACK, 1);
4417 processSync(mapper);
4418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4419 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4420 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004424 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4425
4426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4428 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429
4430 processKey(mapper, BTN_BACK, 0);
4431 processSync(mapper);
4432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435
4436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004438 ASSERT_EQ(0, motionArgs.buttonState);
4439
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4441 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4442 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4443
4444 // press BTN_SIDE, release BTN_SIDE
4445 processKey(mapper, BTN_SIDE, 1);
4446 processSync(mapper);
4447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4448 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4449 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004450
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4454
4455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4457 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458
4459 processKey(mapper, BTN_SIDE, 0);
4460 processSync(mapper);
4461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004464
4465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467 ASSERT_EQ(0, motionArgs.buttonState);
4468
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4470 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4471 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4472
4473 // press BTN_FORWARD, release BTN_FORWARD
4474 processKey(mapper, BTN_FORWARD, 1);
4475 processSync(mapper);
4476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4477 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4478 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004479
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4483
4484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4485 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4486 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487
4488 processKey(mapper, BTN_FORWARD, 0);
4489 processSync(mapper);
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493
4494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004496 ASSERT_EQ(0, motionArgs.buttonState);
4497
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4499 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4500 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4501
4502 // press BTN_EXTRA, release BTN_EXTRA
4503 processKey(mapper, BTN_EXTRA, 1);
4504 processSync(mapper);
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4506 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4507 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004508
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004511 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4512
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4515 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516
4517 processKey(mapper, BTN_EXTRA, 0);
4518 processSync(mapper);
4519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004522
4523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004525 ASSERT_EQ(0, motionArgs.buttonState);
4526
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4528 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4529 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4530
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4532
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533 // press BTN_STYLUS, release BTN_STYLUS
4534 processKey(mapper, BTN_STYLUS, 1);
4535 processSync(mapper);
4536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004538 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4539
4540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4542 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543
4544 processKey(mapper, BTN_STYLUS, 0);
4545 processSync(mapper);
4546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004547 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549
4550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004552 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553
4554 // press BTN_STYLUS2, release BTN_STYLUS2
4555 processKey(mapper, BTN_STYLUS2, 1);
4556 processSync(mapper);
4557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004559 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4560
4561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4562 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4563 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
4565 processKey(mapper, BTN_STYLUS2, 0);
4566 processSync(mapper);
4567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004568 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004570
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004573 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574
4575 // release touch
4576 processUp(mapper);
4577 processSync(mapper);
4578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4579 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4580 ASSERT_EQ(0, motionArgs.buttonState);
4581}
4582
4583TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4584 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4585 addConfigurationProperty("touch.deviceType", "touchScreen");
4586 prepareDisplay(DISPLAY_ORIENTATION_0);
4587 prepareButtons();
4588 prepareAxes(POSITION);
4589 addMapperAndConfigure(mapper);
4590
4591 NotifyMotionArgs motionArgs;
4592
4593 // default tool type is finger
4594 processDown(mapper, 100, 200);
4595 processSync(mapper);
4596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4597 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4599
4600 // eraser
4601 processKey(mapper, BTN_TOOL_RUBBER, 1);
4602 processSync(mapper);
4603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4606
4607 // stylus
4608 processKey(mapper, BTN_TOOL_RUBBER, 0);
4609 processKey(mapper, BTN_TOOL_PEN, 1);
4610 processSync(mapper);
4611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4614
4615 // brush
4616 processKey(mapper, BTN_TOOL_PEN, 0);
4617 processKey(mapper, BTN_TOOL_BRUSH, 1);
4618 processSync(mapper);
4619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4622
4623 // pencil
4624 processKey(mapper, BTN_TOOL_BRUSH, 0);
4625 processKey(mapper, BTN_TOOL_PENCIL, 1);
4626 processSync(mapper);
4627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4630
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004631 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 processKey(mapper, BTN_TOOL_PENCIL, 0);
4633 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4634 processSync(mapper);
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4638
4639 // mouse
4640 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4641 processKey(mapper, BTN_TOOL_MOUSE, 1);
4642 processSync(mapper);
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4646
4647 // lens
4648 processKey(mapper, BTN_TOOL_MOUSE, 0);
4649 processKey(mapper, BTN_TOOL_LENS, 1);
4650 processSync(mapper);
4651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4654
4655 // double-tap
4656 processKey(mapper, BTN_TOOL_LENS, 0);
4657 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4658 processSync(mapper);
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4662
4663 // triple-tap
4664 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4665 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4666 processSync(mapper);
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4670
4671 // quad-tap
4672 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4673 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4674 processSync(mapper);
4675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4678
4679 // finger
4680 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4681 processKey(mapper, BTN_TOOL_FINGER, 1);
4682 processSync(mapper);
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4686
4687 // stylus trumps finger
4688 processKey(mapper, BTN_TOOL_PEN, 1);
4689 processSync(mapper);
4690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4693
4694 // eraser trumps stylus
4695 processKey(mapper, BTN_TOOL_RUBBER, 1);
4696 processSync(mapper);
4697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4700
4701 // mouse trumps eraser
4702 processKey(mapper, BTN_TOOL_MOUSE, 1);
4703 processSync(mapper);
4704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4707
4708 // back to default tool type
4709 processKey(mapper, BTN_TOOL_MOUSE, 0);
4710 processKey(mapper, BTN_TOOL_RUBBER, 0);
4711 processKey(mapper, BTN_TOOL_PEN, 0);
4712 processKey(mapper, BTN_TOOL_FINGER, 0);
4713 processSync(mapper);
4714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4717}
4718
4719TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4720 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4721 addConfigurationProperty("touch.deviceType", "touchScreen");
4722 prepareDisplay(DISPLAY_ORIENTATION_0);
4723 prepareButtons();
4724 prepareAxes(POSITION);
4725 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4726 addMapperAndConfigure(mapper);
4727
4728 NotifyMotionArgs motionArgs;
4729
4730 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4731 processKey(mapper, BTN_TOOL_FINGER, 1);
4732 processMove(mapper, 100, 200);
4733 processSync(mapper);
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4737 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4738
4739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4742 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4743
4744 // move a little
4745 processMove(mapper, 150, 250);
4746 processSync(mapper);
4747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4748 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4750 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4751
4752 // down when BTN_TOUCH is pressed, pressure defaults to 1
4753 processKey(mapper, BTN_TOUCH, 1);
4754 processSync(mapper);
4755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4758 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4759
4760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4761 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4763 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4764
4765 // up when BTN_TOUCH is released, hover restored
4766 processKey(mapper, BTN_TOUCH, 0);
4767 processSync(mapper);
4768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4769 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4771 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4772
4773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4776 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4777
4778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4779 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4781 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4782
4783 // exit hover when pointer goes away
4784 processKey(mapper, BTN_TOOL_FINGER, 0);
4785 processSync(mapper);
4786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4789 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4790}
4791
4792TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4793 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4794 addConfigurationProperty("touch.deviceType", "touchScreen");
4795 prepareDisplay(DISPLAY_ORIENTATION_0);
4796 prepareButtons();
4797 prepareAxes(POSITION | PRESSURE);
4798 addMapperAndConfigure(mapper);
4799
4800 NotifyMotionArgs motionArgs;
4801
4802 // initially hovering because pressure is 0
4803 processDown(mapper, 100, 200);
4804 processPressure(mapper, 0);
4805 processSync(mapper);
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4807 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4809 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4810
4811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4812 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4814 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4815
4816 // move a little
4817 processMove(mapper, 150, 250);
4818 processSync(mapper);
4819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4820 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4822 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4823
4824 // down when pressure is non-zero
4825 processPressure(mapper, RAW_PRESSURE_MAX);
4826 processSync(mapper);
4827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4828 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4830 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4831
4832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4833 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4835 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4836
4837 // up when pressure becomes 0, hover restored
4838 processPressure(mapper, 0);
4839 processSync(mapper);
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4841 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4843 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4844
4845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4846 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4848 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4849
4850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4851 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4853 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4854
4855 // exit hover when pointer goes away
4856 processUp(mapper);
4857 processSync(mapper);
4858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4859 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4861 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4862}
4863
Dan Harmsaca28402018-12-17 13:55:20 -08004864
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865// --- MultiTouchInputMapperTest ---
4866
4867class MultiTouchInputMapperTest : public TouchInputMapperTest {
4868protected:
4869 void prepareAxes(int axes);
4870
4871 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4872 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4873 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4874 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4875 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4876 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4877 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4878 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4879 void processId(MultiTouchInputMapper* mapper, int32_t id);
4880 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4881 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4882 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4883 void processMTSync(MultiTouchInputMapper* mapper);
4884 void processSync(MultiTouchInputMapper* mapper);
4885};
4886
4887void MultiTouchInputMapperTest::prepareAxes(int axes) {
4888 if (axes & POSITION) {
4889 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4890 RAW_X_MIN, RAW_X_MAX, 0, 0);
4891 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4892 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4893 }
4894 if (axes & TOUCH) {
4895 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4896 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4897 if (axes & MINOR) {
4898 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4899 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4900 }
4901 }
4902 if (axes & TOOL) {
4903 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4904 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4905 if (axes & MINOR) {
4906 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4907 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4908 }
4909 }
4910 if (axes & ORIENTATION) {
4911 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4912 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4913 }
4914 if (axes & PRESSURE) {
4915 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4916 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4917 }
4918 if (axes & DISTANCE) {
4919 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4920 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4921 }
4922 if (axes & ID) {
4923 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4924 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4925 }
4926 if (axes & SLOT) {
4927 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4928 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4929 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4930 }
4931 if (axes & TOOL_TYPE) {
4932 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4933 0, MT_TOOL_MAX, 0, 0);
4934 }
4935}
4936
4937void MultiTouchInputMapperTest::processPosition(
4938 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004939 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4940 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941}
4942
4943void MultiTouchInputMapperTest::processTouchMajor(
4944 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004945 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946}
4947
4948void MultiTouchInputMapperTest::processTouchMinor(
4949 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004950 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951}
4952
4953void MultiTouchInputMapperTest::processToolMajor(
4954 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004955 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956}
4957
4958void MultiTouchInputMapperTest::processToolMinor(
4959 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004960 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961}
4962
4963void MultiTouchInputMapperTest::processOrientation(
4964 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004965 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966}
4967
4968void MultiTouchInputMapperTest::processPressure(
4969 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004970 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971}
4972
4973void MultiTouchInputMapperTest::processDistance(
4974 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004975 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976}
4977
4978void MultiTouchInputMapperTest::processId(
4979 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004980 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981}
4982
4983void MultiTouchInputMapperTest::processSlot(
4984 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004985 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986}
4987
4988void MultiTouchInputMapperTest::processToolType(
4989 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004990 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991}
4992
4993void MultiTouchInputMapperTest::processKey(
4994 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004995 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996}
4997
4998void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004999 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000}
5001
5002void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005003 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004}
5005
5006
5007TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
5008 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5009 addConfigurationProperty("touch.deviceType", "touchScreen");
5010 prepareDisplay(DISPLAY_ORIENTATION_0);
5011 prepareAxes(POSITION);
5012 prepareVirtualKeys();
5013 addMapperAndConfigure(mapper);
5014
5015 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5016
5017 NotifyMotionArgs motionArgs;
5018
5019 // Two fingers down at once.
5020 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5021 processPosition(mapper, x1, y1);
5022 processMTSync(mapper);
5023 processPosition(mapper, x2, y2);
5024 processMTSync(mapper);
5025 processSync(mapper);
5026
5027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5028 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5029 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5030 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5031 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5033 ASSERT_EQ(0, motionArgs.flags);
5034 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5035 ASSERT_EQ(0, motionArgs.buttonState);
5036 ASSERT_EQ(0, motionArgs.edgeFlags);
5037 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5038 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5039 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5041 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5042 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5043 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5044 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5045
5046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5047 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5048 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5049 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5050 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5051 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5052 motionArgs.action);
5053 ASSERT_EQ(0, motionArgs.flags);
5054 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5055 ASSERT_EQ(0, motionArgs.buttonState);
5056 ASSERT_EQ(0, motionArgs.edgeFlags);
5057 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5058 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5060 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5063 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5065 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5066 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5067 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5068 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5069
5070 // Move.
5071 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5072 processPosition(mapper, x1, y1);
5073 processMTSync(mapper);
5074 processPosition(mapper, x2, y2);
5075 processMTSync(mapper);
5076 processSync(mapper);
5077
5078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5080 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5081 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5082 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5084 ASSERT_EQ(0, motionArgs.flags);
5085 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5086 ASSERT_EQ(0, motionArgs.buttonState);
5087 ASSERT_EQ(0, motionArgs.edgeFlags);
5088 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5089 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5091 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5094 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5096 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5097 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5098 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5099 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5100
5101 // First finger up.
5102 x2 += 15; y2 -= 20;
5103 processPosition(mapper, x2, y2);
5104 processMTSync(mapper);
5105 processSync(mapper);
5106
5107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5108 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5109 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5110 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5111 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5112 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5113 motionArgs.action);
5114 ASSERT_EQ(0, motionArgs.flags);
5115 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5116 ASSERT_EQ(0, motionArgs.buttonState);
5117 ASSERT_EQ(0, motionArgs.edgeFlags);
5118 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5119 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5121 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5124 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5126 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5127 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5128 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5130
5131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5132 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5133 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5134 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5135 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5137 ASSERT_EQ(0, motionArgs.flags);
5138 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5139 ASSERT_EQ(0, motionArgs.buttonState);
5140 ASSERT_EQ(0, motionArgs.edgeFlags);
5141 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5142 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5145 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5146 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5147 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5148 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5149
5150 // Move.
5151 x2 += 20; y2 -= 25;
5152 processPosition(mapper, x2, y2);
5153 processMTSync(mapper);
5154 processSync(mapper);
5155
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5157 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5158 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5159 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5160 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5162 ASSERT_EQ(0, motionArgs.flags);
5163 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5164 ASSERT_EQ(0, motionArgs.buttonState);
5165 ASSERT_EQ(0, motionArgs.edgeFlags);
5166 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5167 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5170 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5171 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5172 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5173 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5174
5175 // New finger down.
5176 int32_t x3 = 700, y3 = 300;
5177 processPosition(mapper, x2, y2);
5178 processMTSync(mapper);
5179 processPosition(mapper, x3, y3);
5180 processMTSync(mapper);
5181 processSync(mapper);
5182
5183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5185 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5186 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5187 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5188 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5189 motionArgs.action);
5190 ASSERT_EQ(0, motionArgs.flags);
5191 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5192 ASSERT_EQ(0, motionArgs.buttonState);
5193 ASSERT_EQ(0, motionArgs.edgeFlags);
5194 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5195 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5197 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5200 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5202 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5203 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5204 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5205 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5206
5207 // Second finger up.
5208 x3 += 30; y3 -= 20;
5209 processPosition(mapper, x3, y3);
5210 processMTSync(mapper);
5211 processSync(mapper);
5212
5213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5214 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5215 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5216 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5217 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5218 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5219 motionArgs.action);
5220 ASSERT_EQ(0, motionArgs.flags);
5221 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5222 ASSERT_EQ(0, motionArgs.buttonState);
5223 ASSERT_EQ(0, motionArgs.edgeFlags);
5224 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5225 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5227 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5230 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5232 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5233 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5234 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5235 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5236
5237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5238 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5239 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5240 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5241 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5243 ASSERT_EQ(0, motionArgs.flags);
5244 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5245 ASSERT_EQ(0, motionArgs.buttonState);
5246 ASSERT_EQ(0, motionArgs.edgeFlags);
5247 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5248 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5249 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5251 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5252 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5253 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5254 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5255
5256 // Last finger up.
5257 processMTSync(mapper);
5258 processSync(mapper);
5259
5260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5261 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5262 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5263 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5264 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5265 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5266 ASSERT_EQ(0, motionArgs.flags);
5267 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5268 ASSERT_EQ(0, motionArgs.buttonState);
5269 ASSERT_EQ(0, motionArgs.edgeFlags);
5270 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5271 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5274 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5275 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5276 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5277 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5278
5279 // Should not have sent any more keys or motions.
5280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5282}
5283
5284TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5285 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5286 addConfigurationProperty("touch.deviceType", "touchScreen");
5287 prepareDisplay(DISPLAY_ORIENTATION_0);
5288 prepareAxes(POSITION | ID);
5289 prepareVirtualKeys();
5290 addMapperAndConfigure(mapper);
5291
5292 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5293
5294 NotifyMotionArgs motionArgs;
5295
5296 // Two fingers down at once.
5297 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5298 processPosition(mapper, x1, y1);
5299 processId(mapper, 1);
5300 processMTSync(mapper);
5301 processPosition(mapper, x2, y2);
5302 processId(mapper, 2);
5303 processMTSync(mapper);
5304 processSync(mapper);
5305
5306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5308 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5309 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5312 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5313
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5315 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5316 motionArgs.action);
5317 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5318 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5320 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5323 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5325 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5326
5327 // Move.
5328 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5329 processPosition(mapper, x1, y1);
5330 processId(mapper, 1);
5331 processMTSync(mapper);
5332 processPosition(mapper, x2, y2);
5333 processId(mapper, 2);
5334 processMTSync(mapper);
5335 processSync(mapper);
5336
5337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5339 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5342 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5345 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5347 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5348
5349 // First finger up.
5350 x2 += 15; y2 -= 20;
5351 processPosition(mapper, x2, y2);
5352 processId(mapper, 2);
5353 processMTSync(mapper);
5354 processSync(mapper);
5355
5356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5357 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5358 motionArgs.action);
5359 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5362 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5365 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5367 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5368
5369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5371 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5372 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5375 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5376
5377 // Move.
5378 x2 += 20; y2 -= 25;
5379 processPosition(mapper, x2, y2);
5380 processId(mapper, 2);
5381 processMTSync(mapper);
5382 processSync(mapper);
5383
5384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5386 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5387 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5390 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5391
5392 // New finger down.
5393 int32_t x3 = 700, y3 = 300;
5394 processPosition(mapper, x2, y2);
5395 processId(mapper, 2);
5396 processMTSync(mapper);
5397 processPosition(mapper, x3, y3);
5398 processId(mapper, 3);
5399 processMTSync(mapper);
5400 processSync(mapper);
5401
5402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5403 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5404 motionArgs.action);
5405 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5406 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5408 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5411 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5413 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5414
5415 // Second finger up.
5416 x3 += 30; y3 -= 20;
5417 processPosition(mapper, x3, y3);
5418 processId(mapper, 3);
5419 processMTSync(mapper);
5420 processSync(mapper);
5421
5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5423 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5424 motionArgs.action);
5425 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5426 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5428 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5431 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5433 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5434
5435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5437 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5438 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5441 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5442
5443 // Last finger up.
5444 processMTSync(mapper);
5445 processSync(mapper);
5446
5447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5448 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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 // Should not have sent any more keys or motions.
5456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5458}
5459
5460TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5461 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5462 addConfigurationProperty("touch.deviceType", "touchScreen");
5463 prepareDisplay(DISPLAY_ORIENTATION_0);
5464 prepareAxes(POSITION | ID | SLOT);
5465 prepareVirtualKeys();
5466 addMapperAndConfigure(mapper);
5467
5468 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5469
5470 NotifyMotionArgs motionArgs;
5471
5472 // Two fingers down at once.
5473 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5474 processPosition(mapper, x1, y1);
5475 processId(mapper, 1);
5476 processSlot(mapper, 1);
5477 processPosition(mapper, x2, y2);
5478 processId(mapper, 2);
5479 processSync(mapper);
5480
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5482 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5483 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5484 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5487 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5488
5489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5490 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5491 motionArgs.action);
5492 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5495 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5498 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5500 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5501
5502 // Move.
5503 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5504 processSlot(mapper, 0);
5505 processPosition(mapper, x1, y1);
5506 processSlot(mapper, 1);
5507 processPosition(mapper, x2, y2);
5508 processSync(mapper);
5509
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5512 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5513 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5515 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5518 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5520 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5521
5522 // First finger up.
5523 x2 += 15; y2 -= 20;
5524 processSlot(mapper, 0);
5525 processId(mapper, -1);
5526 processSlot(mapper, 1);
5527 processPosition(mapper, x2, y2);
5528 processSync(mapper);
5529
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5531 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5532 motionArgs.action);
5533 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5534 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5536 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5539 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5541 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5542
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5545 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5546 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5549 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5550
5551 // Move.
5552 x2 += 20; y2 -= 25;
5553 processPosition(mapper, x2, y2);
5554 processSync(mapper);
5555
5556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5558 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5559 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5562 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5563
5564 // New finger down.
5565 int32_t x3 = 700, y3 = 300;
5566 processPosition(mapper, x2, y2);
5567 processSlot(mapper, 0);
5568 processId(mapper, 3);
5569 processPosition(mapper, x3, y3);
5570 processSync(mapper);
5571
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5574 motionArgs.action);
5575 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5576 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5578 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5581 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5583 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5584
5585 // Second finger up.
5586 x3 += 30; y3 -= 20;
5587 processSlot(mapper, 1);
5588 processId(mapper, -1);
5589 processSlot(mapper, 0);
5590 processPosition(mapper, x3, y3);
5591 processSync(mapper);
5592
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5594 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5595 motionArgs.action);
5596 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5597 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5599 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5602 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5604 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5605
5606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5608 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5609 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5612 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5613
5614 // Last finger up.
5615 processId(mapper, -1);
5616 processSync(mapper);
5617
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5619 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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 // Should not have sent any more keys or motions.
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5629}
5630
5631TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5632 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5633 addConfigurationProperty("touch.deviceType", "touchScreen");
5634 prepareDisplay(DISPLAY_ORIENTATION_0);
5635 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5636 addMapperAndConfigure(mapper);
5637
5638 // These calculations are based on the input device calibration documentation.
5639 int32_t rawX = 100;
5640 int32_t rawY = 200;
5641 int32_t rawTouchMajor = 7;
5642 int32_t rawTouchMinor = 6;
5643 int32_t rawToolMajor = 9;
5644 int32_t rawToolMinor = 8;
5645 int32_t rawPressure = 11;
5646 int32_t rawDistance = 0;
5647 int32_t rawOrientation = 3;
5648 int32_t id = 5;
5649
5650 float x = toDisplayX(rawX);
5651 float y = toDisplayY(rawY);
5652 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5653 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5654 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5655 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5656 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5657 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5658 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5659 float distance = float(rawDistance);
5660
5661 processPosition(mapper, rawX, rawY);
5662 processTouchMajor(mapper, rawTouchMajor);
5663 processTouchMinor(mapper, rawTouchMinor);
5664 processToolMajor(mapper, rawToolMajor);
5665 processToolMinor(mapper, rawToolMinor);
5666 processPressure(mapper, rawPressure);
5667 processOrientation(mapper, rawOrientation);
5668 processDistance(mapper, rawDistance);
5669 processId(mapper, id);
5670 processMTSync(mapper);
5671 processSync(mapper);
5672
5673 NotifyMotionArgs args;
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5675 ASSERT_EQ(0, args.pointerProperties[0].id);
5676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5677 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5678 orientation, distance));
5679}
5680
5681TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5682 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5683 addConfigurationProperty("touch.deviceType", "touchScreen");
5684 prepareDisplay(DISPLAY_ORIENTATION_0);
5685 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5686 addConfigurationProperty("touch.size.calibration", "geometric");
5687 addMapperAndConfigure(mapper);
5688
5689 // These calculations are based on the input device calibration documentation.
5690 int32_t rawX = 100;
5691 int32_t rawY = 200;
5692 int32_t rawTouchMajor = 140;
5693 int32_t rawTouchMinor = 120;
5694 int32_t rawToolMajor = 180;
5695 int32_t rawToolMinor = 160;
5696
5697 float x = toDisplayX(rawX);
5698 float y = toDisplayY(rawY);
5699 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5700 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5701 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5702 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5703 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5704
5705 processPosition(mapper, rawX, rawY);
5706 processTouchMajor(mapper, rawTouchMajor);
5707 processTouchMinor(mapper, rawTouchMinor);
5708 processToolMajor(mapper, rawToolMajor);
5709 processToolMinor(mapper, rawToolMinor);
5710 processMTSync(mapper);
5711 processSync(mapper);
5712
5713 NotifyMotionArgs args;
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5716 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5717}
5718
5719TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5720 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5721 addConfigurationProperty("touch.deviceType", "touchScreen");
5722 prepareDisplay(DISPLAY_ORIENTATION_0);
5723 prepareAxes(POSITION | TOUCH | TOOL);
5724 addConfigurationProperty("touch.size.calibration", "diameter");
5725 addConfigurationProperty("touch.size.scale", "10");
5726 addConfigurationProperty("touch.size.bias", "160");
5727 addConfigurationProperty("touch.size.isSummed", "1");
5728 addMapperAndConfigure(mapper);
5729
5730 // These calculations are based on the input device calibration documentation.
5731 // Note: We only provide a single common touch/tool value because the device is assumed
5732 // not to emit separate values for each pointer (isSummed = 1).
5733 int32_t rawX = 100;
5734 int32_t rawY = 200;
5735 int32_t rawX2 = 150;
5736 int32_t rawY2 = 250;
5737 int32_t rawTouchMajor = 5;
5738 int32_t rawToolMajor = 8;
5739
5740 float x = toDisplayX(rawX);
5741 float y = toDisplayY(rawY);
5742 float x2 = toDisplayX(rawX2);
5743 float y2 = toDisplayY(rawY2);
5744 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5745 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5746 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5747
5748 processPosition(mapper, rawX, rawY);
5749 processTouchMajor(mapper, rawTouchMajor);
5750 processToolMajor(mapper, rawToolMajor);
5751 processMTSync(mapper);
5752 processPosition(mapper, rawX2, rawY2);
5753 processTouchMajor(mapper, rawTouchMajor);
5754 processToolMajor(mapper, rawToolMajor);
5755 processMTSync(mapper);
5756 processSync(mapper);
5757
5758 NotifyMotionArgs args;
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5760 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5761
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5763 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5764 args.action);
5765 ASSERT_EQ(size_t(2), args.pointerCount);
5766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5767 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5769 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5770}
5771
5772TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5773 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5774 addConfigurationProperty("touch.deviceType", "touchScreen");
5775 prepareDisplay(DISPLAY_ORIENTATION_0);
5776 prepareAxes(POSITION | TOUCH | TOOL);
5777 addConfigurationProperty("touch.size.calibration", "area");
5778 addConfigurationProperty("touch.size.scale", "43");
5779 addConfigurationProperty("touch.size.bias", "3");
5780 addMapperAndConfigure(mapper);
5781
5782 // These calculations are based on the input device calibration documentation.
5783 int32_t rawX = 100;
5784 int32_t rawY = 200;
5785 int32_t rawTouchMajor = 5;
5786 int32_t rawToolMajor = 8;
5787
5788 float x = toDisplayX(rawX);
5789 float y = toDisplayY(rawY);
5790 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5791 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5792 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5793
5794 processPosition(mapper, rawX, rawY);
5795 processTouchMajor(mapper, rawTouchMajor);
5796 processToolMajor(mapper, rawToolMajor);
5797 processMTSync(mapper);
5798 processSync(mapper);
5799
5800 NotifyMotionArgs args;
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5803 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5804}
5805
5806TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5807 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5808 addConfigurationProperty("touch.deviceType", "touchScreen");
5809 prepareDisplay(DISPLAY_ORIENTATION_0);
5810 prepareAxes(POSITION | PRESSURE);
5811 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5812 addConfigurationProperty("touch.pressure.scale", "0.01");
5813 addMapperAndConfigure(mapper);
5814
Michael Wrightaa449c92017-12-13 21:21:43 +00005815 InputDeviceInfo info;
5816 mapper->populateDeviceInfo(&info);
5817 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5818 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5819 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5820
Michael Wrightd02c5b62014-02-10 15:10:22 -08005821 // These calculations are based on the input device calibration documentation.
5822 int32_t rawX = 100;
5823 int32_t rawY = 200;
5824 int32_t rawPressure = 60;
5825
5826 float x = toDisplayX(rawX);
5827 float y = toDisplayY(rawY);
5828 float pressure = float(rawPressure) * 0.01f;
5829
5830 processPosition(mapper, rawX, rawY);
5831 processPressure(mapper, rawPressure);
5832 processMTSync(mapper);
5833 processSync(mapper);
5834
5835 NotifyMotionArgs args;
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5838 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5839}
5840
5841TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5842 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5843 addConfigurationProperty("touch.deviceType", "touchScreen");
5844 prepareDisplay(DISPLAY_ORIENTATION_0);
5845 prepareAxes(POSITION | ID | SLOT);
5846 addMapperAndConfigure(mapper);
5847
5848 NotifyMotionArgs motionArgs;
5849 NotifyKeyArgs keyArgs;
5850
5851 processId(mapper, 1);
5852 processPosition(mapper, 100, 200);
5853 processSync(mapper);
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5855 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5856 ASSERT_EQ(0, motionArgs.buttonState);
5857
5858 // press BTN_LEFT, release BTN_LEFT
5859 processKey(mapper, BTN_LEFT, 1);
5860 processSync(mapper);
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5863 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5864
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5867 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5868
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869 processKey(mapper, BTN_LEFT, 0);
5870 processSync(mapper);
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005872 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005873 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005874
5875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005877 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005878
5879 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5880 processKey(mapper, BTN_RIGHT, 1);
5881 processKey(mapper, BTN_MIDDLE, 1);
5882 processSync(mapper);
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5884 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5885 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5886 motionArgs.buttonState);
5887
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5890 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5891
5892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5894 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5895 motionArgs.buttonState);
5896
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897 processKey(mapper, BTN_RIGHT, 0);
5898 processSync(mapper);
5899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005900 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005901 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005902
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005905 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906
5907 processKey(mapper, BTN_MIDDLE, 0);
5908 processSync(mapper);
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005911 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005912
5913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005915 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916
5917 // press BTN_BACK, release BTN_BACK
5918 processKey(mapper, BTN_BACK, 1);
5919 processSync(mapper);
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5921 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5922 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005923
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005926 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5927
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5929 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5930 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005931
5932 processKey(mapper, BTN_BACK, 0);
5933 processSync(mapper);
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005937
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005940 ASSERT_EQ(0, motionArgs.buttonState);
5941
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5943 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5944 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5945
5946 // press BTN_SIDE, release BTN_SIDE
5947 processKey(mapper, BTN_SIDE, 1);
5948 processSync(mapper);
5949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5950 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5951 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005952
Michael Wrightd02c5b62014-02-10 15:10:22 -08005953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005955 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5956
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5959 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960
5961 processKey(mapper, BTN_SIDE, 0);
5962 processSync(mapper);
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005964 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005966
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005969 ASSERT_EQ(0, motionArgs.buttonState);
5970
Michael Wrightd02c5b62014-02-10 15:10:22 -08005971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5972 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5973 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5974
5975 // press BTN_FORWARD, release BTN_FORWARD
5976 processKey(mapper, BTN_FORWARD, 1);
5977 processSync(mapper);
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5979 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5980 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005981
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005984 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5985
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5988 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989
5990 processKey(mapper, BTN_FORWARD, 0);
5991 processSync(mapper);
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005993 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005995
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005998 ASSERT_EQ(0, motionArgs.buttonState);
5999
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6001 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6002 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6003
6004 // press BTN_EXTRA, release BTN_EXTRA
6005 processKey(mapper, BTN_EXTRA, 1);
6006 processSync(mapper);
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6008 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6009 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006010
Michael Wrightd02c5b62014-02-10 15:10:22 -08006011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006013 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6014
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6017 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006018
6019 processKey(mapper, BTN_EXTRA, 0);
6020 processSync(mapper);
6021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006022 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006024
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006027 ASSERT_EQ(0, motionArgs.buttonState);
6028
Michael Wrightd02c5b62014-02-10 15:10:22 -08006029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6030 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6031 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6032
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6034
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035 // press BTN_STYLUS, release BTN_STYLUS
6036 processKey(mapper, BTN_STYLUS, 1);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006040 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6041
6042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6043 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6044 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045
6046 processKey(mapper, BTN_STYLUS, 0);
6047 processSync(mapper);
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006051
6052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006054 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055
6056 // press BTN_STYLUS2, release BTN_STYLUS2
6057 processKey(mapper, BTN_STYLUS2, 1);
6058 processSync(mapper);
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006061 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6062
6063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6065 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066
6067 processKey(mapper, BTN_STYLUS2, 0);
6068 processSync(mapper);
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006070 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072
6073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006075 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076
6077 // release touch
6078 processId(mapper, -1);
6079 processSync(mapper);
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6081 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6082 ASSERT_EQ(0, motionArgs.buttonState);
6083}
6084
6085TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
6086 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6087 addConfigurationProperty("touch.deviceType", "touchScreen");
6088 prepareDisplay(DISPLAY_ORIENTATION_0);
6089 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6090 addMapperAndConfigure(mapper);
6091
6092 NotifyMotionArgs motionArgs;
6093
6094 // default tool type is finger
6095 processId(mapper, 1);
6096 processPosition(mapper, 100, 200);
6097 processSync(mapper);
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6099 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6101
6102 // eraser
6103 processKey(mapper, BTN_TOOL_RUBBER, 1);
6104 processSync(mapper);
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6108
6109 // stylus
6110 processKey(mapper, BTN_TOOL_RUBBER, 0);
6111 processKey(mapper, BTN_TOOL_PEN, 1);
6112 processSync(mapper);
6113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6116
6117 // brush
6118 processKey(mapper, BTN_TOOL_PEN, 0);
6119 processKey(mapper, BTN_TOOL_BRUSH, 1);
6120 processSync(mapper);
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6124
6125 // pencil
6126 processKey(mapper, BTN_TOOL_BRUSH, 0);
6127 processKey(mapper, BTN_TOOL_PENCIL, 1);
6128 processSync(mapper);
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6132
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006133 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 processKey(mapper, BTN_TOOL_PENCIL, 0);
6135 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6136 processSync(mapper);
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6140
6141 // mouse
6142 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6143 processKey(mapper, BTN_TOOL_MOUSE, 1);
6144 processSync(mapper);
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6148
6149 // lens
6150 processKey(mapper, BTN_TOOL_MOUSE, 0);
6151 processKey(mapper, BTN_TOOL_LENS, 1);
6152 processSync(mapper);
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6156
6157 // double-tap
6158 processKey(mapper, BTN_TOOL_LENS, 0);
6159 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6160 processSync(mapper);
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6164
6165 // triple-tap
6166 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6167 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6168 processSync(mapper);
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6172
6173 // quad-tap
6174 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6175 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6176 processSync(mapper);
6177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6180
6181 // finger
6182 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6183 processKey(mapper, BTN_TOOL_FINGER, 1);
6184 processSync(mapper);
6185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6187 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6188
6189 // stylus trumps finger
6190 processKey(mapper, BTN_TOOL_PEN, 1);
6191 processSync(mapper);
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6195
6196 // eraser trumps stylus
6197 processKey(mapper, BTN_TOOL_RUBBER, 1);
6198 processSync(mapper);
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6202
6203 // mouse trumps eraser
6204 processKey(mapper, BTN_TOOL_MOUSE, 1);
6205 processSync(mapper);
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6209
6210 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6211 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6212 processSync(mapper);
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6216
6217 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6218 processToolType(mapper, MT_TOOL_PEN);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6223
6224 // back to default tool type
6225 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6226 processKey(mapper, BTN_TOOL_MOUSE, 0);
6227 processKey(mapper, BTN_TOOL_RUBBER, 0);
6228 processKey(mapper, BTN_TOOL_PEN, 0);
6229 processKey(mapper, BTN_TOOL_FINGER, 0);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6234}
6235
6236TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6237 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6238 addConfigurationProperty("touch.deviceType", "touchScreen");
6239 prepareDisplay(DISPLAY_ORIENTATION_0);
6240 prepareAxes(POSITION | ID | SLOT);
6241 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6242 addMapperAndConfigure(mapper);
6243
6244 NotifyMotionArgs motionArgs;
6245
6246 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6247 processId(mapper, 1);
6248 processPosition(mapper, 100, 200);
6249 processSync(mapper);
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6251 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6253 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6254
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6256 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6258 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6259
6260 // move a little
6261 processPosition(mapper, 150, 250);
6262 processSync(mapper);
6263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6264 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6266 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6267
6268 // down when BTN_TOUCH is pressed, pressure defaults to 1
6269 processKey(mapper, BTN_TOUCH, 1);
6270 processSync(mapper);
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6272 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6274 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6275
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6279 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6280
6281 // up when BTN_TOUCH is released, hover restored
6282 processKey(mapper, BTN_TOUCH, 0);
6283 processSync(mapper);
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6285 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6287 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6288
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6292 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6293
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6297 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6298
6299 // exit hover when pointer goes away
6300 processId(mapper, -1);
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6305 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6306}
6307
6308TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6309 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6310 addConfigurationProperty("touch.deviceType", "touchScreen");
6311 prepareDisplay(DISPLAY_ORIENTATION_0);
6312 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6313 addMapperAndConfigure(mapper);
6314
6315 NotifyMotionArgs motionArgs;
6316
6317 // initially hovering because pressure is 0
6318 processId(mapper, 1);
6319 processPosition(mapper, 100, 200);
6320 processPressure(mapper, 0);
6321 processSync(mapper);
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6325 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6326
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6328 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6330 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6331
6332 // move a little
6333 processPosition(mapper, 150, 250);
6334 processSync(mapper);
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6338 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6339
6340 // down when pressure becomes non-zero
6341 processPressure(mapper, RAW_PRESSURE_MAX);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6344 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6346 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6347
6348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6349 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6351 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6352
6353 // up when pressure becomes 0, hover restored
6354 processPressure(mapper, 0);
6355 processSync(mapper);
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6357 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6359 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6360
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6362 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6364 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6365
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6367 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6369 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6370
6371 // exit hover when pointer goes away
6372 processId(mapper, -1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6377 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6378}
6379
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006380/**
6381 * Set the input device port <--> display port associations, and check that the
6382 * events are routed to the display that matches the display port.
6383 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6384 */
6385TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6386 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6387 const std::string usb2 = "USB2";
6388 const uint8_t hdmi1 = 0;
6389 const uint8_t hdmi2 = 1;
6390 const std::string secondaryUniqueId = "uniqueId2";
6391 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6392
6393 addConfigurationProperty("touch.deviceType", "touchScreen");
6394 prepareAxes(POSITION);
6395 addMapperAndConfigure(mapper);
6396
6397 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6398 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6399
6400 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6401 // for this input device is specified, and the matching viewport is not present,
6402 // the input device should be disabled (at the mapper level).
6403
6404 // Add viewport for display 2 on hdmi2
6405 prepareSecondaryDisplay(type, hdmi2);
6406 // Send a touch event
6407 processPosition(mapper, 100, 100);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6410
6411 // Add viewport for display 1 on hdmi1
6412 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6413 // Send a touch event again
6414 processPosition(mapper, 100, 100);
6415 processSync(mapper);
6416
6417 NotifyMotionArgs args;
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6419 ASSERT_EQ(DISPLAY_ID, args.displayId);
6420}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421
Arthur Hung41a712e2018-11-22 19:41:03 +08006422/**
6423 * Expect fallback to internal viewport if device is external and external viewport is not present.
6424 */
6425TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6426 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6427 prepareAxes(POSITION);
6428 addConfigurationProperty("touch.deviceType", "touchScreen");
6429 prepareDisplay(DISPLAY_ORIENTATION_0);
6430 mDevice->setExternal(true);
6431 addMapperAndConfigure(mapper);
6432
6433 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6434
6435 NotifyMotionArgs motionArgs;
6436
6437 // Expect the event to be sent to the internal viewport,
6438 // because an external viewport is not present.
6439 processPosition(mapper, 100, 100);
6440 processSync(mapper);
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6442 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6443
6444 // Expect the event to be sent to the external viewport if it is present.
6445 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6446 processPosition(mapper, 100, 100);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6450}
6451
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006452TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6453 // Setup PointerController for second display.
6454 sp<FakePointerController> fakePointerController = new FakePointerController();
6455 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6456 fakePointerController->setPosition(100, 200);
6457 fakePointerController->setButtonState(0);
6458 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6459 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6460
6461 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6462 prepareDisplay(DISPLAY_ORIENTATION_0);
6463 prepareAxes(POSITION);
6464 addMapperAndConfigure(mapper);
6465
6466 // Check source is mouse that would obtain the PointerController.
6467 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6468
6469 NotifyMotionArgs motionArgs;
6470 processPosition(mapper, 100, 100);
6471 processSync(mapper);
6472
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6475 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6476}
6477
Arthur Hung7c645402019-01-25 17:45:42 +08006478TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6479 // Setup the first touch screen device.
6480 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6481 prepareAxes(POSITION | ID | SLOT);
6482 addConfigurationProperty("touch.deviceType", "touchScreen");
6483 addMapperAndConfigure(mapper);
6484
6485 // Create the second touch screen device, and enable multi fingers.
6486 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006487 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006488 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006489 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006490 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006491 std::unique_ptr<InputDevice> device2 =
6492 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6493 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006494 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6495 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6496 0 /*flat*/, 0 /*fuzz*/);
6497 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6498 0 /*flat*/, 0 /*fuzz*/);
6499 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6500 0 /*flat*/, 0 /*fuzz*/);
6501 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6502 0 /*flat*/, 0 /*fuzz*/);
6503 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6504 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6505 String8("touchScreen"));
6506
6507 // Setup the second touch screen device.
Arthur Hung2c9a3342019-07-23 14:18:59 +08006508 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2.get());
Arthur Hung7c645402019-01-25 17:45:42 +08006509 device2->addMapper(mapper2);
6510 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6511 device2->reset(ARBITRARY_TIME);
6512
6513 // Setup PointerController.
6514 sp<FakePointerController> fakePointerController = new FakePointerController();
6515 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6516 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6517
6518 // Setup policy for associated displays and show touches.
6519 const uint8_t hdmi1 = 0;
6520 const uint8_t hdmi2 = 1;
6521 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6522 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6523 mFakePolicy->setShowTouches(true);
6524
6525 // Create displays.
6526 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6527 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6528
6529 // Default device will reconfigure above, need additional reconfiguration for another device.
6530 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6531 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6532
6533 // Two fingers down at default display.
6534 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6535 processPosition(mapper, x1, y1);
6536 processId(mapper, 1);
6537 processSlot(mapper, 1);
6538 processPosition(mapper, x2, y2);
6539 processId(mapper, 2);
6540 processSync(mapper);
6541
6542 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6543 fakePointerController->getSpots().find(DISPLAY_ID);
6544 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6545 ASSERT_EQ(size_t(2), iter->second.size());
6546
6547 // Two fingers down at second display.
6548 processPosition(mapper2, x1, y1);
6549 processId(mapper2, 1);
6550 processSlot(mapper2, 1);
6551 processPosition(mapper2, x2, y2);
6552 processId(mapper2, 2);
6553 processSync(mapper2);
6554
6555 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6556 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6557 ASSERT_EQ(size_t(2), iter->second.size());
6558}
6559
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006560TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6561 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6562 prepareAxes(POSITION);
6563 addConfigurationProperty("touch.deviceType", "touchScreen");
6564 prepareDisplay(DISPLAY_ORIENTATION_0);
6565 addMapperAndConfigure(mapper);
6566
6567 NotifyMotionArgs motionArgs;
6568 // Unrotated video frame
6569 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6570 std::vector<TouchVideoFrame> frames{frame};
6571 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6572 processPosition(mapper, 100, 200);
6573 processSync(mapper);
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6575 ASSERT_EQ(frames, motionArgs.videoFrames);
6576
6577 // Subsequent touch events should not have any videoframes
6578 // This is implemented separately in FakeEventHub,
6579 // but that should match the behaviour of TouchVideoDevice.
6580 processPosition(mapper, 200, 200);
6581 processSync(mapper);
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6584}
6585
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006586TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6587 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6588 prepareAxes(POSITION);
6589 addConfigurationProperty("touch.deviceType", "touchScreen");
6590 addMapperAndConfigure(mapper);
6591 // Unrotated video frame
6592 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6593 NotifyMotionArgs motionArgs;
6594
6595 // Test all 4 orientations
6596 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6597 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6598 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6599 clearViewports();
6600 prepareDisplay(orientation);
6601 std::vector<TouchVideoFrame> frames{frame};
6602 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6603 processPosition(mapper, 100, 200);
6604 processSync(mapper);
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 frames[0].rotate(orientation);
6607 ASSERT_EQ(frames, motionArgs.videoFrames);
6608 }
6609}
6610
6611TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6612 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6613 prepareAxes(POSITION);
6614 addConfigurationProperty("touch.deviceType", "touchScreen");
6615 addMapperAndConfigure(mapper);
6616 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6617 // so mix these.
6618 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6619 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6620 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6621 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6622 NotifyMotionArgs motionArgs;
6623
6624 prepareDisplay(DISPLAY_ORIENTATION_90);
6625 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6626 processPosition(mapper, 100, 200);
6627 processSync(mapper);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6629 std::for_each(frames.begin(), frames.end(),
6630 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6631 ASSERT_EQ(frames, motionArgs.videoFrames);
6632}
6633
Arthur Hung9da14732019-09-02 16:16:58 +08006634/**
6635 * If we had defined port associations, but the viewport is not ready, the touch device would be
6636 * expected to be disabled, and it should be enabled after the viewport has found.
6637 */
6638TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
6639 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6640 constexpr uint8_t hdmi2 = 1;
6641 const std::string secondaryUniqueId = "uniqueId2";
6642 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6643
6644 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6645
6646 addConfigurationProperty("touch.deviceType", "touchScreen");
6647 prepareAxes(POSITION);
6648 addMapperAndConfigure(mapper);
6649
6650 ASSERT_EQ(mDevice->isEnabled(), false);
6651
6652 // Add display on hdmi2, the device should be enabled and can receive touch event.
6653 prepareSecondaryDisplay(type, hdmi2);
6654 ASSERT_EQ(mDevice->isEnabled(), true);
6655
6656 // Send a touch event.
6657 processPosition(mapper, 100, 100);
6658 processSync(mapper);
6659
6660 NotifyMotionArgs args;
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6662 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6663}
6664
Arthur Hung6cd19a42019-08-30 19:04:12 +08006665/**
6666 * Test touch should not work if outside of surface.
6667 */
6668TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
6669 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6670 addConfigurationProperty("touch.deviceType", "touchScreen");
6671 prepareDisplay(DISPLAY_ORIENTATION_0);
6672 // Let surface be different from physical display.
6673 std::optional<DisplayViewport> internalViewport =
6674 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6675 internalViewport->logicalLeft = internalViewport->physicalTop + 20;
6676 internalViewport->logicalTop = internalViewport->physicalRight + 20;
6677 internalViewport->logicalRight = internalViewport->physicalRight - 20;
6678 internalViewport->logicalBottom = internalViewport->physicalBottom - 20;
6679 mFakePolicy->updateViewport(internalViewport.value());
6680
6681 prepareAxes(POSITION);
6682 addMapperAndConfigure(mapper);
6683
6684 int32_t rawX = 10;
6685 int32_t rawY = 10;
6686 processPosition(mapper, rawX, rawY);
6687 processSync(mapper);
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6689}
6690
Michael Wrightd02c5b62014-02-10 15:10:22 -08006691} // namespace android