blob: 8d4ab6afbde2edcfb38f521f656e1f410663bf95 [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 Pradhanf15a8aa2019-11-05 01:10:04 +00001136 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 Pradhanf15a8aa2019-11-05 01:10:04 +00001320 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001322 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 Pradhanf15a8aa2019-11-05 01:10:04 +00001327 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 Pradhanf15a8aa2019-11-05 01:10:04 +00001332 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);
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001353 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001354 }
1355
1356 void enableDevice(int32_t deviceId, InputDevice* device) {
1357 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001358 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001359 }
1360
Michael Wrightd02c5b62014-02-10 15:10:22 -08001361 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001362 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001363 const PropertyMap* configuration) {
1364 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1365 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1366 device->addMapper(mapper);
1367 mReader->setNextDevice(device);
1368 addDevice(deviceId, name, classes, configuration);
1369 return mapper;
1370 }
1371};
1372
1373TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001374 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001375 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001376 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001377 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001378
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001379 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381 ASSERT_EQ(1U, inputDevices.size());
1382 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001383 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1385 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1386 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1387
1388 // Should also have received a notification describing the new input devices.
1389 inputDevices = mFakePolicy->getInputDevices();
1390 ASSERT_EQ(1U, inputDevices.size());
1391 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001392 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001393 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1394 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1395 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1396}
1397
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001398TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1399 constexpr int32_t deviceId = 1;
1400 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001401 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001402 // Must add at least one mapper or the device will be ignored!
1403 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1404 device->addMapper(mapper);
1405 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001406 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001407
Yi Kong9b14ac62018-07-17 13:48:38 -07001408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001409
1410 NotifyDeviceResetArgs resetArgs;
1411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001412 ASSERT_EQ(deviceId, resetArgs.deviceId);
1413
1414 ASSERT_EQ(device->isEnabled(), true);
1415 disableDevice(deviceId, device);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001416
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001418 ASSERT_EQ(deviceId, resetArgs.deviceId);
1419 ASSERT_EQ(device->isEnabled(), false);
1420
1421 disableDevice(deviceId, device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001424 ASSERT_EQ(device->isEnabled(), false);
1425
1426 enableDevice(deviceId, device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001428 ASSERT_EQ(deviceId, resetArgs.deviceId);
1429 ASSERT_EQ(device->isEnabled(), true);
1430}
1431
Michael Wrightd02c5b62014-02-10 15:10:22 -08001432TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001433 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001434 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001435 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001436 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1437
1438 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1439 AINPUT_SOURCE_ANY, AKEYCODE_A))
1440 << "Should return unknown when the device id is >= 0 but unknown.";
1441
1442 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1443 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1444 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1445
1446 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1447 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1448 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1449
1450 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1451 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1452 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1453
1454 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1455 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1456 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1457}
1458
1459TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001460 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001461 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001462 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001463 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1464
1465 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1466 AINPUT_SOURCE_ANY, KEY_A))
1467 << "Should return unknown when the device id is >= 0 but unknown.";
1468
1469 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1470 AINPUT_SOURCE_TRACKBALL, KEY_A))
1471 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1472
1473 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1474 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1475 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1476
1477 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1478 AINPUT_SOURCE_TRACKBALL, KEY_A))
1479 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1480
1481 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1482 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1483 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1484}
1485
1486TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001487 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001488 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001489 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001490 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1491
1492 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1493 AINPUT_SOURCE_ANY, SW_LID))
1494 << "Should return unknown when the device id is >= 0 but unknown.";
1495
1496 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1497 AINPUT_SOURCE_TRACKBALL, SW_LID))
1498 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1499
1500 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1501 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1502 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1503
1504 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1505 AINPUT_SOURCE_TRACKBALL, SW_LID))
1506 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1507
1508 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1509 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1510 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1511}
1512
1513TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001514 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001515 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001516 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001517
Michael Wrightd02c5b62014-02-10 15:10:22 -08001518 mapper->addSupportedKeyCode(AKEYCODE_A);
1519 mapper->addSupportedKeyCode(AKEYCODE_B);
1520
1521 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1522 uint8_t flags[4] = { 0, 0, 0, 1 };
1523
1524 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1525 << "Should return false when device id is >= 0 but unknown.";
1526 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1527
1528 flags[3] = 1;
1529 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1530 << "Should return false when device id is valid but the sources are not supported by the device.";
1531 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1532
1533 flags[3] = 1;
1534 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1535 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1536 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1537
1538 flags[3] = 1;
1539 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1540 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1541 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1542
1543 flags[3] = 1;
1544 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1545 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1546 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1547}
1548
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001549TEST_F(InputReaderTest, WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001550 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001551
1552 NotifyConfigurationChangedArgs args;
1553
1554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1555 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1556}
1557
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001558TEST_F(InputReaderTest, ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001559 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001560 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001561 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562
1563 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1565
1566 RawEvent event;
1567 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1568 ASSERT_EQ(0, event.when);
1569 ASSERT_EQ(1, event.deviceId);
1570 ASSERT_EQ(EV_KEY, event.type);
1571 ASSERT_EQ(KEY_A, event.code);
1572 ASSERT_EQ(1, event.value);
1573}
1574
Prabir Pradhan42611e02018-11-27 14:04:02 -08001575TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1576 constexpr int32_t deviceId = 1;
1577 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001578 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001579 // Must add at least one mapper or the device will be ignored!
1580 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1581 device->addMapper(mapper);
1582 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001583 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001584
1585 NotifyDeviceResetArgs resetArgs;
1586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1587 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1588
1589 disableDevice(deviceId, device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001591 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1592 prevSequenceNum = resetArgs.sequenceNum;
1593
1594 enableDevice(deviceId, device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001596 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1597 prevSequenceNum = resetArgs.sequenceNum;
1598
1599 disableDevice(deviceId, device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001601 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1602 prevSequenceNum = resetArgs.sequenceNum;
1603}
1604
Arthur Hungc23540e2018-11-29 20:42:11 +08001605TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1606 constexpr int32_t deviceId = 1;
1607 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1608 const char* DEVICE_LOCATION = "USB1";
1609 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1610 DEVICE_LOCATION);
1611 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1612 device->addMapper(mapper);
1613 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001614
1615 const uint8_t hdmi1 = 1;
1616
1617 // Associated touch screen with second display.
1618 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1619
1620 // Add default and second display.
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001621 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001622 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1623 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1624 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1625 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1626 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001627
1628 // Add the device, and make sure all of the callbacks are triggered.
1629 // The device is added after the input port associations are processed since
1630 // we do not yet support dynamic device-to-display associations.
1631 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
1634 ASSERT_NO_FATAL_FAILURE(mapper->assertConfigureWasCalled());
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);
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
1644 ASSERT_NO_FATAL_FAILURE(mapper->assertConfigureWasCalled());
Arthur Hung2c9a3342019-07-23 14:18:59 +08001645 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001646}
1647
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648
1649// --- InputDeviceTest ---
1650
1651class InputDeviceTest : public testing::Test {
1652protected:
1653 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001654 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001655 static const int32_t DEVICE_ID;
1656 static const int32_t DEVICE_GENERATION;
1657 static const int32_t DEVICE_CONTROLLER_NUMBER;
1658 static const uint32_t DEVICE_CLASSES;
1659
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001660 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001661 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001662 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001663 FakeInputReaderContext* mFakeContext;
1664
1665 InputDevice* mDevice;
1666
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001667 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001668 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001669 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001670 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001671 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1672
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001673 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001674 InputDeviceIdentifier identifier;
1675 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001676 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001677 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1678 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1679 }
1680
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001681 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682 delete mDevice;
1683
1684 delete mFakeContext;
1685 mFakeListener.clear();
1686 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001687 }
1688};
1689
1690const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001691const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001692const int32_t InputDeviceTest::DEVICE_ID = 1;
1693const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1694const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1695const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1696 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1697
1698TEST_F(InputDeviceTest, ImmutableProperties) {
1699 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001700 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001701 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1702}
1703
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001704TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1705 ASSERT_EQ(mDevice->isEnabled(), true);
1706}
1707
Michael Wrightd02c5b62014-02-10 15:10:22 -08001708TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1709 // Configuration.
1710 InputReaderConfiguration config;
1711 mDevice->configure(ARBITRARY_TIME, &config, 0);
1712
1713 // Reset.
1714 mDevice->reset(ARBITRARY_TIME);
1715
1716 NotifyDeviceResetArgs resetArgs;
1717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1718 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1719 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1720
1721 // Metadata.
1722 ASSERT_TRUE(mDevice->isIgnored());
1723 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1724
1725 InputDeviceInfo info;
1726 mDevice->getDeviceInfo(&info);
1727 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001728 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1730 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1731
1732 // State queries.
1733 ASSERT_EQ(0, mDevice->getMetaState());
1734
1735 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1736 << "Ignored device should return unknown key code state.";
1737 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1738 << "Ignored device should return unknown scan code state.";
1739 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1740 << "Ignored device should return unknown switch state.";
1741
1742 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1743 uint8_t flags[2] = { 0, 1 };
1744 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1745 << "Ignored device should never mark any key codes.";
1746 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1747 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1748}
1749
1750TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1751 // Configuration.
1752 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1753
1754 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1755 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1756 mapper1->setMetaState(AMETA_ALT_ON);
1757 mapper1->addSupportedKeyCode(AKEYCODE_A);
1758 mapper1->addSupportedKeyCode(AKEYCODE_B);
1759 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1760 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1761 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1762 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1763 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1764 mDevice->addMapper(mapper1);
1765
1766 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1767 mapper2->setMetaState(AMETA_SHIFT_ON);
1768 mDevice->addMapper(mapper2);
1769
1770 InputReaderConfiguration config;
1771 mDevice->configure(ARBITRARY_TIME, &config, 0);
1772
1773 String8 propertyValue;
1774 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1775 << "Device should have read configuration during configuration phase.";
1776 ASSERT_STREQ("value", propertyValue.string());
1777
1778 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1779 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1780
1781 // Reset
1782 mDevice->reset(ARBITRARY_TIME);
1783 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1784 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1785
1786 NotifyDeviceResetArgs resetArgs;
1787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1788 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1789 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1790
1791 // Metadata.
1792 ASSERT_FALSE(mDevice->isIgnored());
1793 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1794
1795 InputDeviceInfo info;
1796 mDevice->getDeviceInfo(&info);
1797 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001798 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1800 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1801
1802 // State queries.
1803 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1804 << "Should query mappers and combine meta states.";
1805
1806 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1807 << "Should return unknown key code state when source not supported.";
1808 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1809 << "Should return unknown scan code state when source not supported.";
1810 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1811 << "Should return unknown switch state when source not supported.";
1812
1813 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1814 << "Should query mapper when source is supported.";
1815 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1816 << "Should query mapper when source is supported.";
1817 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1818 << "Should query mapper when source is supported.";
1819
1820 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1821 uint8_t flags[4] = { 0, 0, 0, 1 };
1822 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1823 << "Should do nothing when source is unsupported.";
1824 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1825 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1826 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1827 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1828
1829 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1830 << "Should query mapper when source is supported.";
1831 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1832 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1833 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1834 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1835
1836 // Event handling.
1837 RawEvent event;
1838 mDevice->process(&event, 1);
1839
1840 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1841 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1842}
1843
Arthur Hung2c9a3342019-07-23 14:18:59 +08001844// A single input device is associated with a specific display. Check that:
1845// 1. Device is disabled if the viewport corresponding to the associated display is not found
1846// 2. Device is disabled when setEnabled API is called
1847TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
1848 FakeInputMapper* mapper = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1849 mDevice->addMapper(mapper);
1850
1851 // First Configuration.
1852 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1853
1854 // Device should be enabled by default.
1855 ASSERT_TRUE(mDevice->isEnabled());
1856
1857 // Prepare associated info.
1858 constexpr uint8_t hdmi = 1;
1859 const std::string UNIQUE_ID = "local:1";
1860
1861 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1862 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1863 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1864 // Device should be disabled because it is associated with a specific display via
1865 // input port <-> display port association, but the corresponding display is not found
1866 ASSERT_FALSE(mDevice->isEnabled());
1867
1868 // Prepare displays.
1869 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1870 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1871 ViewportType::VIEWPORT_INTERNAL);
1872 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1873 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1874 ASSERT_TRUE(mDevice->isEnabled());
1875
1876 // Device should be disabled after set disable.
1877 mFakePolicy->addDisabledDevice(mDevice->getId());
1878 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1879 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1880 ASSERT_FALSE(mDevice->isEnabled());
1881
1882 // Device should still be disabled even found the associated display.
1883 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1884 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1885 ASSERT_FALSE(mDevice->isEnabled());
1886}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001887
1888// --- InputMapperTest ---
1889
1890class InputMapperTest : public testing::Test {
1891protected:
1892 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001893 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894 static const int32_t DEVICE_ID;
1895 static const int32_t DEVICE_GENERATION;
1896 static const int32_t DEVICE_CONTROLLER_NUMBER;
1897 static const uint32_t DEVICE_CLASSES;
1898
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001899 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001900 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001901 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001902 FakeInputReaderContext* mFakeContext;
1903 InputDevice* mDevice;
1904
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001905 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001906 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001907 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001908 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001909 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1910 InputDeviceIdentifier identifier;
1911 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001912 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001913 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1914 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1915
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001916 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001917 }
1918
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00001919 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001920 delete mDevice;
1921 delete mFakeContext;
1922 mFakeListener.clear();
1923 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001924 }
1925
1926 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001927 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001928 }
1929
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001930 void configureDevice(uint32_t changes) {
1931 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1932 }
1933
Michael Wrightd02c5b62014-02-10 15:10:22 -08001934 void addMapperAndConfigure(InputMapper* mapper) {
1935 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001936 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001937 mDevice->reset(ARBITRARY_TIME);
1938 }
1939
1940 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001941 int32_t orientation, const std::string& uniqueId,
1942 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001943 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001944 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001945 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1946 }
1947
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001948 void clearViewports() {
1949 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001950 }
1951
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001952 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001953 int32_t code, int32_t value) {
1954 RawEvent event;
1955 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001956 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001957 event.type = type;
1958 event.code = code;
1959 event.value = value;
1960 mapper->process(&event);
1961 }
1962
1963 static void assertMotionRange(const InputDeviceInfo& info,
1964 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1965 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001966 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001967 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1968 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1969 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1970 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1971 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1972 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1973 }
1974
1975 static void assertPointerCoords(const PointerCoords& coords,
1976 float x, float y, float pressure, float size,
1977 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1978 float orientation, float distance) {
1979 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1980 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1981 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1982 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1983 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1984 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1985 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1986 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1987 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1988 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1989 }
1990
1991 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1992 float actualX, actualY;
1993 controller->getPosition(&actualX, &actualY);
1994 ASSERT_NEAR(x, actualX, 1);
1995 ASSERT_NEAR(y, actualY, 1);
1996 }
1997};
1998
1999const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002000const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002001const int32_t InputMapperTest::DEVICE_ID = 1;
2002const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2003const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2004const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
2005
2006
2007// --- SwitchInputMapperTest ---
2008
2009class SwitchInputMapperTest : public InputMapperTest {
2010protected:
2011};
2012
2013TEST_F(SwitchInputMapperTest, GetSources) {
2014 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2015 addMapperAndConfigure(mapper);
2016
2017 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
2018}
2019
2020TEST_F(SwitchInputMapperTest, GetSwitchState) {
2021 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2022 addMapperAndConfigure(mapper);
2023
2024 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
2025 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2026
2027 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
2028 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2029}
2030
2031TEST_F(SwitchInputMapperTest, Process) {
2032 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2033 addMapperAndConfigure(mapper);
2034
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002035 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2036 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2037 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2038 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002039
2040 NotifySwitchArgs args;
2041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2042 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002043 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2044 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002045 args.switchMask);
2046 ASSERT_EQ(uint32_t(0), args.policyFlags);
2047}
2048
2049
2050// --- KeyboardInputMapperTest ---
2051
2052class KeyboardInputMapperTest : public InputMapperTest {
2053protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002054 const std::string UNIQUE_ID = "local:0";
2055
2056 void prepareDisplay(int32_t orientation);
2057
Arthur Hung2c9a3342019-07-23 14:18:59 +08002058 void testDPadKeyRotation(KeyboardInputMapper* mapper, int32_t originalScanCode,
2059 int32_t originalKeyCode, int32_t rotatedKeyCode,
2060 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002061};
2062
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002063/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2064 * orientation.
2065 */
2066void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2067 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002068 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002069}
2070
Michael Wrightd02c5b62014-02-10 15:10:22 -08002071void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002072 int32_t originalScanCode, int32_t originalKeyCode,
2073 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002074 NotifyKeyArgs args;
2075
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002076 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2078 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2079 ASSERT_EQ(originalScanCode, args.scanCode);
2080 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002081 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002082
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002083 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2085 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2086 ASSERT_EQ(originalScanCode, args.scanCode);
2087 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002088 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002089}
2090
Michael Wrightd02c5b62014-02-10 15:10:22 -08002091TEST_F(KeyboardInputMapperTest, GetSources) {
2092 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2093 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2094 addMapperAndConfigure(mapper);
2095
2096 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
2097}
2098
2099TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2100 const int32_t USAGE_A = 0x070004;
2101 const int32_t USAGE_UNKNOWN = 0x07ffff;
2102 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2103 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2104
2105 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2106 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2107 addMapperAndConfigure(mapper);
2108
2109 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002110 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002111 NotifyKeyArgs args;
2112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2113 ASSERT_EQ(DEVICE_ID, args.deviceId);
2114 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2115 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2116 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2117 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2118 ASSERT_EQ(KEY_HOME, args.scanCode);
2119 ASSERT_EQ(AMETA_NONE, args.metaState);
2120 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2121 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2122 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2123
2124 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002125 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2127 ASSERT_EQ(DEVICE_ID, args.deviceId);
2128 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2129 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2130 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2131 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2132 ASSERT_EQ(KEY_HOME, args.scanCode);
2133 ASSERT_EQ(AMETA_NONE, args.metaState);
2134 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2135 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2136 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2137
2138 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002139 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2140 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2142 ASSERT_EQ(DEVICE_ID, args.deviceId);
2143 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2144 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2146 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2147 ASSERT_EQ(0, args.scanCode);
2148 ASSERT_EQ(AMETA_NONE, args.metaState);
2149 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2150 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2151 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2152
2153 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002154 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2155 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2157 ASSERT_EQ(DEVICE_ID, args.deviceId);
2158 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2159 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2160 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2161 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2162 ASSERT_EQ(0, args.scanCode);
2163 ASSERT_EQ(AMETA_NONE, args.metaState);
2164 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2165 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2166 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2167
2168 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002169 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2170 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2172 ASSERT_EQ(DEVICE_ID, args.deviceId);
2173 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2174 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2175 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2176 ASSERT_EQ(0, args.keyCode);
2177 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2178 ASSERT_EQ(AMETA_NONE, args.metaState);
2179 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2180 ASSERT_EQ(0U, args.policyFlags);
2181 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2182
2183 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002184 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2185 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2187 ASSERT_EQ(DEVICE_ID, args.deviceId);
2188 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2189 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2190 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2191 ASSERT_EQ(0, args.keyCode);
2192 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2193 ASSERT_EQ(AMETA_NONE, args.metaState);
2194 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2195 ASSERT_EQ(0U, args.policyFlags);
2196 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2197}
2198
2199TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2200 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2201 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2202
2203 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2204 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2205 addMapperAndConfigure(mapper);
2206
2207 // Initial metastate.
2208 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2209
2210 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002211 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002212 NotifyKeyArgs args;
2213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2214 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2215 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2216 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2217
2218 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002219 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2221 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2223
2224 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002225 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2227 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2228 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2229
2230 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002231 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2233 ASSERT_EQ(AMETA_NONE, args.metaState);
2234 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2235 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2236}
2237
2238TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2239 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2240 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2241 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2242 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2243
2244 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2245 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2246 addMapperAndConfigure(mapper);
2247
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002248 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002249 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2250 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2251 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2252 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2253 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2254 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2255 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2256 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2257}
2258
2259TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2260 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2261 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2262 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2263 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2264
2265 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2266 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2267 addConfigurationProperty("keyboard.orientationAware", "1");
2268 addMapperAndConfigure(mapper);
2269
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002270 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002271 ASSERT_NO_FATAL_FAILURE(
2272 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2273 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2274 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2275 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2276 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2277 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2278 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002280 clearViewports();
2281 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002282 ASSERT_NO_FATAL_FAILURE(
2283 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2284 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2285 AKEYCODE_DPAD_UP, DISPLAY_ID));
2286 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2287 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2288 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2289 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002290
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002291 clearViewports();
2292 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002293 ASSERT_NO_FATAL_FAILURE(
2294 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2295 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2296 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2297 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2298 AKEYCODE_DPAD_UP, DISPLAY_ID));
2299 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2300 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002301
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002302 clearViewports();
2303 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002304 ASSERT_NO_FATAL_FAILURE(
2305 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2306 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2307 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2308 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2309 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2310 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2311 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002312
2313 // Special case: if orientation changes while key is down, we still emit the same keycode
2314 // in the key up as we did in the key down.
2315 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002316 clearViewports();
2317 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002318 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2320 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2321 ASSERT_EQ(KEY_UP, args.scanCode);
2322 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2323
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002324 clearViewports();
2325 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002326 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2328 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2329 ASSERT_EQ(KEY_UP, args.scanCode);
2330 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2331}
2332
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002333TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2334 // If the keyboard is not orientation aware,
2335 // key events should not be associated with a specific display id
2336 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2337
2338 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2339 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2340 addMapperAndConfigure(mapper);
2341 NotifyKeyArgs args;
2342
2343 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002344 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002346 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2348 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2349
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002350 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002351 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002353 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2355 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2356}
2357
2358TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2359 // If the keyboard is orientation aware,
2360 // key events should be associated with the internal viewport
2361 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2362
2363 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2364 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2365 addConfigurationProperty("keyboard.orientationAware", "1");
2366 addMapperAndConfigure(mapper);
2367 NotifyKeyArgs args;
2368
2369 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2370 // ^--- already checked by the previous test
2371
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002372 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002373 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002374 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002376 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2378 ASSERT_EQ(DISPLAY_ID, args.displayId);
2379
2380 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002381 clearViewports();
2382 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002383 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002384 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002386 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2388 ASSERT_EQ(newDisplayId, args.displayId);
2389}
2390
Michael Wrightd02c5b62014-02-10 15:10:22 -08002391TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2392 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2393 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2394 addMapperAndConfigure(mapper);
2395
2396 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2397 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2398
2399 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2400 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2401}
2402
2403TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2404 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2405 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2406 addMapperAndConfigure(mapper);
2407
2408 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2409 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2410
2411 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2412 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2413}
2414
2415TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2416 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2417 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2418 addMapperAndConfigure(mapper);
2419
2420 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2421
2422 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2423 uint8_t flags[2] = { 0, 0 };
2424 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2425 ASSERT_TRUE(flags[0]);
2426 ASSERT_FALSE(flags[1]);
2427}
2428
2429TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2430 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2431 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2432 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2433 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2434 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2435 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2436
2437 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2438 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2439 addMapperAndConfigure(mapper);
2440
2441 // Initialization should have turned all of the lights off.
2442 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2443 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2444 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2445
2446 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002447 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2448 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2450 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2451 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2452 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2453
2454 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002455 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2456 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2458 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2459 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2460 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2461
2462 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002463 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2464 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2466 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2467 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2468 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2469
2470 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002471 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2472 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2474 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2475 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2476 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2477
2478 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002479 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2480 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002481 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2482 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2483 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2484 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2485
2486 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002487 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2488 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2490 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2491 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2492 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2493}
2494
Arthur Hung2c9a3342019-07-23 14:18:59 +08002495TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2496 // keyboard 1.
2497 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2498 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2499 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2500 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2501
2502 // keyboard 2.
2503 const std::string USB2 = "USB2";
2504 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2505 InputDeviceIdentifier identifier;
2506 identifier.name = "KEYBOARD2";
2507 identifier.location = USB2;
2508 std::unique_ptr<InputDevice> device2 =
2509 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2510 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2511 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2512 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2513 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2514 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2515 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2516
2517 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2518 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2519 addMapperAndConfigure(mapper);
2520
2521 KeyboardInputMapper* mapper2 = new KeyboardInputMapper(device2.get(), AINPUT_SOURCE_KEYBOARD,
2522 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2523 device2->addMapper(mapper2);
2524 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2525 device2->reset(ARBITRARY_TIME);
2526
2527 // Prepared displays and associated info.
2528 constexpr uint8_t hdmi1 = 0;
2529 constexpr uint8_t hdmi2 = 1;
2530 const std::string SECONDARY_UNIQUE_ID = "local:1";
2531
2532 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2533 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2534
2535 // No associated display viewport found, should disable the device.
2536 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2537 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2538 ASSERT_FALSE(device2->isEnabled());
2539
2540 // Prepare second display.
2541 constexpr int32_t newDisplayId = 2;
2542 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2543 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2544 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2545 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2546 // Default device will reconfigure above, need additional reconfiguration for another device.
2547 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2548 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2549
2550 // Device should be enabled after the associated display is found.
2551 ASSERT_TRUE(mDevice->isEnabled());
2552 ASSERT_TRUE(device2->isEnabled());
2553
2554 // Test pad key events
2555 ASSERT_NO_FATAL_FAILURE(
2556 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2557 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2558 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2559 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2560 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2561 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2562 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2563
2564 ASSERT_NO_FATAL_FAILURE(
2565 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2567 AKEYCODE_DPAD_RIGHT, newDisplayId));
2568 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2569 AKEYCODE_DPAD_DOWN, newDisplayId));
2570 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2571 AKEYCODE_DPAD_LEFT, newDisplayId));
2572}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002573
2574// --- CursorInputMapperTest ---
2575
2576class CursorInputMapperTest : public InputMapperTest {
2577protected:
2578 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2579
2580 sp<FakePointerController> mFakePointerController;
2581
Prabir Pradhanf15a8aa2019-11-05 01:10:04 +00002582 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583 InputMapperTest::SetUp();
2584
2585 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002586 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587 }
2588
2589 void testMotionRotation(CursorInputMapper* mapper,
2590 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002591
2592 void prepareDisplay(int32_t orientation) {
2593 const std::string uniqueId = "local:0";
2594 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2595 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2596 orientation, uniqueId, NO_PORT, viewportType);
2597 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598};
2599
2600const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2601
2602void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2603 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2604 NotifyMotionArgs args;
2605
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002606 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2607 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2608 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2612 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2613 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2614 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2615}
2616
2617TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2618 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2619 addConfigurationProperty("cursor.mode", "pointer");
2620 addMapperAndConfigure(mapper);
2621
2622 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2623}
2624
2625TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2626 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2627 addConfigurationProperty("cursor.mode", "navigation");
2628 addMapperAndConfigure(mapper);
2629
2630 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2631}
2632
2633TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2634 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2635 addConfigurationProperty("cursor.mode", "pointer");
2636 addMapperAndConfigure(mapper);
2637
2638 InputDeviceInfo info;
2639 mapper->populateDeviceInfo(&info);
2640
2641 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002642 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2643 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2645 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2646
2647 // When the bounds are set, then there should be a valid motion range.
2648 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2649
2650 InputDeviceInfo info2;
2651 mapper->populateDeviceInfo(&info2);
2652
2653 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2654 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2655 1, 800 - 1, 0.0f, 0.0f));
2656 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2657 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2658 2, 480 - 1, 0.0f, 0.0f));
2659 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2660 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2661 0.0f, 1.0f, 0.0f, 0.0f));
2662}
2663
2664TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2665 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2666 addConfigurationProperty("cursor.mode", "navigation");
2667 addMapperAndConfigure(mapper);
2668
2669 InputDeviceInfo info;
2670 mapper->populateDeviceInfo(&info);
2671
2672 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2673 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2674 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2675 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2676 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2677 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2678 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2679 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2680 0.0f, 1.0f, 0.0f, 0.0f));
2681}
2682
2683TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2684 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2685 addConfigurationProperty("cursor.mode", "navigation");
2686 addMapperAndConfigure(mapper);
2687
2688 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2689
2690 NotifyMotionArgs args;
2691
2692 // Button press.
2693 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002694 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2695 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2697 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2698 ASSERT_EQ(DEVICE_ID, args.deviceId);
2699 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2700 ASSERT_EQ(uint32_t(0), args.policyFlags);
2701 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2702 ASSERT_EQ(0, args.flags);
2703 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2704 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2705 ASSERT_EQ(0, args.edgeFlags);
2706 ASSERT_EQ(uint32_t(1), args.pointerCount);
2707 ASSERT_EQ(0, args.pointerProperties[0].id);
2708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2710 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2711 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2712 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2713 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2714
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2716 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2717 ASSERT_EQ(DEVICE_ID, args.deviceId);
2718 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2719 ASSERT_EQ(uint32_t(0), args.policyFlags);
2720 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2721 ASSERT_EQ(0, args.flags);
2722 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2723 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2724 ASSERT_EQ(0, args.edgeFlags);
2725 ASSERT_EQ(uint32_t(1), args.pointerCount);
2726 ASSERT_EQ(0, args.pointerProperties[0].id);
2727 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2729 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2730 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2731 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2732 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2733
Michael Wrightd02c5b62014-02-10 15:10:22 -08002734 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002735 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2736 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2738 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2739 ASSERT_EQ(DEVICE_ID, args.deviceId);
2740 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2741 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002742 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2743 ASSERT_EQ(0, args.flags);
2744 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2745 ASSERT_EQ(0, args.buttonState);
2746 ASSERT_EQ(0, args.edgeFlags);
2747 ASSERT_EQ(uint32_t(1), args.pointerCount);
2748 ASSERT_EQ(0, args.pointerProperties[0].id);
2749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2751 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2752 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2753 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2754 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2755
2756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2757 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2758 ASSERT_EQ(DEVICE_ID, args.deviceId);
2759 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2760 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2762 ASSERT_EQ(0, args.flags);
2763 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2764 ASSERT_EQ(0, args.buttonState);
2765 ASSERT_EQ(0, args.edgeFlags);
2766 ASSERT_EQ(uint32_t(1), args.pointerCount);
2767 ASSERT_EQ(0, args.pointerProperties[0].id);
2768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2770 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2771 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2772 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2773 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2774}
2775
2776TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2777 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2778 addConfigurationProperty("cursor.mode", "navigation");
2779 addMapperAndConfigure(mapper);
2780
2781 NotifyMotionArgs args;
2782
2783 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002784 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2785 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2789 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2790
2791 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002792 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2793 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2797 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2798}
2799
2800TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2801 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2802 addConfigurationProperty("cursor.mode", "navigation");
2803 addMapperAndConfigure(mapper);
2804
2805 NotifyMotionArgs args;
2806
2807 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002808 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2809 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2811 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2813 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2814
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2816 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2818 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2819
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002821 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2822 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002824 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2826 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2827
2828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2831 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2832}
2833
2834TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2835 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2836 addConfigurationProperty("cursor.mode", "navigation");
2837 addMapperAndConfigure(mapper);
2838
2839 NotifyMotionArgs args;
2840
2841 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002842 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2843 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2844 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2845 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2847 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2849 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2850 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2851
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2853 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2855 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2856 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2857
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002859 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2860 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2861 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2865 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2866 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2867
2868 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002869 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2870 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002872 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2874 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2875
2876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2879 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2880}
2881
2882TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2883 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2884 addConfigurationProperty("cursor.mode", "navigation");
2885 addMapperAndConfigure(mapper);
2886
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002887 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002888 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2889 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2890 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2891 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2892 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2893 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2894 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2895 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2896}
2897
2898TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2899 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2900 addConfigurationProperty("cursor.mode", "navigation");
2901 addConfigurationProperty("cursor.orientationAware", "1");
2902 addMapperAndConfigure(mapper);
2903
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002904 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2906 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2907 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2908 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2909 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2910 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2911 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2912 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2913
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002914 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2916 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2917 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2918 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2919 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2920 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2921 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2922 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2923
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002924 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002925 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2926 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2927 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2928 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2929 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2930 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2931 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2932 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2933
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002934 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002935 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2936 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2937 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2938 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2939 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2940 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2941 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2942 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2943}
2944
2945TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2946 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2947 addConfigurationProperty("cursor.mode", "pointer");
2948 addMapperAndConfigure(mapper);
2949
2950 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2951 mFakePointerController->setPosition(100, 200);
2952 mFakePointerController->setButtonState(0);
2953
2954 NotifyMotionArgs motionArgs;
2955 NotifyKeyArgs keyArgs;
2956
2957 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002958 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2959 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2961 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2962 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2963 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2965 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2966
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2969 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2970 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2972 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2973
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002974 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2975 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002977 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978 ASSERT_EQ(0, motionArgs.buttonState);
2979 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2981 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2982
2983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002984 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985 ASSERT_EQ(0, motionArgs.buttonState);
2986 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2988 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2989
2990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002992 ASSERT_EQ(0, motionArgs.buttonState);
2993 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2995 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2996
2997 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002998 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
2999 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3000 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3002 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3003 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3004 motionArgs.buttonState);
3005 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3006 mFakePointerController->getButtonState());
3007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3008 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3009
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3011 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3012 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3013 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3014 mFakePointerController->getButtonState());
3015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3016 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3017
3018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3019 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3020 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3021 motionArgs.buttonState);
3022 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3023 mFakePointerController->getButtonState());
3024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3025 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3026
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003027 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3028 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3032 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3034 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3035
3036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003038 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3039 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3041 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3042
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003043 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3044 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003046 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3047 ASSERT_EQ(0, motionArgs.buttonState);
3048 ASSERT_EQ(0, mFakePointerController->getButtonState());
3049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3050 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 -08003051 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3052 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003053
3054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 ASSERT_EQ(0, motionArgs.buttonState);
3056 ASSERT_EQ(0, mFakePointerController->getButtonState());
3057 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3059 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 -08003060
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3062 ASSERT_EQ(0, motionArgs.buttonState);
3063 ASSERT_EQ(0, mFakePointerController->getButtonState());
3064 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3066 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3067
3068 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003069 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3070 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3072 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3073 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003074
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003076 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3078 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3080 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3081
3082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3083 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3084 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3085 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3087 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3088
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003089 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3090 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093 ASSERT_EQ(0, motionArgs.buttonState);
3094 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3096 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3097
3098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003100 ASSERT_EQ(0, motionArgs.buttonState);
3101 ASSERT_EQ(0, mFakePointerController->getButtonState());
3102
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3104 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3106 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3107 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3108
3109 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003110 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3111 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3113 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3114 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003115
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003117 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3119 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3121 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3122
3123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3125 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3126 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3128 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3129
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003130 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3131 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134 ASSERT_EQ(0, motionArgs.buttonState);
3135 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3137 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 -08003138
3139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3140 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3141 ASSERT_EQ(0, motionArgs.buttonState);
3142 ASSERT_EQ(0, mFakePointerController->getButtonState());
3143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3144 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3145
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3147 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3148 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3149
3150 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003151 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3152 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3154 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3155 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003156
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003158 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3160 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3162 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3163
3164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3165 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3166 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3167 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3169 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3170
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003171 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3172 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175 ASSERT_EQ(0, motionArgs.buttonState);
3176 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3178 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003179
3180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3181 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3182 ASSERT_EQ(0, motionArgs.buttonState);
3183 ASSERT_EQ(0, mFakePointerController->getButtonState());
3184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3185 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3186
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3188 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3189 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3190
3191 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003192 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3193 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3195 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3196 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003197
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003199 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3201 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3203 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3204
3205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3207 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3208 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3210 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3211
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003212 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3213 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216 ASSERT_EQ(0, motionArgs.buttonState);
3217 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3219 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003220
3221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3222 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3223 ASSERT_EQ(0, motionArgs.buttonState);
3224 ASSERT_EQ(0, mFakePointerController->getButtonState());
3225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3226 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3227
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3229 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3230 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3231}
3232
3233TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3234 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3235 addConfigurationProperty("cursor.mode", "pointer");
3236 addMapperAndConfigure(mapper);
3237
3238 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3239 mFakePointerController->setPosition(100, 200);
3240 mFakePointerController->setButtonState(0);
3241
3242 NotifyMotionArgs args;
3243
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003244 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3245 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3246 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003248 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3249 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3251 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3252 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3253}
3254
3255TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3256 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3257 addConfigurationProperty("cursor.mode", "pointer");
3258 mFakePolicy->setPointerCapture(true);
3259 addMapperAndConfigure(mapper);
3260
3261 NotifyDeviceResetArgs resetArgs;
3262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3263 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3264 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3265
3266 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3267 mFakePointerController->setPosition(100, 200);
3268 mFakePointerController->setButtonState(0);
3269
3270 NotifyMotionArgs args;
3271
3272 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003273 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3274 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3275 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3277 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3280 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3281 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3282
3283 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003284 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3285 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3287 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3288 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3290 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3292 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3295 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3296
3297 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003298 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3299 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3301 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3304 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3306 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3307 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3309 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3310
3311 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003312 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3313 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3314 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3316 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3319 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3320 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3321
3322 // Disable pointer capture and check that the device generation got bumped
3323 // and events are generated the usual way.
3324 const uint32_t generation = mFakeContext->getGeneration();
3325 mFakePolicy->setPointerCapture(false);
3326 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3327 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3328
3329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3330 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3331 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3332
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003333 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3334 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3335 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3337 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3340 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3341 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3342}
3343
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003344TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3345 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3346 addMapperAndConfigure(mapper);
3347
3348 // Setup PointerController for second display.
3349 constexpr int32_t SECOND_DISPLAY_ID = 1;
3350 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3351 mFakePointerController->setPosition(100, 200);
3352 mFakePointerController->setButtonState(0);
3353 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3354
3355 NotifyMotionArgs args;
3356 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3357 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3358 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3360 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3363 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3364 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3365 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3366}
3367
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368
3369// --- TouchInputMapperTest ---
3370
3371class TouchInputMapperTest : public InputMapperTest {
3372protected:
3373 static const int32_t RAW_X_MIN;
3374 static const int32_t RAW_X_MAX;
3375 static const int32_t RAW_Y_MIN;
3376 static const int32_t RAW_Y_MAX;
3377 static const int32_t RAW_TOUCH_MIN;
3378 static const int32_t RAW_TOUCH_MAX;
3379 static const int32_t RAW_TOOL_MIN;
3380 static const int32_t RAW_TOOL_MAX;
3381 static const int32_t RAW_PRESSURE_MIN;
3382 static const int32_t RAW_PRESSURE_MAX;
3383 static const int32_t RAW_ORIENTATION_MIN;
3384 static const int32_t RAW_ORIENTATION_MAX;
3385 static const int32_t RAW_DISTANCE_MIN;
3386 static const int32_t RAW_DISTANCE_MAX;
3387 static const int32_t RAW_TILT_MIN;
3388 static const int32_t RAW_TILT_MAX;
3389 static const int32_t RAW_ID_MIN;
3390 static const int32_t RAW_ID_MAX;
3391 static const int32_t RAW_SLOT_MIN;
3392 static const int32_t RAW_SLOT_MAX;
3393 static const float X_PRECISION;
3394 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003395 static const float X_PRECISION_VIRTUAL;
3396 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397
3398 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003399 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400
3401 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3402
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003403 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003404 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003405
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406 enum Axes {
3407 POSITION = 1 << 0,
3408 TOUCH = 1 << 1,
3409 TOOL = 1 << 2,
3410 PRESSURE = 1 << 3,
3411 ORIENTATION = 1 << 4,
3412 MINOR = 1 << 5,
3413 ID = 1 << 6,
3414 DISTANCE = 1 << 7,
3415 TILT = 1 << 8,
3416 SLOT = 1 << 9,
3417 TOOL_TYPE = 1 << 10,
3418 };
3419
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003420 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3421 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003422 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003424 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 int32_t toRawX(float displayX);
3426 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003427 float toCookedX(float rawX, float rawY);
3428 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003430 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003432 float toDisplayY(int32_t rawY, int32_t displayHeight);
3433
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434};
3435
3436const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3437const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3438const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3439const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3440const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3441const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3442const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3443const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003444const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3445const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3447const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3448const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3449const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3450const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3451const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3452const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3453const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3454const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3455const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3456const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3457const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003458const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3459 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3460const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3461 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003462const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3463 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003464
3465const float TouchInputMapperTest::GEOMETRIC_SCALE =
3466 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3467 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3468
3469const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3470 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3471 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3472};
3473
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003474void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003475 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003476 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3477}
3478
3479void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3480 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3481 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482}
3483
Santos Cordonfa5cf462017-04-05 10:37:00 -07003484void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003485 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3486 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003487 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003488}
3489
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490void TouchInputMapperTest::prepareVirtualKeys() {
3491 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3492 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3493 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3494 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3495}
3496
Jason Gerecke489fda82012-09-07 17:19:40 -07003497void TouchInputMapperTest::prepareLocationCalibration() {
3498 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3499}
3500
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501int32_t TouchInputMapperTest::toRawX(float displayX) {
3502 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3503}
3504
3505int32_t TouchInputMapperTest::toRawY(float displayY) {
3506 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3507}
3508
Jason Gerecke489fda82012-09-07 17:19:40 -07003509float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3510 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3511 return rawX;
3512}
3513
3514float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3515 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3516 return rawY;
3517}
3518
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003520 return toDisplayX(rawX, DISPLAY_WIDTH);
3521}
3522
3523float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3524 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525}
3526
3527float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003528 return toDisplayY(rawY, DISPLAY_HEIGHT);
3529}
3530
3531float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3532 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533}
3534
3535
3536// --- SingleTouchInputMapperTest ---
3537
3538class SingleTouchInputMapperTest : public TouchInputMapperTest {
3539protected:
3540 void prepareButtons();
3541 void prepareAxes(int axes);
3542
3543 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3544 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3545 void processUp(SingleTouchInputMapper* mappery);
3546 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3547 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3548 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3549 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3550 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3551 void processSync(SingleTouchInputMapper* mapper);
3552};
3553
3554void SingleTouchInputMapperTest::prepareButtons() {
3555 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3556}
3557
3558void SingleTouchInputMapperTest::prepareAxes(int axes) {
3559 if (axes & POSITION) {
3560 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3561 RAW_X_MIN, RAW_X_MAX, 0, 0);
3562 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3563 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3564 }
3565 if (axes & PRESSURE) {
3566 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3567 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3568 }
3569 if (axes & TOOL) {
3570 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3571 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3572 }
3573 if (axes & DISTANCE) {
3574 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3575 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3576 }
3577 if (axes & TILT) {
3578 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3579 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3580 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3581 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3582 }
3583}
3584
3585void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003586 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3587 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3588 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589}
3590
3591void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003592 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3593 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594}
3595
3596void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003597 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598}
3599
3600void SingleTouchInputMapperTest::processPressure(
3601 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003602 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603}
3604
3605void SingleTouchInputMapperTest::processToolMajor(
3606 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003607 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608}
3609
3610void SingleTouchInputMapperTest::processDistance(
3611 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003612 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613}
3614
3615void SingleTouchInputMapperTest::processTilt(
3616 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003617 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3618 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003619}
3620
3621void SingleTouchInputMapperTest::processKey(
3622 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003623 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624}
3625
3626void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003627 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628}
3629
3630
3631TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3632 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3633 prepareButtons();
3634 prepareAxes(POSITION);
3635 addMapperAndConfigure(mapper);
3636
3637 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3638}
3639
3640TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3641 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3642 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3643 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3644 prepareButtons();
3645 prepareAxes(POSITION);
3646 addMapperAndConfigure(mapper);
3647
3648 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3649}
3650
3651TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3652 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3653 prepareButtons();
3654 prepareAxes(POSITION);
3655 addConfigurationProperty("touch.deviceType", "touchPad");
3656 addMapperAndConfigure(mapper);
3657
3658 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3659}
3660
3661TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3662 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3663 prepareButtons();
3664 prepareAxes(POSITION);
3665 addConfigurationProperty("touch.deviceType", "touchScreen");
3666 addMapperAndConfigure(mapper);
3667
3668 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3669}
3670
3671TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3672 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3673 addConfigurationProperty("touch.deviceType", "touchScreen");
3674 prepareDisplay(DISPLAY_ORIENTATION_0);
3675 prepareButtons();
3676 prepareAxes(POSITION);
3677 prepareVirtualKeys();
3678 addMapperAndConfigure(mapper);
3679
3680 // Unknown key.
3681 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3682
3683 // Virtual key is down.
3684 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3685 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3686 processDown(mapper, x, y);
3687 processSync(mapper);
3688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3689
3690 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3691
3692 // Virtual key is up.
3693 processUp(mapper);
3694 processSync(mapper);
3695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3696
3697 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3698}
3699
3700TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3701 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3702 addConfigurationProperty("touch.deviceType", "touchScreen");
3703 prepareDisplay(DISPLAY_ORIENTATION_0);
3704 prepareButtons();
3705 prepareAxes(POSITION);
3706 prepareVirtualKeys();
3707 addMapperAndConfigure(mapper);
3708
3709 // Unknown key.
3710 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3711
3712 // Virtual key is down.
3713 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3714 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3715 processDown(mapper, x, y);
3716 processSync(mapper);
3717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3718
3719 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3720
3721 // Virtual key is up.
3722 processUp(mapper);
3723 processSync(mapper);
3724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3725
3726 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3727}
3728
3729TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3730 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3731 addConfigurationProperty("touch.deviceType", "touchScreen");
3732 prepareDisplay(DISPLAY_ORIENTATION_0);
3733 prepareButtons();
3734 prepareAxes(POSITION);
3735 prepareVirtualKeys();
3736 addMapperAndConfigure(mapper);
3737
3738 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3739 uint8_t flags[2] = { 0, 0 };
3740 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3741 ASSERT_TRUE(flags[0]);
3742 ASSERT_FALSE(flags[1]);
3743}
3744
3745TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3746 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3747 addConfigurationProperty("touch.deviceType", "touchScreen");
3748 prepareDisplay(DISPLAY_ORIENTATION_0);
3749 prepareButtons();
3750 prepareAxes(POSITION);
3751 prepareVirtualKeys();
3752 addMapperAndConfigure(mapper);
3753
3754 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3755
3756 NotifyKeyArgs args;
3757
3758 // Press virtual key.
3759 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3760 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3761 processDown(mapper, x, y);
3762 processSync(mapper);
3763
3764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3765 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3766 ASSERT_EQ(DEVICE_ID, args.deviceId);
3767 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3768 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3769 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3770 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3771 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3772 ASSERT_EQ(KEY_HOME, args.scanCode);
3773 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3774 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3775
3776 // Release virtual key.
3777 processUp(mapper);
3778 processSync(mapper);
3779
3780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3781 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3782 ASSERT_EQ(DEVICE_ID, args.deviceId);
3783 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3784 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3785 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3786 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3787 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3788 ASSERT_EQ(KEY_HOME, args.scanCode);
3789 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3790 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3791
3792 // Should not have sent any motions.
3793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3794}
3795
3796TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3797 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3798 addConfigurationProperty("touch.deviceType", "touchScreen");
3799 prepareDisplay(DISPLAY_ORIENTATION_0);
3800 prepareButtons();
3801 prepareAxes(POSITION);
3802 prepareVirtualKeys();
3803 addMapperAndConfigure(mapper);
3804
3805 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3806
3807 NotifyKeyArgs keyArgs;
3808
3809 // Press virtual key.
3810 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3811 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3812 processDown(mapper, x, y);
3813 processSync(mapper);
3814
3815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3816 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3817 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3818 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3819 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3820 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3821 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3822 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3823 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3824 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3825 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3826
3827 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3828 // into the display area.
3829 y -= 100;
3830 processMove(mapper, x, y);
3831 processSync(mapper);
3832
3833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3834 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3835 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3836 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3837 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3838 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3839 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3840 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3841 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3842 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3843 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3844 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3845
3846 NotifyMotionArgs motionArgs;
3847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3848 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3849 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3850 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3851 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3852 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3853 ASSERT_EQ(0, motionArgs.flags);
3854 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3855 ASSERT_EQ(0, motionArgs.buttonState);
3856 ASSERT_EQ(0, motionArgs.edgeFlags);
3857 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3858 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3861 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3862 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3863 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3864 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3865
3866 // Keep moving out of bounds. Should generate a pointer move.
3867 y -= 50;
3868 processMove(mapper, x, y);
3869 processSync(mapper);
3870
3871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3872 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3873 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3874 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3875 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3877 ASSERT_EQ(0, motionArgs.flags);
3878 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3879 ASSERT_EQ(0, motionArgs.buttonState);
3880 ASSERT_EQ(0, motionArgs.edgeFlags);
3881 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3882 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3885 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3886 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3887 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3888 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3889
3890 // Release out of bounds. Should generate a pointer up.
3891 processUp(mapper);
3892 processSync(mapper);
3893
3894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3895 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3896 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3897 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3898 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3899 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3900 ASSERT_EQ(0, motionArgs.flags);
3901 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3902 ASSERT_EQ(0, motionArgs.buttonState);
3903 ASSERT_EQ(0, motionArgs.edgeFlags);
3904 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3905 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3908 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3909 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3910 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3911 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3912
3913 // Should not have sent any more keys or motions.
3914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3916}
3917
3918TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3919 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3920 addConfigurationProperty("touch.deviceType", "touchScreen");
3921 prepareDisplay(DISPLAY_ORIENTATION_0);
3922 prepareButtons();
3923 prepareAxes(POSITION);
3924 prepareVirtualKeys();
3925 addMapperAndConfigure(mapper);
3926
3927 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3928
3929 NotifyMotionArgs motionArgs;
3930
3931 // Initially go down out of bounds.
3932 int32_t x = -10;
3933 int32_t y = -10;
3934 processDown(mapper, x, y);
3935 processSync(mapper);
3936
3937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3938
3939 // Move into the display area. Should generate a pointer down.
3940 x = 50;
3941 y = 75;
3942 processMove(mapper, x, y);
3943 processSync(mapper);
3944
3945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3946 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3947 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3948 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3949 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3950 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3951 ASSERT_EQ(0, motionArgs.flags);
3952 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3953 ASSERT_EQ(0, motionArgs.buttonState);
3954 ASSERT_EQ(0, motionArgs.edgeFlags);
3955 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3956 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3959 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3960 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3961 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3962 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3963
3964 // Release. Should generate a pointer up.
3965 processUp(mapper);
3966 processSync(mapper);
3967
3968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3970 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3971 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3972 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3973 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3974 ASSERT_EQ(0, motionArgs.flags);
3975 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3976 ASSERT_EQ(0, motionArgs.buttonState);
3977 ASSERT_EQ(0, motionArgs.edgeFlags);
3978 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3979 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3982 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3983 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3984 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3985 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3986
3987 // Should not have sent any more keys or motions.
3988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3990}
3991
Santos Cordonfa5cf462017-04-05 10:37:00 -07003992TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3993 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3994 addConfigurationProperty("touch.deviceType", "touchScreen");
3995 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3996
3997 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
3998 prepareButtons();
3999 prepareAxes(POSITION);
4000 prepareVirtualKeys();
4001 addMapperAndConfigure(mapper);
4002
4003 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4004
4005 NotifyMotionArgs motionArgs;
4006
4007 // Down.
4008 int32_t x = 100;
4009 int32_t y = 125;
4010 processDown(mapper, x, y);
4011 processSync(mapper);
4012
4013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4014 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4015 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4016 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4017 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4018 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4019 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4020 ASSERT_EQ(0, motionArgs.flags);
4021 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4022 ASSERT_EQ(0, motionArgs.buttonState);
4023 ASSERT_EQ(0, motionArgs.edgeFlags);
4024 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4025 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4026 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4028 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4029 1, 0, 0, 0, 0, 0, 0, 0));
4030 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4031 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4032 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4033
4034 // Move.
4035 x += 50;
4036 y += 75;
4037 processMove(mapper, x, y);
4038 processSync(mapper);
4039
4040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4041 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4042 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4043 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4044 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4045 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4047 ASSERT_EQ(0, motionArgs.flags);
4048 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4049 ASSERT_EQ(0, motionArgs.buttonState);
4050 ASSERT_EQ(0, motionArgs.edgeFlags);
4051 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4052 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4055 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4056 1, 0, 0, 0, 0, 0, 0, 0));
4057 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4058 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4059 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4060
4061 // Up.
4062 processUp(mapper);
4063 processSync(mapper);
4064
4065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4066 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4067 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4068 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4069 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4070 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4071 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4072 ASSERT_EQ(0, motionArgs.flags);
4073 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4074 ASSERT_EQ(0, motionArgs.buttonState);
4075 ASSERT_EQ(0, motionArgs.edgeFlags);
4076 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4077 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4080 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4081 1, 0, 0, 0, 0, 0, 0, 0));
4082 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4083 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4084 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4085
4086 // Should not have sent any more keys or motions.
4087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4089}
4090
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
4092 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4093 addConfigurationProperty("touch.deviceType", "touchScreen");
4094 prepareDisplay(DISPLAY_ORIENTATION_0);
4095 prepareButtons();
4096 prepareAxes(POSITION);
4097 prepareVirtualKeys();
4098 addMapperAndConfigure(mapper);
4099
4100 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4101
4102 NotifyMotionArgs motionArgs;
4103
4104 // Down.
4105 int32_t x = 100;
4106 int32_t y = 125;
4107 processDown(mapper, x, y);
4108 processSync(mapper);
4109
4110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4111 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4112 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4113 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4114 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4115 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4116 ASSERT_EQ(0, motionArgs.flags);
4117 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4118 ASSERT_EQ(0, motionArgs.buttonState);
4119 ASSERT_EQ(0, motionArgs.edgeFlags);
4120 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4121 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4124 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4125 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4126 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4127 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4128
4129 // Move.
4130 x += 50;
4131 y += 75;
4132 processMove(mapper, x, y);
4133 processSync(mapper);
4134
4135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4136 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4137 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4138 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4139 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4141 ASSERT_EQ(0, motionArgs.flags);
4142 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4143 ASSERT_EQ(0, motionArgs.buttonState);
4144 ASSERT_EQ(0, motionArgs.edgeFlags);
4145 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4146 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4149 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4150 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4151 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4152 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4153
4154 // Up.
4155 processUp(mapper);
4156 processSync(mapper);
4157
4158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4159 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4160 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4161 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4162 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4163 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4164 ASSERT_EQ(0, motionArgs.flags);
4165 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4166 ASSERT_EQ(0, motionArgs.buttonState);
4167 ASSERT_EQ(0, motionArgs.edgeFlags);
4168 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4169 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4172 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4173 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4174 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4175 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4176
4177 // Should not have sent any more keys or motions.
4178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4180}
4181
4182TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
4183 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4184 addConfigurationProperty("touch.deviceType", "touchScreen");
4185 prepareButtons();
4186 prepareAxes(POSITION);
4187 addConfigurationProperty("touch.orientationAware", "0");
4188 addMapperAndConfigure(mapper);
4189
4190 NotifyMotionArgs args;
4191
4192 // Rotation 90.
4193 prepareDisplay(DISPLAY_ORIENTATION_90);
4194 processDown(mapper, toRawX(50), toRawY(75));
4195 processSync(mapper);
4196
4197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4198 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4199 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4200
4201 processUp(mapper);
4202 processSync(mapper);
4203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4204}
4205
4206TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4207 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4208 addConfigurationProperty("touch.deviceType", "touchScreen");
4209 prepareButtons();
4210 prepareAxes(POSITION);
4211 addMapperAndConfigure(mapper);
4212
4213 NotifyMotionArgs args;
4214
4215 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004216 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 prepareDisplay(DISPLAY_ORIENTATION_0);
4218 processDown(mapper, toRawX(50), toRawY(75));
4219 processSync(mapper);
4220
4221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4222 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4223 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4224
4225 processUp(mapper);
4226 processSync(mapper);
4227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4228
4229 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004230 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 prepareDisplay(DISPLAY_ORIENTATION_90);
4232 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4233 processSync(mapper);
4234
4235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4236 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4237 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4238
4239 processUp(mapper);
4240 processSync(mapper);
4241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4242
4243 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004244 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 prepareDisplay(DISPLAY_ORIENTATION_180);
4246 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4247 processSync(mapper);
4248
4249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4250 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4251 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4252
4253 processUp(mapper);
4254 processSync(mapper);
4255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4256
4257 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004258 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259 prepareDisplay(DISPLAY_ORIENTATION_270);
4260 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4261 processSync(mapper);
4262
4263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4264 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4265 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4266
4267 processUp(mapper);
4268 processSync(mapper);
4269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4270}
4271
4272TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4273 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4274 addConfigurationProperty("touch.deviceType", "touchScreen");
4275 prepareDisplay(DISPLAY_ORIENTATION_0);
4276 prepareButtons();
4277 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4278 addMapperAndConfigure(mapper);
4279
4280 // These calculations are based on the input device calibration documentation.
4281 int32_t rawX = 100;
4282 int32_t rawY = 200;
4283 int32_t rawPressure = 10;
4284 int32_t rawToolMajor = 12;
4285 int32_t rawDistance = 2;
4286 int32_t rawTiltX = 30;
4287 int32_t rawTiltY = 110;
4288
4289 float x = toDisplayX(rawX);
4290 float y = toDisplayY(rawY);
4291 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4292 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4293 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4294 float distance = float(rawDistance);
4295
4296 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4297 float tiltScale = M_PI / 180;
4298 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4299 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4300 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4301 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4302
4303 processDown(mapper, rawX, rawY);
4304 processPressure(mapper, rawPressure);
4305 processToolMajor(mapper, rawToolMajor);
4306 processDistance(mapper, rawDistance);
4307 processTilt(mapper, rawTiltX, rawTiltY);
4308 processSync(mapper);
4309
4310 NotifyMotionArgs args;
4311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4313 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4314 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4315}
4316
Jason Gerecke489fda82012-09-07 17:19:40 -07004317TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4318 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4319 addConfigurationProperty("touch.deviceType", "touchScreen");
4320 prepareDisplay(DISPLAY_ORIENTATION_0);
4321 prepareLocationCalibration();
4322 prepareButtons();
4323 prepareAxes(POSITION);
4324 addMapperAndConfigure(mapper);
4325
4326 int32_t rawX = 100;
4327 int32_t rawY = 200;
4328
4329 float x = toDisplayX(toCookedX(rawX, rawY));
4330 float y = toDisplayY(toCookedY(rawX, rawY));
4331
4332 processDown(mapper, rawX, rawY);
4333 processSync(mapper);
4334
4335 NotifyMotionArgs args;
4336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4338 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4339}
4340
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4342 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4343 addConfigurationProperty("touch.deviceType", "touchScreen");
4344 prepareDisplay(DISPLAY_ORIENTATION_0);
4345 prepareButtons();
4346 prepareAxes(POSITION);
4347 addMapperAndConfigure(mapper);
4348
4349 NotifyMotionArgs motionArgs;
4350 NotifyKeyArgs keyArgs;
4351
4352 processDown(mapper, 100, 200);
4353 processSync(mapper);
4354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4355 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4356 ASSERT_EQ(0, motionArgs.buttonState);
4357
4358 // press BTN_LEFT, release BTN_LEFT
4359 processKey(mapper, BTN_LEFT, 1);
4360 processSync(mapper);
4361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4363 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4364
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4367 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4368
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 processKey(mapper, BTN_LEFT, 0);
4370 processSync(mapper);
4371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004372 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004377 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378
4379 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4380 processKey(mapper, BTN_RIGHT, 1);
4381 processKey(mapper, BTN_MIDDLE, 1);
4382 processSync(mapper);
4383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4385 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4386 motionArgs.buttonState);
4387
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4390 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4391
4392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4394 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4395 motionArgs.buttonState);
4396
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 processKey(mapper, BTN_RIGHT, 0);
4398 processSync(mapper);
4399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004402
4403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406
4407 processKey(mapper, BTN_MIDDLE, 0);
4408 processSync(mapper);
4409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412
4413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416
4417 // press BTN_BACK, release BTN_BACK
4418 processKey(mapper, BTN_BACK, 1);
4419 processSync(mapper);
4420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4421 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4422 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004423
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004426 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4427
4428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4430 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431
4432 processKey(mapper, BTN_BACK, 0);
4433 processSync(mapper);
4434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004437
4438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004440 ASSERT_EQ(0, motionArgs.buttonState);
4441
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4443 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4444 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4445
4446 // press BTN_SIDE, release BTN_SIDE
4447 processKey(mapper, BTN_SIDE, 1);
4448 processSync(mapper);
4449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4450 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4451 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004452
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004455 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4456
4457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4459 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460
4461 processKey(mapper, BTN_SIDE, 0);
4462 processSync(mapper);
4463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004466
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004469 ASSERT_EQ(0, motionArgs.buttonState);
4470
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4472 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4473 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4474
4475 // press BTN_FORWARD, release BTN_FORWARD
4476 processKey(mapper, BTN_FORWARD, 1);
4477 processSync(mapper);
4478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4479 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4480 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004481
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004484 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4485
4486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4487 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4488 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489
4490 processKey(mapper, BTN_FORWARD, 0);
4491 processSync(mapper);
4492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004495
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004498 ASSERT_EQ(0, motionArgs.buttonState);
4499
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4501 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4502 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4503
4504 // press BTN_EXTRA, release BTN_EXTRA
4505 processKey(mapper, BTN_EXTRA, 1);
4506 processSync(mapper);
4507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4508 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4509 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004510
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004513 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4514
4515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4516 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4517 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518
4519 processKey(mapper, BTN_EXTRA, 0);
4520 processSync(mapper);
4521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004522 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004524
4525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004527 ASSERT_EQ(0, motionArgs.buttonState);
4528
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4530 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4531 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4532
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4534
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535 // press BTN_STYLUS, release BTN_STYLUS
4536 processKey(mapper, BTN_STYLUS, 1);
4537 processSync(mapper);
4538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004540 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4541
4542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4544 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545
4546 processKey(mapper, BTN_STYLUS, 0);
4547 processSync(mapper);
4548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004551
4552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004554 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555
4556 // press BTN_STYLUS2, release BTN_STYLUS2
4557 processKey(mapper, BTN_STYLUS2, 1);
4558 processSync(mapper);
4559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004561 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4562
4563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4565 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566
4567 processKey(mapper, BTN_STYLUS2, 0);
4568 processSync(mapper);
4569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004570 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004572
4573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004575 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576
4577 // release touch
4578 processUp(mapper);
4579 processSync(mapper);
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4581 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4582 ASSERT_EQ(0, motionArgs.buttonState);
4583}
4584
4585TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4586 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4587 addConfigurationProperty("touch.deviceType", "touchScreen");
4588 prepareDisplay(DISPLAY_ORIENTATION_0);
4589 prepareButtons();
4590 prepareAxes(POSITION);
4591 addMapperAndConfigure(mapper);
4592
4593 NotifyMotionArgs motionArgs;
4594
4595 // default tool type is finger
4596 processDown(mapper, 100, 200);
4597 processSync(mapper);
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4599 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4601
4602 // eraser
4603 processKey(mapper, BTN_TOOL_RUBBER, 1);
4604 processSync(mapper);
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4608
4609 // stylus
4610 processKey(mapper, BTN_TOOL_RUBBER, 0);
4611 processKey(mapper, BTN_TOOL_PEN, 1);
4612 processSync(mapper);
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4616
4617 // brush
4618 processKey(mapper, BTN_TOOL_PEN, 0);
4619 processKey(mapper, BTN_TOOL_BRUSH, 1);
4620 processSync(mapper);
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4624
4625 // pencil
4626 processKey(mapper, BTN_TOOL_BRUSH, 0);
4627 processKey(mapper, BTN_TOOL_PENCIL, 1);
4628 processSync(mapper);
4629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4632
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004633 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 processKey(mapper, BTN_TOOL_PENCIL, 0);
4635 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4636 processSync(mapper);
4637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4640
4641 // mouse
4642 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4643 processKey(mapper, BTN_TOOL_MOUSE, 1);
4644 processSync(mapper);
4645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4648
4649 // lens
4650 processKey(mapper, BTN_TOOL_MOUSE, 0);
4651 processKey(mapper, BTN_TOOL_LENS, 1);
4652 processSync(mapper);
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4656
4657 // double-tap
4658 processKey(mapper, BTN_TOOL_LENS, 0);
4659 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4660 processSync(mapper);
4661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4664
4665 // triple-tap
4666 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4667 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4668 processSync(mapper);
4669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4672
4673 // quad-tap
4674 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4675 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4676 processSync(mapper);
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4680
4681 // finger
4682 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4683 processKey(mapper, BTN_TOOL_FINGER, 1);
4684 processSync(mapper);
4685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4688
4689 // stylus trumps finger
4690 processKey(mapper, BTN_TOOL_PEN, 1);
4691 processSync(mapper);
4692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4695
4696 // eraser trumps stylus
4697 processKey(mapper, BTN_TOOL_RUBBER, 1);
4698 processSync(mapper);
4699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4702
4703 // mouse trumps eraser
4704 processKey(mapper, BTN_TOOL_MOUSE, 1);
4705 processSync(mapper);
4706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4709
4710 // back to default tool type
4711 processKey(mapper, BTN_TOOL_MOUSE, 0);
4712 processKey(mapper, BTN_TOOL_RUBBER, 0);
4713 processKey(mapper, BTN_TOOL_PEN, 0);
4714 processKey(mapper, BTN_TOOL_FINGER, 0);
4715 processSync(mapper);
4716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4719}
4720
4721TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4722 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4723 addConfigurationProperty("touch.deviceType", "touchScreen");
4724 prepareDisplay(DISPLAY_ORIENTATION_0);
4725 prepareButtons();
4726 prepareAxes(POSITION);
4727 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4728 addMapperAndConfigure(mapper);
4729
4730 NotifyMotionArgs motionArgs;
4731
4732 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4733 processKey(mapper, BTN_TOOL_FINGER, 1);
4734 processMove(mapper, 100, 200);
4735 processSync(mapper);
4736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4737 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4739 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4740
4741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4742 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4744 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4745
4746 // move a little
4747 processMove(mapper, 150, 250);
4748 processSync(mapper);
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4750 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4752 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4753
4754 // down when BTN_TOUCH is pressed, pressure defaults to 1
4755 processKey(mapper, BTN_TOUCH, 1);
4756 processSync(mapper);
4757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4758 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4760 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4761
4762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4763 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4765 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4766
4767 // up when BTN_TOUCH is released, hover restored
4768 processKey(mapper, BTN_TOUCH, 0);
4769 processSync(mapper);
4770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4771 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4773 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4774
4775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4778 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4779
4780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4783 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4784
4785 // exit hover when pointer goes away
4786 processKey(mapper, BTN_TOOL_FINGER, 0);
4787 processSync(mapper);
4788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4789 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4791 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4792}
4793
4794TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4795 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4796 addConfigurationProperty("touch.deviceType", "touchScreen");
4797 prepareDisplay(DISPLAY_ORIENTATION_0);
4798 prepareButtons();
4799 prepareAxes(POSITION | PRESSURE);
4800 addMapperAndConfigure(mapper);
4801
4802 NotifyMotionArgs motionArgs;
4803
4804 // initially hovering because pressure is 0
4805 processDown(mapper, 100, 200);
4806 processPressure(mapper, 0);
4807 processSync(mapper);
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4809 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4811 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4812
4813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4814 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4816 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4817
4818 // move a little
4819 processMove(mapper, 150, 250);
4820 processSync(mapper);
4821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4822 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4824 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4825
4826 // down when pressure is non-zero
4827 processPressure(mapper, RAW_PRESSURE_MAX);
4828 processSync(mapper);
4829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4830 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4832 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4833
4834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4835 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4837 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4838
4839 // up when pressure becomes 0, hover restored
4840 processPressure(mapper, 0);
4841 processSync(mapper);
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4843 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4845 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4846
4847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4848 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4850 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4851
4852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4853 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4855 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4856
4857 // exit hover when pointer goes away
4858 processUp(mapper);
4859 processSync(mapper);
4860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4861 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4863 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4864}
4865
Dan Harmsaca28402018-12-17 13:55:20 -08004866
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867// --- MultiTouchInputMapperTest ---
4868
4869class MultiTouchInputMapperTest : public TouchInputMapperTest {
4870protected:
4871 void prepareAxes(int axes);
4872
4873 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4874 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4875 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4876 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4877 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4878 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4879 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4880 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4881 void processId(MultiTouchInputMapper* mapper, int32_t id);
4882 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4883 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4884 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4885 void processMTSync(MultiTouchInputMapper* mapper);
4886 void processSync(MultiTouchInputMapper* mapper);
4887};
4888
4889void MultiTouchInputMapperTest::prepareAxes(int axes) {
4890 if (axes & POSITION) {
4891 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4892 RAW_X_MIN, RAW_X_MAX, 0, 0);
4893 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4894 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4895 }
4896 if (axes & TOUCH) {
4897 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4898 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4899 if (axes & MINOR) {
4900 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4901 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4902 }
4903 }
4904 if (axes & TOOL) {
4905 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4906 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4907 if (axes & MINOR) {
4908 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4909 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4910 }
4911 }
4912 if (axes & ORIENTATION) {
4913 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4914 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4915 }
4916 if (axes & PRESSURE) {
4917 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4918 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4919 }
4920 if (axes & DISTANCE) {
4921 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4922 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4923 }
4924 if (axes & ID) {
4925 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4926 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4927 }
4928 if (axes & SLOT) {
4929 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4930 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4931 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4932 }
4933 if (axes & TOOL_TYPE) {
4934 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4935 0, MT_TOOL_MAX, 0, 0);
4936 }
4937}
4938
4939void MultiTouchInputMapperTest::processPosition(
4940 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004941 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4942 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943}
4944
4945void MultiTouchInputMapperTest::processTouchMajor(
4946 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004947 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948}
4949
4950void MultiTouchInputMapperTest::processTouchMinor(
4951 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004952 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953}
4954
4955void MultiTouchInputMapperTest::processToolMajor(
4956 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004957 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958}
4959
4960void MultiTouchInputMapperTest::processToolMinor(
4961 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004962 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963}
4964
4965void MultiTouchInputMapperTest::processOrientation(
4966 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004967 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968}
4969
4970void MultiTouchInputMapperTest::processPressure(
4971 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004972 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973}
4974
4975void MultiTouchInputMapperTest::processDistance(
4976 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004977 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978}
4979
4980void MultiTouchInputMapperTest::processId(
4981 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004982 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983}
4984
4985void MultiTouchInputMapperTest::processSlot(
4986 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004987 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988}
4989
4990void MultiTouchInputMapperTest::processToolType(
4991 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004992 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993}
4994
4995void MultiTouchInputMapperTest::processKey(
4996 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004997 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998}
4999
5000void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005001 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002}
5003
5004void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005005 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006}
5007
5008
5009TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
5010 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5011 addConfigurationProperty("touch.deviceType", "touchScreen");
5012 prepareDisplay(DISPLAY_ORIENTATION_0);
5013 prepareAxes(POSITION);
5014 prepareVirtualKeys();
5015 addMapperAndConfigure(mapper);
5016
5017 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5018
5019 NotifyMotionArgs motionArgs;
5020
5021 // Two fingers down at once.
5022 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5023 processPosition(mapper, x1, y1);
5024 processMTSync(mapper);
5025 processPosition(mapper, x2, y2);
5026 processMTSync(mapper);
5027 processSync(mapper);
5028
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5030 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5031 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5032 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5033 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5034 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5035 ASSERT_EQ(0, motionArgs.flags);
5036 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5037 ASSERT_EQ(0, motionArgs.buttonState);
5038 ASSERT_EQ(0, motionArgs.edgeFlags);
5039 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5040 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5043 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5044 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5045 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5046 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5047
5048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5049 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5050 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5051 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5052 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5053 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5054 motionArgs.action);
5055 ASSERT_EQ(0, motionArgs.flags);
5056 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5057 ASSERT_EQ(0, motionArgs.buttonState);
5058 ASSERT_EQ(0, motionArgs.edgeFlags);
5059 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5060 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5062 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5065 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5067 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5068 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5069 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5070 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5071
5072 // Move.
5073 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5074 processPosition(mapper, x1, y1);
5075 processMTSync(mapper);
5076 processPosition(mapper, x2, y2);
5077 processMTSync(mapper);
5078 processSync(mapper);
5079
5080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5082 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5083 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5084 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5086 ASSERT_EQ(0, motionArgs.flags);
5087 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5088 ASSERT_EQ(0, motionArgs.buttonState);
5089 ASSERT_EQ(0, motionArgs.edgeFlags);
5090 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5093 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5096 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5098 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5099 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5100 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5101 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5102
5103 // First finger up.
5104 x2 += 15; y2 -= 20;
5105 processPosition(mapper, x2, y2);
5106 processMTSync(mapper);
5107 processSync(mapper);
5108
5109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5110 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5111 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5112 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5113 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5114 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5115 motionArgs.action);
5116 ASSERT_EQ(0, motionArgs.flags);
5117 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5118 ASSERT_EQ(0, motionArgs.buttonState);
5119 ASSERT_EQ(0, motionArgs.edgeFlags);
5120 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5121 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5123 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5126 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5128 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5129 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5130 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5131 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5132
5133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5134 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5135 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5136 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5137 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5139 ASSERT_EQ(0, motionArgs.flags);
5140 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5141 ASSERT_EQ(0, motionArgs.buttonState);
5142 ASSERT_EQ(0, motionArgs.edgeFlags);
5143 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5144 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5147 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5148 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5149 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5150 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5151
5152 // Move.
5153 x2 += 20; y2 -= 25;
5154 processPosition(mapper, x2, y2);
5155 processMTSync(mapper);
5156 processSync(mapper);
5157
5158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5159 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5160 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5161 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5162 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5164 ASSERT_EQ(0, motionArgs.flags);
5165 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5166 ASSERT_EQ(0, motionArgs.buttonState);
5167 ASSERT_EQ(0, motionArgs.edgeFlags);
5168 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5169 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5172 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5173 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5174 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5175 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5176
5177 // New finger down.
5178 int32_t x3 = 700, y3 = 300;
5179 processPosition(mapper, x2, y2);
5180 processMTSync(mapper);
5181 processPosition(mapper, x3, y3);
5182 processMTSync(mapper);
5183 processSync(mapper);
5184
5185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5186 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5187 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5188 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5189 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5190 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5191 motionArgs.action);
5192 ASSERT_EQ(0, motionArgs.flags);
5193 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5194 ASSERT_EQ(0, motionArgs.buttonState);
5195 ASSERT_EQ(0, motionArgs.edgeFlags);
5196 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5197 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5199 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5202 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5204 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5205 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5206 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5207 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5208
5209 // Second finger up.
5210 x3 += 30; y3 -= 20;
5211 processPosition(mapper, x3, y3);
5212 processMTSync(mapper);
5213 processSync(mapper);
5214
5215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5217 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5218 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5219 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5220 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5221 motionArgs.action);
5222 ASSERT_EQ(0, motionArgs.flags);
5223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5224 ASSERT_EQ(0, motionArgs.buttonState);
5225 ASSERT_EQ(0, motionArgs.edgeFlags);
5226 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5229 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5232 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5234 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5235 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5236 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5238
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5240 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5241 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5242 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5243 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5245 ASSERT_EQ(0, motionArgs.flags);
5246 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5247 ASSERT_EQ(0, motionArgs.buttonState);
5248 ASSERT_EQ(0, motionArgs.edgeFlags);
5249 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5250 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5253 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5254 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5255 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5257
5258 // Last finger up.
5259 processMTSync(mapper);
5260 processSync(mapper);
5261
5262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5264 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5265 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5266 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5267 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5268 ASSERT_EQ(0, motionArgs.flags);
5269 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5270 ASSERT_EQ(0, motionArgs.buttonState);
5271 ASSERT_EQ(0, motionArgs.edgeFlags);
5272 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5273 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5276 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5277 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5278 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5280
5281 // Should not have sent any more keys or motions.
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5284}
5285
5286TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5287 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5288 addConfigurationProperty("touch.deviceType", "touchScreen");
5289 prepareDisplay(DISPLAY_ORIENTATION_0);
5290 prepareAxes(POSITION | ID);
5291 prepareVirtualKeys();
5292 addMapperAndConfigure(mapper);
5293
5294 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5295
5296 NotifyMotionArgs motionArgs;
5297
5298 // Two fingers down at once.
5299 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5300 processPosition(mapper, x1, y1);
5301 processId(mapper, 1);
5302 processMTSync(mapper);
5303 processPosition(mapper, x2, y2);
5304 processId(mapper, 2);
5305 processMTSync(mapper);
5306 processSync(mapper);
5307
5308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5309 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5310 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5311 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5314 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5315
5316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5317 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5318 motionArgs.action);
5319 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5320 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5322 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5325 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5327 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5328
5329 // Move.
5330 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5331 processPosition(mapper, x1, y1);
5332 processId(mapper, 1);
5333 processMTSync(mapper);
5334 processPosition(mapper, x2, y2);
5335 processId(mapper, 2);
5336 processMTSync(mapper);
5337 processSync(mapper);
5338
5339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5341 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5342 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5344 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5347 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5349 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5350
5351 // First finger up.
5352 x2 += 15; y2 -= 20;
5353 processPosition(mapper, x2, y2);
5354 processId(mapper, 2);
5355 processMTSync(mapper);
5356 processSync(mapper);
5357
5358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5359 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5360 motionArgs.action);
5361 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5362 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5364 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5367 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5369 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5370
5371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5373 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5374 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5377 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5378
5379 // Move.
5380 x2 += 20; y2 -= 25;
5381 processPosition(mapper, x2, y2);
5382 processId(mapper, 2);
5383 processMTSync(mapper);
5384 processSync(mapper);
5385
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5388 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5389 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5392 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5393
5394 // New finger down.
5395 int32_t x3 = 700, y3 = 300;
5396 processPosition(mapper, x2, y2);
5397 processId(mapper, 2);
5398 processMTSync(mapper);
5399 processPosition(mapper, x3, y3);
5400 processId(mapper, 3);
5401 processMTSync(mapper);
5402 processSync(mapper);
5403
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5405 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5406 motionArgs.action);
5407 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5408 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5410 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5413 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5415 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5416
5417 // Second finger up.
5418 x3 += 30; y3 -= 20;
5419 processPosition(mapper, x3, y3);
5420 processId(mapper, 3);
5421 processMTSync(mapper);
5422 processSync(mapper);
5423
5424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5425 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5426 motionArgs.action);
5427 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5428 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5430 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5433 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5435 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5436
5437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5439 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5443 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5444
5445 // Last finger up.
5446 processMTSync(mapper);
5447 processSync(mapper);
5448
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5450 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5451 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5452 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5455 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5456
5457 // Should not have sent any more keys or motions.
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5460}
5461
5462TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5463 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5464 addConfigurationProperty("touch.deviceType", "touchScreen");
5465 prepareDisplay(DISPLAY_ORIENTATION_0);
5466 prepareAxes(POSITION | ID | SLOT);
5467 prepareVirtualKeys();
5468 addMapperAndConfigure(mapper);
5469
5470 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5471
5472 NotifyMotionArgs motionArgs;
5473
5474 // Two fingers down at once.
5475 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5476 processPosition(mapper, x1, y1);
5477 processId(mapper, 1);
5478 processSlot(mapper, 1);
5479 processPosition(mapper, x2, y2);
5480 processId(mapper, 2);
5481 processSync(mapper);
5482
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5484 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5485 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5486 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5489 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5490
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5492 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5493 motionArgs.action);
5494 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5495 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5497 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5500 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5502 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5503
5504 // Move.
5505 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5506 processSlot(mapper, 0);
5507 processPosition(mapper, x1, y1);
5508 processSlot(mapper, 1);
5509 processPosition(mapper, x2, y2);
5510 processSync(mapper);
5511
5512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5514 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5515 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5517 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5520 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5522 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5523
5524 // First finger up.
5525 x2 += 15; y2 -= 20;
5526 processSlot(mapper, 0);
5527 processId(mapper, -1);
5528 processSlot(mapper, 1);
5529 processPosition(mapper, x2, y2);
5530 processSync(mapper);
5531
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5533 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5534 motionArgs.action);
5535 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5536 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5538 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5541 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5543 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5544
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5547 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5548 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5551 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5552
5553 // Move.
5554 x2 += 20; y2 -= 25;
5555 processPosition(mapper, x2, y2);
5556 processSync(mapper);
5557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5560 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5561 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5564 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5565
5566 // New finger down.
5567 int32_t x3 = 700, y3 = 300;
5568 processPosition(mapper, x2, y2);
5569 processSlot(mapper, 0);
5570 processId(mapper, 3);
5571 processPosition(mapper, x3, y3);
5572 processSync(mapper);
5573
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5575 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5576 motionArgs.action);
5577 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5578 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5580 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5583 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5585 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5586
5587 // Second finger up.
5588 x3 += 30; y3 -= 20;
5589 processSlot(mapper, 1);
5590 processId(mapper, -1);
5591 processSlot(mapper, 0);
5592 processPosition(mapper, x3, y3);
5593 processSync(mapper);
5594
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5596 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5597 motionArgs.action);
5598 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5599 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5601 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5604 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5606 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5607
5608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5610 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5611 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5614 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5615
5616 // Last finger up.
5617 processId(mapper, -1);
5618 processSync(mapper);
5619
5620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5621 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5622 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5623 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5626 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5627
5628 // Should not have sent any more keys or motions.
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5631}
5632
5633TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5634 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5635 addConfigurationProperty("touch.deviceType", "touchScreen");
5636 prepareDisplay(DISPLAY_ORIENTATION_0);
5637 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5638 addMapperAndConfigure(mapper);
5639
5640 // These calculations are based on the input device calibration documentation.
5641 int32_t rawX = 100;
5642 int32_t rawY = 200;
5643 int32_t rawTouchMajor = 7;
5644 int32_t rawTouchMinor = 6;
5645 int32_t rawToolMajor = 9;
5646 int32_t rawToolMinor = 8;
5647 int32_t rawPressure = 11;
5648 int32_t rawDistance = 0;
5649 int32_t rawOrientation = 3;
5650 int32_t id = 5;
5651
5652 float x = toDisplayX(rawX);
5653 float y = toDisplayY(rawY);
5654 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5655 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5656 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5657 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5658 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5659 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5660 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5661 float distance = float(rawDistance);
5662
5663 processPosition(mapper, rawX, rawY);
5664 processTouchMajor(mapper, rawTouchMajor);
5665 processTouchMinor(mapper, rawTouchMinor);
5666 processToolMajor(mapper, rawToolMajor);
5667 processToolMinor(mapper, rawToolMinor);
5668 processPressure(mapper, rawPressure);
5669 processOrientation(mapper, rawOrientation);
5670 processDistance(mapper, rawDistance);
5671 processId(mapper, id);
5672 processMTSync(mapper);
5673 processSync(mapper);
5674
5675 NotifyMotionArgs args;
5676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5677 ASSERT_EQ(0, args.pointerProperties[0].id);
5678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5679 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5680 orientation, distance));
5681}
5682
5683TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5684 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5685 addConfigurationProperty("touch.deviceType", "touchScreen");
5686 prepareDisplay(DISPLAY_ORIENTATION_0);
5687 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5688 addConfigurationProperty("touch.size.calibration", "geometric");
5689 addMapperAndConfigure(mapper);
5690
5691 // These calculations are based on the input device calibration documentation.
5692 int32_t rawX = 100;
5693 int32_t rawY = 200;
5694 int32_t rawTouchMajor = 140;
5695 int32_t rawTouchMinor = 120;
5696 int32_t rawToolMajor = 180;
5697 int32_t rawToolMinor = 160;
5698
5699 float x = toDisplayX(rawX);
5700 float y = toDisplayY(rawY);
5701 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5702 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5703 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5704 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5705 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5706
5707 processPosition(mapper, rawX, rawY);
5708 processTouchMajor(mapper, rawTouchMajor);
5709 processTouchMinor(mapper, rawTouchMinor);
5710 processToolMajor(mapper, rawToolMajor);
5711 processToolMinor(mapper, rawToolMinor);
5712 processMTSync(mapper);
5713 processSync(mapper);
5714
5715 NotifyMotionArgs args;
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5718 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5719}
5720
5721TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5722 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5723 addConfigurationProperty("touch.deviceType", "touchScreen");
5724 prepareDisplay(DISPLAY_ORIENTATION_0);
5725 prepareAxes(POSITION | TOUCH | TOOL);
5726 addConfigurationProperty("touch.size.calibration", "diameter");
5727 addConfigurationProperty("touch.size.scale", "10");
5728 addConfigurationProperty("touch.size.bias", "160");
5729 addConfigurationProperty("touch.size.isSummed", "1");
5730 addMapperAndConfigure(mapper);
5731
5732 // These calculations are based on the input device calibration documentation.
5733 // Note: We only provide a single common touch/tool value because the device is assumed
5734 // not to emit separate values for each pointer (isSummed = 1).
5735 int32_t rawX = 100;
5736 int32_t rawY = 200;
5737 int32_t rawX2 = 150;
5738 int32_t rawY2 = 250;
5739 int32_t rawTouchMajor = 5;
5740 int32_t rawToolMajor = 8;
5741
5742 float x = toDisplayX(rawX);
5743 float y = toDisplayY(rawY);
5744 float x2 = toDisplayX(rawX2);
5745 float y2 = toDisplayY(rawY2);
5746 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5747 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5748 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5749
5750 processPosition(mapper, rawX, rawY);
5751 processTouchMajor(mapper, rawTouchMajor);
5752 processToolMajor(mapper, rawToolMajor);
5753 processMTSync(mapper);
5754 processPosition(mapper, rawX2, rawY2);
5755 processTouchMajor(mapper, rawTouchMajor);
5756 processToolMajor(mapper, rawToolMajor);
5757 processMTSync(mapper);
5758 processSync(mapper);
5759
5760 NotifyMotionArgs args;
5761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5762 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5763
5764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5765 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5766 args.action);
5767 ASSERT_EQ(size_t(2), args.pointerCount);
5768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5769 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5771 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5772}
5773
5774TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5775 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5776 addConfigurationProperty("touch.deviceType", "touchScreen");
5777 prepareDisplay(DISPLAY_ORIENTATION_0);
5778 prepareAxes(POSITION | TOUCH | TOOL);
5779 addConfigurationProperty("touch.size.calibration", "area");
5780 addConfigurationProperty("touch.size.scale", "43");
5781 addConfigurationProperty("touch.size.bias", "3");
5782 addMapperAndConfigure(mapper);
5783
5784 // These calculations are based on the input device calibration documentation.
5785 int32_t rawX = 100;
5786 int32_t rawY = 200;
5787 int32_t rawTouchMajor = 5;
5788 int32_t rawToolMajor = 8;
5789
5790 float x = toDisplayX(rawX);
5791 float y = toDisplayY(rawY);
5792 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5793 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5794 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5795
5796 processPosition(mapper, rawX, rawY);
5797 processTouchMajor(mapper, rawTouchMajor);
5798 processToolMajor(mapper, rawToolMajor);
5799 processMTSync(mapper);
5800 processSync(mapper);
5801
5802 NotifyMotionArgs args;
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5805 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5806}
5807
5808TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5809 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5810 addConfigurationProperty("touch.deviceType", "touchScreen");
5811 prepareDisplay(DISPLAY_ORIENTATION_0);
5812 prepareAxes(POSITION | PRESSURE);
5813 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5814 addConfigurationProperty("touch.pressure.scale", "0.01");
5815 addMapperAndConfigure(mapper);
5816
Michael Wrightaa449c92017-12-13 21:21:43 +00005817 InputDeviceInfo info;
5818 mapper->populateDeviceInfo(&info);
5819 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5820 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5821 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5822
Michael Wrightd02c5b62014-02-10 15:10:22 -08005823 // These calculations are based on the input device calibration documentation.
5824 int32_t rawX = 100;
5825 int32_t rawY = 200;
5826 int32_t rawPressure = 60;
5827
5828 float x = toDisplayX(rawX);
5829 float y = toDisplayY(rawY);
5830 float pressure = float(rawPressure) * 0.01f;
5831
5832 processPosition(mapper, rawX, rawY);
5833 processPressure(mapper, rawPressure);
5834 processMTSync(mapper);
5835 processSync(mapper);
5836
5837 NotifyMotionArgs args;
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5840 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5841}
5842
5843TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5844 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5845 addConfigurationProperty("touch.deviceType", "touchScreen");
5846 prepareDisplay(DISPLAY_ORIENTATION_0);
5847 prepareAxes(POSITION | ID | SLOT);
5848 addMapperAndConfigure(mapper);
5849
5850 NotifyMotionArgs motionArgs;
5851 NotifyKeyArgs keyArgs;
5852
5853 processId(mapper, 1);
5854 processPosition(mapper, 100, 200);
5855 processSync(mapper);
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5858 ASSERT_EQ(0, motionArgs.buttonState);
5859
5860 // press BTN_LEFT, release BTN_LEFT
5861 processKey(mapper, BTN_LEFT, 1);
5862 processSync(mapper);
5863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5865 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5866
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5868 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5869 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5870
Michael Wrightd02c5b62014-02-10 15:10:22 -08005871 processKey(mapper, BTN_LEFT, 0);
5872 processSync(mapper);
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005874 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005876
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005879 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005880
5881 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5882 processKey(mapper, BTN_RIGHT, 1);
5883 processKey(mapper, BTN_MIDDLE, 1);
5884 processSync(mapper);
5885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5887 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5888 motionArgs.buttonState);
5889
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5891 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5892 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5893
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5896 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5897 motionArgs.buttonState);
5898
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899 processKey(mapper, BTN_RIGHT, 0);
5900 processSync(mapper);
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005903 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005904
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005907 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005908
5909 processKey(mapper, BTN_MIDDLE, 0);
5910 processSync(mapper);
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005914
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005917 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918
5919 // press BTN_BACK, release BTN_BACK
5920 processKey(mapper, BTN_BACK, 1);
5921 processSync(mapper);
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5923 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5924 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005925
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005928 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5929
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5932 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933
5934 processKey(mapper, BTN_BACK, 0);
5935 processSync(mapper);
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005937 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005938 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005939
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005942 ASSERT_EQ(0, motionArgs.buttonState);
5943
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5945 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5946 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5947
5948 // press BTN_SIDE, release BTN_SIDE
5949 processKey(mapper, BTN_SIDE, 1);
5950 processSync(mapper);
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5952 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5953 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005954
Michael Wrightd02c5b62014-02-10 15:10:22 -08005955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005957 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5958
5959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5960 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5961 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005962
5963 processKey(mapper, BTN_SIDE, 0);
5964 processSync(mapper);
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005968
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005971 ASSERT_EQ(0, motionArgs.buttonState);
5972
Michael Wrightd02c5b62014-02-10 15:10:22 -08005973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5974 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5975 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5976
5977 // press BTN_FORWARD, release BTN_FORWARD
5978 processKey(mapper, BTN_FORWARD, 1);
5979 processSync(mapper);
5980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5981 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5982 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005983
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005986 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5987
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5990 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991
5992 processKey(mapper, BTN_FORWARD, 0);
5993 processSync(mapper);
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005996 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005997
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006000 ASSERT_EQ(0, motionArgs.buttonState);
6001
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6003 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6004 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6005
6006 // press BTN_EXTRA, release BTN_EXTRA
6007 processKey(mapper, BTN_EXTRA, 1);
6008 processSync(mapper);
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6010 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6011 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006012
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006015 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6016
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6018 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6019 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006020
6021 processKey(mapper, BTN_EXTRA, 0);
6022 processSync(mapper);
6023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006024 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006025 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006026
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006029 ASSERT_EQ(0, motionArgs.buttonState);
6030
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6032 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6033 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6034
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6036
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037 // press BTN_STYLUS, release BTN_STYLUS
6038 processKey(mapper, BTN_STYLUS, 1);
6039 processSync(mapper);
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006042 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6043
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6046 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047
6048 processKey(mapper, BTN_STYLUS, 0);
6049 processSync(mapper);
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006051 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006053
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006056 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057
6058 // press BTN_STYLUS2, release BTN_STYLUS2
6059 processKey(mapper, BTN_STYLUS2, 1);
6060 processSync(mapper);
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6064
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6067 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068
6069 processKey(mapper, BTN_STYLUS2, 0);
6070 processSync(mapper);
6071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006074
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006077 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078
6079 // release touch
6080 processId(mapper, -1);
6081 processSync(mapper);
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6083 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6084 ASSERT_EQ(0, motionArgs.buttonState);
6085}
6086
6087TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
6088 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6089 addConfigurationProperty("touch.deviceType", "touchScreen");
6090 prepareDisplay(DISPLAY_ORIENTATION_0);
6091 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6092 addMapperAndConfigure(mapper);
6093
6094 NotifyMotionArgs motionArgs;
6095
6096 // default tool type is finger
6097 processId(mapper, 1);
6098 processPosition(mapper, 100, 200);
6099 processSync(mapper);
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6103
6104 // eraser
6105 processKey(mapper, BTN_TOOL_RUBBER, 1);
6106 processSync(mapper);
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6110
6111 // stylus
6112 processKey(mapper, BTN_TOOL_RUBBER, 0);
6113 processKey(mapper, BTN_TOOL_PEN, 1);
6114 processSync(mapper);
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6118
6119 // brush
6120 processKey(mapper, BTN_TOOL_PEN, 0);
6121 processKey(mapper, BTN_TOOL_BRUSH, 1);
6122 processSync(mapper);
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6126
6127 // pencil
6128 processKey(mapper, BTN_TOOL_BRUSH, 0);
6129 processKey(mapper, BTN_TOOL_PENCIL, 1);
6130 processSync(mapper);
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6134
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006135 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136 processKey(mapper, BTN_TOOL_PENCIL, 0);
6137 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6142
6143 // mouse
6144 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6145 processKey(mapper, BTN_TOOL_MOUSE, 1);
6146 processSync(mapper);
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6150
6151 // lens
6152 processKey(mapper, BTN_TOOL_MOUSE, 0);
6153 processKey(mapper, BTN_TOOL_LENS, 1);
6154 processSync(mapper);
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6158
6159 // double-tap
6160 processKey(mapper, BTN_TOOL_LENS, 0);
6161 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6162 processSync(mapper);
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6166
6167 // triple-tap
6168 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6169 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6170 processSync(mapper);
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6174
6175 // quad-tap
6176 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6177 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6178 processSync(mapper);
6179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6182
6183 // finger
6184 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6185 processKey(mapper, BTN_TOOL_FINGER, 1);
6186 processSync(mapper);
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6190
6191 // stylus trumps finger
6192 processKey(mapper, BTN_TOOL_PEN, 1);
6193 processSync(mapper);
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6197
6198 // eraser trumps stylus
6199 processKey(mapper, BTN_TOOL_RUBBER, 1);
6200 processSync(mapper);
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6204
6205 // mouse trumps eraser
6206 processKey(mapper, BTN_TOOL_MOUSE, 1);
6207 processSync(mapper);
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6210 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6211
6212 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6213 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6218
6219 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6220 processToolType(mapper, MT_TOOL_PEN);
6221 processSync(mapper);
6222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6225
6226 // back to default tool type
6227 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6228 processKey(mapper, BTN_TOOL_MOUSE, 0);
6229 processKey(mapper, BTN_TOOL_RUBBER, 0);
6230 processKey(mapper, BTN_TOOL_PEN, 0);
6231 processKey(mapper, BTN_TOOL_FINGER, 0);
6232 processSync(mapper);
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6236}
6237
6238TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6239 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6240 addConfigurationProperty("touch.deviceType", "touchScreen");
6241 prepareDisplay(DISPLAY_ORIENTATION_0);
6242 prepareAxes(POSITION | ID | SLOT);
6243 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6244 addMapperAndConfigure(mapper);
6245
6246 NotifyMotionArgs motionArgs;
6247
6248 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6249 processId(mapper, 1);
6250 processPosition(mapper, 100, 200);
6251 processSync(mapper);
6252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6253 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6255 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6256
6257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6258 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6260 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6261
6262 // move a little
6263 processPosition(mapper, 150, 250);
6264 processSync(mapper);
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6266 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6268 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6269
6270 // down when BTN_TOUCH is pressed, pressure defaults to 1
6271 processKey(mapper, BTN_TOUCH, 1);
6272 processSync(mapper);
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6274 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6276 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6277
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6279 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6281 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6282
6283 // up when BTN_TOUCH is released, hover restored
6284 processKey(mapper, BTN_TOUCH, 0);
6285 processSync(mapper);
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6289 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6290
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6294 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6295
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6299 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6300
6301 // exit hover when pointer goes away
6302 processId(mapper, -1);
6303 processSync(mapper);
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6305 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6307 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6308}
6309
6310TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6311 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6312 addConfigurationProperty("touch.deviceType", "touchScreen");
6313 prepareDisplay(DISPLAY_ORIENTATION_0);
6314 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6315 addMapperAndConfigure(mapper);
6316
6317 NotifyMotionArgs motionArgs;
6318
6319 // initially hovering because pressure is 0
6320 processId(mapper, 1);
6321 processPosition(mapper, 100, 200);
6322 processPressure(mapper, 0);
6323 processSync(mapper);
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6325 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6327 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6328
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6330 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6332 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6333
6334 // move a little
6335 processPosition(mapper, 150, 250);
6336 processSync(mapper);
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6340 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6341
6342 // down when pressure becomes non-zero
6343 processPressure(mapper, RAW_PRESSURE_MAX);
6344 processSync(mapper);
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6348 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6349
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6353 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6354
6355 // up when pressure becomes 0, hover restored
6356 processPressure(mapper, 0);
6357 processSync(mapper);
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6361 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6362
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6364 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6366 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6367
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6371 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6372
6373 // exit hover when pointer goes away
6374 processId(mapper, -1);
6375 processSync(mapper);
6376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6379 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6380}
6381
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006382/**
6383 * Set the input device port <--> display port associations, and check that the
6384 * events are routed to the display that matches the display port.
6385 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6386 */
6387TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6388 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6389 const std::string usb2 = "USB2";
6390 const uint8_t hdmi1 = 0;
6391 const uint8_t hdmi2 = 1;
6392 const std::string secondaryUniqueId = "uniqueId2";
6393 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6394
6395 addConfigurationProperty("touch.deviceType", "touchScreen");
6396 prepareAxes(POSITION);
6397 addMapperAndConfigure(mapper);
6398
6399 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6400 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6401
6402 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6403 // for this input device is specified, and the matching viewport is not present,
6404 // the input device should be disabled (at the mapper level).
6405
6406 // Add viewport for display 2 on hdmi2
6407 prepareSecondaryDisplay(type, hdmi2);
6408 // Send a touch event
6409 processPosition(mapper, 100, 100);
6410 processSync(mapper);
6411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6412
6413 // Add viewport for display 1 on hdmi1
6414 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6415 // Send a touch event again
6416 processPosition(mapper, 100, 100);
6417 processSync(mapper);
6418
6419 NotifyMotionArgs args;
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6421 ASSERT_EQ(DISPLAY_ID, args.displayId);
6422}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423
Arthur Hung41a712e2018-11-22 19:41:03 +08006424/**
6425 * Expect fallback to internal viewport if device is external and external viewport is not present.
6426 */
6427TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6428 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6429 prepareAxes(POSITION);
6430 addConfigurationProperty("touch.deviceType", "touchScreen");
6431 prepareDisplay(DISPLAY_ORIENTATION_0);
6432 mDevice->setExternal(true);
6433 addMapperAndConfigure(mapper);
6434
6435 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6436
6437 NotifyMotionArgs motionArgs;
6438
6439 // Expect the event to be sent to the internal viewport,
6440 // because an external viewport is not present.
6441 processPosition(mapper, 100, 100);
6442 processSync(mapper);
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6444 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6445
6446 // Expect the event to be sent to the external viewport if it is present.
6447 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6448 processPosition(mapper, 100, 100);
6449 processSync(mapper);
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6452}
6453
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006454TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6455 // Setup PointerController for second display.
6456 sp<FakePointerController> fakePointerController = new FakePointerController();
6457 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6458 fakePointerController->setPosition(100, 200);
6459 fakePointerController->setButtonState(0);
6460 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6461 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6462
6463 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6464 prepareDisplay(DISPLAY_ORIENTATION_0);
6465 prepareAxes(POSITION);
6466 addMapperAndConfigure(mapper);
6467
6468 // Check source is mouse that would obtain the PointerController.
6469 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6470
6471 NotifyMotionArgs motionArgs;
6472 processPosition(mapper, 100, 100);
6473 processSync(mapper);
6474
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6477 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6478}
6479
Arthur Hung7c645402019-01-25 17:45:42 +08006480TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6481 // Setup the first touch screen device.
6482 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6483 prepareAxes(POSITION | ID | SLOT);
6484 addConfigurationProperty("touch.deviceType", "touchScreen");
6485 addMapperAndConfigure(mapper);
6486
6487 // Create the second touch screen device, and enable multi fingers.
6488 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006489 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006490 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006491 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006492 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006493 std::unique_ptr<InputDevice> device2 =
6494 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6495 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006496 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6497 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6498 0 /*flat*/, 0 /*fuzz*/);
6499 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6500 0 /*flat*/, 0 /*fuzz*/);
6501 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6502 0 /*flat*/, 0 /*fuzz*/);
6503 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6504 0 /*flat*/, 0 /*fuzz*/);
6505 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6506 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6507 String8("touchScreen"));
6508
6509 // Setup the second touch screen device.
Arthur Hung2c9a3342019-07-23 14:18:59 +08006510 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2.get());
Arthur Hung7c645402019-01-25 17:45:42 +08006511 device2->addMapper(mapper2);
6512 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6513 device2->reset(ARBITRARY_TIME);
6514
6515 // Setup PointerController.
6516 sp<FakePointerController> fakePointerController = new FakePointerController();
6517 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6518 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6519
6520 // Setup policy for associated displays and show touches.
6521 const uint8_t hdmi1 = 0;
6522 const uint8_t hdmi2 = 1;
6523 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6524 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6525 mFakePolicy->setShowTouches(true);
6526
6527 // Create displays.
6528 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6529 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6530
6531 // Default device will reconfigure above, need additional reconfiguration for another device.
6532 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6533 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6534
6535 // Two fingers down at default display.
6536 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6537 processPosition(mapper, x1, y1);
6538 processId(mapper, 1);
6539 processSlot(mapper, 1);
6540 processPosition(mapper, x2, y2);
6541 processId(mapper, 2);
6542 processSync(mapper);
6543
6544 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6545 fakePointerController->getSpots().find(DISPLAY_ID);
6546 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6547 ASSERT_EQ(size_t(2), iter->second.size());
6548
6549 // Two fingers down at second display.
6550 processPosition(mapper2, x1, y1);
6551 processId(mapper2, 1);
6552 processSlot(mapper2, 1);
6553 processPosition(mapper2, x2, y2);
6554 processId(mapper2, 2);
6555 processSync(mapper2);
6556
6557 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6558 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6559 ASSERT_EQ(size_t(2), iter->second.size());
6560}
6561
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006562TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6563 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6564 prepareAxes(POSITION);
6565 addConfigurationProperty("touch.deviceType", "touchScreen");
6566 prepareDisplay(DISPLAY_ORIENTATION_0);
6567 addMapperAndConfigure(mapper);
6568
6569 NotifyMotionArgs motionArgs;
6570 // Unrotated video frame
6571 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6572 std::vector<TouchVideoFrame> frames{frame};
6573 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6574 processPosition(mapper, 100, 200);
6575 processSync(mapper);
6576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6577 ASSERT_EQ(frames, motionArgs.videoFrames);
6578
6579 // Subsequent touch events should not have any videoframes
6580 // This is implemented separately in FakeEventHub,
6581 // but that should match the behaviour of TouchVideoDevice.
6582 processPosition(mapper, 200, 200);
6583 processSync(mapper);
6584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6585 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6586}
6587
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006588TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6589 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6590 prepareAxes(POSITION);
6591 addConfigurationProperty("touch.deviceType", "touchScreen");
6592 addMapperAndConfigure(mapper);
6593 // Unrotated video frame
6594 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6595 NotifyMotionArgs motionArgs;
6596
6597 // Test all 4 orientations
6598 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6599 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6600 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6601 clearViewports();
6602 prepareDisplay(orientation);
6603 std::vector<TouchVideoFrame> frames{frame};
6604 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6605 processPosition(mapper, 100, 200);
6606 processSync(mapper);
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6608 frames[0].rotate(orientation);
6609 ASSERT_EQ(frames, motionArgs.videoFrames);
6610 }
6611}
6612
6613TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6614 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6615 prepareAxes(POSITION);
6616 addConfigurationProperty("touch.deviceType", "touchScreen");
6617 addMapperAndConfigure(mapper);
6618 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6619 // so mix these.
6620 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6621 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6622 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6623 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6624 NotifyMotionArgs motionArgs;
6625
6626 prepareDisplay(DISPLAY_ORIENTATION_90);
6627 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6628 processPosition(mapper, 100, 200);
6629 processSync(mapper);
6630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6631 std::for_each(frames.begin(), frames.end(),
6632 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6633 ASSERT_EQ(frames, motionArgs.videoFrames);
6634}
6635
Arthur Hung9da14732019-09-02 16:16:58 +08006636/**
6637 * If we had defined port associations, but the viewport is not ready, the touch device would be
6638 * expected to be disabled, and it should be enabled after the viewport has found.
6639 */
6640TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
6641 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6642 constexpr uint8_t hdmi2 = 1;
6643 const std::string secondaryUniqueId = "uniqueId2";
6644 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6645
6646 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6647
6648 addConfigurationProperty("touch.deviceType", "touchScreen");
6649 prepareAxes(POSITION);
6650 addMapperAndConfigure(mapper);
6651
6652 ASSERT_EQ(mDevice->isEnabled(), false);
6653
6654 // Add display on hdmi2, the device should be enabled and can receive touch event.
6655 prepareSecondaryDisplay(type, hdmi2);
6656 ASSERT_EQ(mDevice->isEnabled(), true);
6657
6658 // Send a touch event.
6659 processPosition(mapper, 100, 100);
6660 processSync(mapper);
6661
6662 NotifyMotionArgs args;
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6664 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6665}
6666
Arthur Hung6cd19a42019-08-30 19:04:12 +08006667/**
6668 * Test touch should not work if outside of surface.
6669 */
6670TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
6671 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6672 addConfigurationProperty("touch.deviceType", "touchScreen");
6673 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006674 prepareAxes(POSITION);
6675 addMapperAndConfigure(mapper);
6676
Arthur Hung05de5772019-09-26 18:31:26 +08006677 // Touch on left-top area should work.
6678 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6679 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6680 processPosition(mapper, rawX, rawY);
6681 processSync(mapper);
6682
6683 NotifyMotionArgs args;
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6685
6686 // Reset.
6687 mapper->reset(ARBITRARY_TIME);
6688
6689 // Let logical display be different to physical display and rotate 90-degrees.
6690 std::optional<DisplayViewport> internalViewport =
6691 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6692 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6693 internalViewport->logicalLeft = 0;
6694 internalViewport->logicalTop = 0;
6695 internalViewport->logicalRight = DISPLAY_HEIGHT;
6696 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6697
6698 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6699 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6700 internalViewport->physicalRight = DISPLAY_HEIGHT;
6701 internalViewport->physicalBottom = DISPLAY_WIDTH;
6702
6703 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6704 internalViewport->deviceHeight = DISPLAY_WIDTH;
6705 mFakePolicy->updateViewport(internalViewport.value());
6706 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6707
6708 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006709 processPosition(mapper, rawX, rawY);
6710 processSync(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6712}
6713
Michael Wrightd02c5b62014-02-10 15:10:22 -08006714} // namespace android