blob: d6624c9f5a5a131d3e9c9729ae98248da7a4ee3e [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
Prabir Pradhan28efc192019-11-05 01:10:04 +00001116 // Make the protected loopOnce method accessible to tests.
1117 using InputReader::loopOnce;
1118
Michael Wrightd02c5b62014-02-10 15:10:22 -08001119protected:
1120 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001121 const InputDeviceIdentifier& identifier,
1122 uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 if (mNextDevice) {
1124 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001125 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126 return device;
1127 }
1128 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1129 }
1130
1131 friend class InputReaderTest;
1132};
1133
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001134// --- InputReaderPolicyTest ---
1135class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001136protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001137 sp<FakeInputReaderPolicy> mFakePolicy;
1138
Prabir Pradhan28efc192019-11-05 01:10:04 +00001139 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1140 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001141};
1142
1143/**
1144 * Check that empty set of viewports is an acceptable configuration.
1145 * Also try to get internal viewport two different ways - by type and by uniqueId.
1146 *
1147 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1148 * Such configuration is not currently allowed.
1149 */
1150TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001151 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001152
1153 // We didn't add any viewports yet, so there shouldn't be any.
1154 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001155 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001156 ASSERT_FALSE(internalViewport);
1157
1158 // Add an internal viewport, then clear it
1159 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001160 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001161
1162 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001163 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001164 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001165 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001166
1167 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001168 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001169 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001170 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001171
1172 mFakePolicy->clearViewports();
1173 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001174 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001175 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001176 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001177 ASSERT_FALSE(internalViewport);
1178}
1179
1180TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1181 const std::string internalUniqueId = "local:0";
1182 const std::string externalUniqueId = "local:1";
1183 const std::string virtualUniqueId1 = "virtual:2";
1184 const std::string virtualUniqueId2 = "virtual:3";
1185 constexpr int32_t virtualDisplayId1 = 2;
1186 constexpr int32_t virtualDisplayId2 = 3;
1187
1188 // Add an internal viewport
1189 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001190 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001191 // Add an external viewport
1192 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001193 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001194 // Add an virtual viewport
1195 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001196 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001197 // Add another virtual viewport
1198 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001199 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001200
1201 // Check matching by type for internal
1202 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001203 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001204 ASSERT_TRUE(internalViewport);
1205 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1206
1207 // Check matching by type for external
1208 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001209 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210 ASSERT_TRUE(externalViewport);
1211 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1212
1213 // Check matching by uniqueId for virtual viewport #1
1214 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001215 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001216 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001217 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001218 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1219 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1220
1221 // Check matching by uniqueId for virtual viewport #2
1222 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001223 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001224 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001225 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001226 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1227 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1228}
1229
1230
1231/**
1232 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1233 * that lookup works by checking display id.
1234 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1235 */
1236TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1237 const std::string uniqueId1 = "uniqueId1";
1238 const std::string uniqueId2 = "uniqueId2";
1239 constexpr int32_t displayId1 = 2;
1240 constexpr int32_t displayId2 = 3;
1241
1242 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1243 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1244 for (const ViewportType& type : types) {
1245 mFakePolicy->clearViewports();
1246 // Add a viewport
1247 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001248 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001249 // Add another viewport
1250 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001251 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001252
1253 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001254 std::optional<DisplayViewport> viewport1 =
1255 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001256 ASSERT_TRUE(viewport1);
1257 ASSERT_EQ(displayId1, viewport1->displayId);
1258 ASSERT_EQ(type, viewport1->type);
1259
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001260 std::optional<DisplayViewport> viewport2 =
1261 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001262 ASSERT_TRUE(viewport2);
1263 ASSERT_EQ(displayId2, viewport2->displayId);
1264 ASSERT_EQ(type, viewport2->type);
1265
1266 // When there are multiple viewports of the same kind, and uniqueId is not specified
1267 // in the call to getDisplayViewport, then that situation is not supported.
1268 // The viewports can be stored in any order, so we cannot rely on the order, since that
1269 // is just implementation detail.
1270 // However, we can check that it still returns *a* viewport, we just cannot assert
1271 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001272 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001273 ASSERT_TRUE(someViewport);
1274 }
1275}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001276
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001277/**
1278 * Check getDisplayViewportByPort
1279 */
1280TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1281 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1282 const std::string uniqueId1 = "uniqueId1";
1283 const std::string uniqueId2 = "uniqueId2";
1284 constexpr int32_t displayId1 = 1;
1285 constexpr int32_t displayId2 = 2;
1286 const uint8_t hdmi1 = 0;
1287 const uint8_t hdmi2 = 1;
1288 const uint8_t hdmi3 = 2;
1289
1290 mFakePolicy->clearViewports();
1291 // Add a viewport that's associated with some display port that's not of interest.
1292 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1293 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1294 // Add another viewport, connected to HDMI1 port
1295 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1296 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1297
1298 // Check that correct display viewport was returned by comparing the display ports.
1299 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1300 ASSERT_TRUE(hdmi1Viewport);
1301 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1302 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1303
1304 // Check that we can still get the same viewport using the uniqueId
1305 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1306 ASSERT_TRUE(hdmi1Viewport);
1307 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1308 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1309 ASSERT_EQ(type, hdmi1Viewport->type);
1310
1311 // Check that we cannot find a port with "HDMI2", because we never added one
1312 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1313 ASSERT_FALSE(hdmi2Viewport);
1314}
1315
Michael Wrightd02c5b62014-02-10 15:10:22 -08001316// --- InputReaderTest ---
1317
1318class InputReaderTest : public testing::Test {
1319protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001320 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001322 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001323 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324
Prabir Pradhan28efc192019-11-05 01:10:04 +00001325 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001326 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001327 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001328 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001329
Prabir Pradhan28efc192019-11-05 01:10:04 +00001330 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1331 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001332 }
1333
Prabir Pradhan28efc192019-11-05 01:10:04 +00001334 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001335 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();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001347 mReader->loopOnce();
1348 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001349 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1350 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001351 }
1352
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001353 void disableDevice(int32_t deviceId, InputDevice* device) {
1354 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001355 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001356 }
1357
1358 void enableDevice(int32_t deviceId, InputDevice* device) {
1359 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001360 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001361 }
1362
Michael Wrightd02c5b62014-02-10 15:10:22 -08001363 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001364 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365 const PropertyMap* configuration) {
1366 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1367 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1368 device->addMapper(mapper);
1369 mReader->setNextDevice(device);
1370 addDevice(deviceId, name, classes, configuration);
1371 return mapper;
1372 }
1373};
1374
1375TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001376 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001377 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001378 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001379 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001381 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001382 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001383 ASSERT_EQ(1U, inputDevices.size());
1384 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001385 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1387 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1388 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1389
1390 // Should also have received a notification describing the new input devices.
1391 inputDevices = mFakePolicy->getInputDevices();
1392 ASSERT_EQ(1U, inputDevices.size());
1393 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001394 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001395 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1396 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1397 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1398}
1399
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001400TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1401 constexpr int32_t deviceId = 1;
1402 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001403 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001404 // Must add at least one mapper or the device will be ignored!
1405 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1406 device->addMapper(mapper);
1407 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001408 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001409
Yi Kong9b14ac62018-07-17 13:48:38 -07001410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001411
1412 NotifyDeviceResetArgs resetArgs;
1413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001414 ASSERT_EQ(deviceId, resetArgs.deviceId);
1415
1416 ASSERT_EQ(device->isEnabled(), true);
1417 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001418 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001419
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001421 ASSERT_EQ(deviceId, resetArgs.deviceId);
1422 ASSERT_EQ(device->isEnabled(), false);
1423
1424 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001425 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001428 ASSERT_EQ(device->isEnabled(), false);
1429
1430 enableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001431 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001433 ASSERT_EQ(deviceId, resetArgs.deviceId);
1434 ASSERT_EQ(device->isEnabled(), true);
1435}
1436
Michael Wrightd02c5b62014-02-10 15:10:22 -08001437TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001438 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001439 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001440 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001441 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1442
1443 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1444 AINPUT_SOURCE_ANY, AKEYCODE_A))
1445 << "Should return unknown when the device id is >= 0 but unknown.";
1446
1447 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1448 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1449 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1450
1451 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1452 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1453 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1454
1455 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1456 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1457 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1458
1459 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1460 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1461 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1462}
1463
1464TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001465 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001466 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001467 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001468 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1469
1470 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1471 AINPUT_SOURCE_ANY, KEY_A))
1472 << "Should return unknown when the device id is >= 0 but unknown.";
1473
1474 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1475 AINPUT_SOURCE_TRACKBALL, KEY_A))
1476 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1477
1478 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1479 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1480 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1481
1482 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1483 AINPUT_SOURCE_TRACKBALL, KEY_A))
1484 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1485
1486 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1487 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1488 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1489}
1490
1491TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001492 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001493 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001494 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001495 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1496
1497 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1498 AINPUT_SOURCE_ANY, SW_LID))
1499 << "Should return unknown when the device id is >= 0 but unknown.";
1500
1501 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1502 AINPUT_SOURCE_TRACKBALL, SW_LID))
1503 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1504
1505 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1506 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1507 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1508
1509 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1510 AINPUT_SOURCE_TRACKBALL, SW_LID))
1511 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1512
1513 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1514 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1515 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1516}
1517
1518TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001519 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001520 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001521 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001522
Michael Wrightd02c5b62014-02-10 15:10:22 -08001523 mapper->addSupportedKeyCode(AKEYCODE_A);
1524 mapper->addSupportedKeyCode(AKEYCODE_B);
1525
1526 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1527 uint8_t flags[4] = { 0, 0, 0, 1 };
1528
1529 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1530 << "Should return false when device id is >= 0 but unknown.";
1531 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1532
1533 flags[3] = 1;
1534 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1535 << "Should return false when device id is valid but the sources are not supported by the device.";
1536 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1537
1538 flags[3] = 1;
1539 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1540 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1541 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1542
1543 flags[3] = 1;
1544 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1545 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1546 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1547
1548 flags[3] = 1;
1549 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1550 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1551 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1552}
1553
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001554TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001555 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001556
1557 NotifyConfigurationChangedArgs args;
1558
1559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1560 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1561}
1562
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001563TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001564 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001565 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001566 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567
1568 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001569 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1571
1572 RawEvent event;
1573 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1574 ASSERT_EQ(0, event.when);
1575 ASSERT_EQ(1, event.deviceId);
1576 ASSERT_EQ(EV_KEY, event.type);
1577 ASSERT_EQ(KEY_A, event.code);
1578 ASSERT_EQ(1, event.value);
1579}
1580
Prabir Pradhan42611e02018-11-27 14:04:02 -08001581TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1582 constexpr int32_t deviceId = 1;
1583 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001584 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001585 // Must add at least one mapper or the device will be ignored!
1586 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1587 device->addMapper(mapper);
1588 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001589 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001590
1591 NotifyDeviceResetArgs resetArgs;
1592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1593 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1594
1595 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001596 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001598 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1599 prevSequenceNum = resetArgs.sequenceNum;
1600
1601 enableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001602 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001604 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1605 prevSequenceNum = resetArgs.sequenceNum;
1606
1607 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001608 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001610 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1611 prevSequenceNum = resetArgs.sequenceNum;
1612}
1613
Arthur Hungc23540e2018-11-29 20:42:11 +08001614TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1615 constexpr int32_t deviceId = 1;
1616 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1617 const char* DEVICE_LOCATION = "USB1";
1618 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1619 DEVICE_LOCATION);
1620 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1621 device->addMapper(mapper);
1622 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001623
1624 const uint8_t hdmi1 = 1;
1625
1626 // Associated touch screen with second display.
1627 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1628
1629 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001630 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001631 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1632 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1633 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1634 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1635 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001636 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001637
1638 // Add the device, and make sure all of the callbacks are triggered.
1639 // The device is added after the input port associations are processed since
1640 // we do not yet support dynamic device-to-display associations.
1641 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
1644 ASSERT_NO_FATAL_FAILURE(mapper->assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001645
Arthur Hung2c9a3342019-07-23 14:18:59 +08001646 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001647 ASSERT_EQ(deviceId, device->getId());
1648 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1649 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001650
1651 // Can't dispatch event from a disabled device.
1652 disableDevice(deviceId, device);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001653 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001654 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001655}
1656
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657
1658// --- InputDeviceTest ---
1659
1660class InputDeviceTest : public testing::Test {
1661protected:
1662 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001663 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664 static const int32_t DEVICE_ID;
1665 static const int32_t DEVICE_GENERATION;
1666 static const int32_t DEVICE_CONTROLLER_NUMBER;
1667 static const uint32_t DEVICE_CLASSES;
1668
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001669 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001670 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001671 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001672 FakeInputReaderContext* mFakeContext;
1673
1674 InputDevice* mDevice;
1675
Prabir Pradhan28efc192019-11-05 01:10:04 +00001676 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001677 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001678 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001679 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001680 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1681
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001682 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001683 InputDeviceIdentifier identifier;
1684 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001685 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001686 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1687 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1688 }
1689
Prabir Pradhan28efc192019-11-05 01:10:04 +00001690 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001691 delete mDevice;
1692
1693 delete mFakeContext;
1694 mFakeListener.clear();
1695 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001696 }
1697};
1698
1699const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001700const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001701const int32_t InputDeviceTest::DEVICE_ID = 1;
1702const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1703const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1704const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1705 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1706
1707TEST_F(InputDeviceTest, ImmutableProperties) {
1708 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001709 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001710 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1711}
1712
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001713TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1714 ASSERT_EQ(mDevice->isEnabled(), true);
1715}
1716
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1718 // Configuration.
1719 InputReaderConfiguration config;
1720 mDevice->configure(ARBITRARY_TIME, &config, 0);
1721
1722 // Reset.
1723 mDevice->reset(ARBITRARY_TIME);
1724
1725 NotifyDeviceResetArgs resetArgs;
1726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1727 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1728 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1729
1730 // Metadata.
1731 ASSERT_TRUE(mDevice->isIgnored());
1732 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1733
1734 InputDeviceInfo info;
1735 mDevice->getDeviceInfo(&info);
1736 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001737 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001738 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1739 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1740
1741 // State queries.
1742 ASSERT_EQ(0, mDevice->getMetaState());
1743
1744 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1745 << "Ignored device should return unknown key code state.";
1746 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1747 << "Ignored device should return unknown scan code state.";
1748 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1749 << "Ignored device should return unknown switch state.";
1750
1751 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1752 uint8_t flags[2] = { 0, 1 };
1753 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1754 << "Ignored device should never mark any key codes.";
1755 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1756 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1757}
1758
1759TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1760 // Configuration.
1761 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1762
1763 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1764 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1765 mapper1->setMetaState(AMETA_ALT_ON);
1766 mapper1->addSupportedKeyCode(AKEYCODE_A);
1767 mapper1->addSupportedKeyCode(AKEYCODE_B);
1768 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1769 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1770 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1771 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1772 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1773 mDevice->addMapper(mapper1);
1774
1775 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1776 mapper2->setMetaState(AMETA_SHIFT_ON);
1777 mDevice->addMapper(mapper2);
1778
1779 InputReaderConfiguration config;
1780 mDevice->configure(ARBITRARY_TIME, &config, 0);
1781
1782 String8 propertyValue;
1783 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1784 << "Device should have read configuration during configuration phase.";
1785 ASSERT_STREQ("value", propertyValue.string());
1786
1787 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1788 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1789
1790 // Reset
1791 mDevice->reset(ARBITRARY_TIME);
1792 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1793 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1794
1795 NotifyDeviceResetArgs resetArgs;
1796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1797 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1798 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1799
1800 // Metadata.
1801 ASSERT_FALSE(mDevice->isIgnored());
1802 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1803
1804 InputDeviceInfo info;
1805 mDevice->getDeviceInfo(&info);
1806 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001807 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001808 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1809 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1810
1811 // State queries.
1812 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1813 << "Should query mappers and combine meta states.";
1814
1815 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1816 << "Should return unknown key code state when source not supported.";
1817 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1818 << "Should return unknown scan code state when source not supported.";
1819 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1820 << "Should return unknown switch state when source not supported.";
1821
1822 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1823 << "Should query mapper when source is supported.";
1824 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1825 << "Should query mapper when source is supported.";
1826 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1827 << "Should query mapper when source is supported.";
1828
1829 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1830 uint8_t flags[4] = { 0, 0, 0, 1 };
1831 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1832 << "Should do nothing when source is unsupported.";
1833 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1834 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1835 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1836 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1837
1838 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1839 << "Should query mapper when source is supported.";
1840 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1841 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1842 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1843 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1844
1845 // Event handling.
1846 RawEvent event;
1847 mDevice->process(&event, 1);
1848
1849 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1850 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1851}
1852
Arthur Hung2c9a3342019-07-23 14:18:59 +08001853// A single input device is associated with a specific display. Check that:
1854// 1. Device is disabled if the viewport corresponding to the associated display is not found
1855// 2. Device is disabled when setEnabled API is called
1856TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
1857 FakeInputMapper* mapper = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1858 mDevice->addMapper(mapper);
1859
1860 // First Configuration.
1861 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1862
1863 // Device should be enabled by default.
1864 ASSERT_TRUE(mDevice->isEnabled());
1865
1866 // Prepare associated info.
1867 constexpr uint8_t hdmi = 1;
1868 const std::string UNIQUE_ID = "local:1";
1869
1870 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1871 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1872 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1873 // Device should be disabled because it is associated with a specific display via
1874 // input port <-> display port association, but the corresponding display is not found
1875 ASSERT_FALSE(mDevice->isEnabled());
1876
1877 // Prepare displays.
1878 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1879 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1880 ViewportType::VIEWPORT_INTERNAL);
1881 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1882 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1883 ASSERT_TRUE(mDevice->isEnabled());
1884
1885 // Device should be disabled after set disable.
1886 mFakePolicy->addDisabledDevice(mDevice->getId());
1887 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1888 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1889 ASSERT_FALSE(mDevice->isEnabled());
1890
1891 // Device should still be disabled even found the associated display.
1892 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1893 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1894 ASSERT_FALSE(mDevice->isEnabled());
1895}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896
1897// --- InputMapperTest ---
1898
1899class InputMapperTest : public testing::Test {
1900protected:
1901 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001902 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001903 static const int32_t DEVICE_ID;
1904 static const int32_t DEVICE_GENERATION;
1905 static const int32_t DEVICE_CONTROLLER_NUMBER;
1906 static const uint32_t DEVICE_CLASSES;
1907
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001908 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001909 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001910 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001911 FakeInputReaderContext* mFakeContext;
1912 InputDevice* mDevice;
1913
Prabir Pradhan28efc192019-11-05 01:10:04 +00001914 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001915 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001916 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001917 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1919 InputDeviceIdentifier identifier;
1920 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001921 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001922 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1923 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1924
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001925 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001926 }
1927
Prabir Pradhan28efc192019-11-05 01:10:04 +00001928 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001929 delete mDevice;
1930 delete mFakeContext;
1931 mFakeListener.clear();
1932 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001933 }
1934
1935 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001936 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001937 }
1938
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001939 void configureDevice(uint32_t changes) {
1940 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1941 }
1942
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943 void addMapperAndConfigure(InputMapper* mapper) {
1944 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001945 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001946 mDevice->reset(ARBITRARY_TIME);
1947 }
1948
1949 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001950 int32_t orientation, const std::string& uniqueId,
1951 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001952 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001953 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001954 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1955 }
1956
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001957 void clearViewports() {
1958 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001959 }
1960
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001961 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001962 int32_t code, int32_t value) {
1963 RawEvent event;
1964 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001965 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001966 event.type = type;
1967 event.code = code;
1968 event.value = value;
1969 mapper->process(&event);
1970 }
1971
1972 static void assertMotionRange(const InputDeviceInfo& info,
1973 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1974 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001975 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001976 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1977 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1978 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1979 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1980 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1981 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1982 }
1983
1984 static void assertPointerCoords(const PointerCoords& coords,
1985 float x, float y, float pressure, float size,
1986 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1987 float orientation, float distance) {
1988 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1989 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1990 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1991 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1992 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1993 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1994 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1995 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1996 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1997 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1998 }
1999
2000 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2001 float actualX, actualY;
2002 controller->getPosition(&actualX, &actualY);
2003 ASSERT_NEAR(x, actualX, 1);
2004 ASSERT_NEAR(y, actualY, 1);
2005 }
2006};
2007
2008const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002009const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002010const int32_t InputMapperTest::DEVICE_ID = 1;
2011const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2012const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2013const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
2014
2015
2016// --- SwitchInputMapperTest ---
2017
2018class SwitchInputMapperTest : public InputMapperTest {
2019protected:
2020};
2021
2022TEST_F(SwitchInputMapperTest, GetSources) {
2023 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2024 addMapperAndConfigure(mapper);
2025
2026 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
2027}
2028
2029TEST_F(SwitchInputMapperTest, GetSwitchState) {
2030 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2031 addMapperAndConfigure(mapper);
2032
2033 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
2034 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2035
2036 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
2037 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2038}
2039
2040TEST_F(SwitchInputMapperTest, Process) {
2041 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2042 addMapperAndConfigure(mapper);
2043
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002044 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2045 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2046 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2047 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002048
2049 NotifySwitchArgs args;
2050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2051 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002052 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2053 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002054 args.switchMask);
2055 ASSERT_EQ(uint32_t(0), args.policyFlags);
2056}
2057
2058
2059// --- KeyboardInputMapperTest ---
2060
2061class KeyboardInputMapperTest : public InputMapperTest {
2062protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002063 const std::string UNIQUE_ID = "local:0";
2064
2065 void prepareDisplay(int32_t orientation);
2066
Arthur Hung2c9a3342019-07-23 14:18:59 +08002067 void testDPadKeyRotation(KeyboardInputMapper* mapper, int32_t originalScanCode,
2068 int32_t originalKeyCode, int32_t rotatedKeyCode,
2069 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002070};
2071
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002072/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2073 * orientation.
2074 */
2075void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2076 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002077 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002078}
2079
Michael Wrightd02c5b62014-02-10 15:10:22 -08002080void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002081 int32_t originalScanCode, int32_t originalKeyCode,
2082 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002083 NotifyKeyArgs args;
2084
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002085 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2087 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2088 ASSERT_EQ(originalScanCode, args.scanCode);
2089 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002090 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002091
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002092 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2094 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2095 ASSERT_EQ(originalScanCode, args.scanCode);
2096 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002097 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002098}
2099
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100TEST_F(KeyboardInputMapperTest, GetSources) {
2101 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2102 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2103 addMapperAndConfigure(mapper);
2104
2105 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
2106}
2107
2108TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2109 const int32_t USAGE_A = 0x070004;
2110 const int32_t USAGE_UNKNOWN = 0x07ffff;
2111 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2112 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2113
2114 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2115 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2116 addMapperAndConfigure(mapper);
2117
2118 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002119 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002120 NotifyKeyArgs args;
2121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2122 ASSERT_EQ(DEVICE_ID, args.deviceId);
2123 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2124 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2125 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2126 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2127 ASSERT_EQ(KEY_HOME, args.scanCode);
2128 ASSERT_EQ(AMETA_NONE, args.metaState);
2129 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2130 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2131 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2132
2133 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002134 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2136 ASSERT_EQ(DEVICE_ID, args.deviceId);
2137 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2138 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2139 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2140 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2141 ASSERT_EQ(KEY_HOME, args.scanCode);
2142 ASSERT_EQ(AMETA_NONE, args.metaState);
2143 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2144 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2145 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2146
2147 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002148 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2149 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2151 ASSERT_EQ(DEVICE_ID, args.deviceId);
2152 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2153 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2154 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2155 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2156 ASSERT_EQ(0, args.scanCode);
2157 ASSERT_EQ(AMETA_NONE, args.metaState);
2158 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2159 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2160 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2161
2162 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002163 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2164 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2166 ASSERT_EQ(DEVICE_ID, args.deviceId);
2167 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2168 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2169 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2170 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2171 ASSERT_EQ(0, args.scanCode);
2172 ASSERT_EQ(AMETA_NONE, args.metaState);
2173 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2174 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2175 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2176
2177 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002178 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2179 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2181 ASSERT_EQ(DEVICE_ID, args.deviceId);
2182 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2183 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2184 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2185 ASSERT_EQ(0, args.keyCode);
2186 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2187 ASSERT_EQ(AMETA_NONE, args.metaState);
2188 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2189 ASSERT_EQ(0U, args.policyFlags);
2190 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2191
2192 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002193 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2194 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2196 ASSERT_EQ(DEVICE_ID, args.deviceId);
2197 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2198 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2199 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2200 ASSERT_EQ(0, args.keyCode);
2201 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2202 ASSERT_EQ(AMETA_NONE, args.metaState);
2203 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2204 ASSERT_EQ(0U, args.policyFlags);
2205 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2206}
2207
2208TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2209 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2210 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2211
2212 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2213 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2214 addMapperAndConfigure(mapper);
2215
2216 // Initial metastate.
2217 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2218
2219 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002220 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002221 NotifyKeyArgs args;
2222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2224 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2225 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2226
2227 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002228 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2230 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2231 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2232
2233 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002234 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2236 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2237 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2238
2239 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002240 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2242 ASSERT_EQ(AMETA_NONE, args.metaState);
2243 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2244 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2245}
2246
2247TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2248 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2249 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2250 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2251 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2252
2253 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2254 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2255 addMapperAndConfigure(mapper);
2256
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002257 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002258 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2259 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2260 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2261 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2262 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2263 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2264 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2265 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2266}
2267
2268TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2269 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2270 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2271 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2272 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2273
2274 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2275 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2276 addConfigurationProperty("keyboard.orientationAware", "1");
2277 addMapperAndConfigure(mapper);
2278
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002279 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002280 ASSERT_NO_FATAL_FAILURE(
2281 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2282 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2283 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2284 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2285 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2286 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2287 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002289 clearViewports();
2290 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002291 ASSERT_NO_FATAL_FAILURE(
2292 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2293 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2294 AKEYCODE_DPAD_UP, DISPLAY_ID));
2295 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2296 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2297 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2298 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002299
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002300 clearViewports();
2301 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002302 ASSERT_NO_FATAL_FAILURE(
2303 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2304 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2305 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2306 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2307 AKEYCODE_DPAD_UP, DISPLAY_ID));
2308 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2309 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002310
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002311 clearViewports();
2312 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002313 ASSERT_NO_FATAL_FAILURE(
2314 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2315 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2316 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2317 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2318 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2319 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2320 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002321
2322 // Special case: if orientation changes while key is down, we still emit the same keycode
2323 // in the key up as we did in the key down.
2324 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002325 clearViewports();
2326 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002327 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2329 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2330 ASSERT_EQ(KEY_UP, args.scanCode);
2331 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2332
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002333 clearViewports();
2334 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002335 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2337 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2338 ASSERT_EQ(KEY_UP, args.scanCode);
2339 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2340}
2341
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002342TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2343 // If the keyboard is not orientation aware,
2344 // key events should not be associated with a specific display id
2345 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2346
2347 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2348 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2349 addMapperAndConfigure(mapper);
2350 NotifyKeyArgs args;
2351
2352 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002353 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002355 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2357 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2358
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002359 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002360 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002362 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2364 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2365}
2366
2367TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2368 // If the keyboard is orientation aware,
2369 // key events should be associated with the internal viewport
2370 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2371
2372 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2373 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2374 addConfigurationProperty("keyboard.orientationAware", "1");
2375 addMapperAndConfigure(mapper);
2376 NotifyKeyArgs args;
2377
2378 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2379 // ^--- already checked by the previous test
2380
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002381 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002382 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002383 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002385 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2387 ASSERT_EQ(DISPLAY_ID, args.displayId);
2388
2389 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002390 clearViewports();
2391 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002392 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002393 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002395 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2397 ASSERT_EQ(newDisplayId, args.displayId);
2398}
2399
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2401 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2402 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2403 addMapperAndConfigure(mapper);
2404
2405 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2406 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2407
2408 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2409 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2410}
2411
2412TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2413 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2414 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2415 addMapperAndConfigure(mapper);
2416
2417 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2418 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2419
2420 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2421 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2422}
2423
2424TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2425 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2426 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2427 addMapperAndConfigure(mapper);
2428
2429 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2430
2431 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2432 uint8_t flags[2] = { 0, 0 };
2433 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2434 ASSERT_TRUE(flags[0]);
2435 ASSERT_FALSE(flags[1]);
2436}
2437
2438TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2439 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2440 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2441 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2442 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2443 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2444 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2445
2446 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2447 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2448 addMapperAndConfigure(mapper);
2449
2450 // Initialization should have turned all of the lights off.
2451 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2452 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2453 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2454
2455 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002456 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2457 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2459 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2460 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2461 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2462
2463 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002464 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2465 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002466 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2467 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2468 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2469 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2470
2471 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002472 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2473 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2475 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2476 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2477 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2478
2479 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002480 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2481 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002482 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2483 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2484 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2485 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2486
2487 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002488 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2489 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002490 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2491 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2492 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2493 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2494
2495 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002496 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2497 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002498 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2499 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2500 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2501 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2502}
2503
Arthur Hung2c9a3342019-07-23 14:18:59 +08002504TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2505 // keyboard 1.
2506 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2507 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2508 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2509 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2510
2511 // keyboard 2.
2512 const std::string USB2 = "USB2";
2513 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2514 InputDeviceIdentifier identifier;
2515 identifier.name = "KEYBOARD2";
2516 identifier.location = USB2;
2517 std::unique_ptr<InputDevice> device2 =
2518 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2519 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2520 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2521 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2522 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2523 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2524 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2525
2526 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2527 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2528 addMapperAndConfigure(mapper);
2529
2530 KeyboardInputMapper* mapper2 = new KeyboardInputMapper(device2.get(), AINPUT_SOURCE_KEYBOARD,
2531 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2532 device2->addMapper(mapper2);
2533 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2534 device2->reset(ARBITRARY_TIME);
2535
2536 // Prepared displays and associated info.
2537 constexpr uint8_t hdmi1 = 0;
2538 constexpr uint8_t hdmi2 = 1;
2539 const std::string SECONDARY_UNIQUE_ID = "local:1";
2540
2541 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2542 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2543
2544 // No associated display viewport found, should disable the device.
2545 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2546 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2547 ASSERT_FALSE(device2->isEnabled());
2548
2549 // Prepare second display.
2550 constexpr int32_t newDisplayId = 2;
2551 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2552 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2553 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2554 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2555 // Default device will reconfigure above, need additional reconfiguration for another device.
2556 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2557 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2558
2559 // Device should be enabled after the associated display is found.
2560 ASSERT_TRUE(mDevice->isEnabled());
2561 ASSERT_TRUE(device2->isEnabled());
2562
2563 // Test pad key events
2564 ASSERT_NO_FATAL_FAILURE(
2565 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2567 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2568 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2569 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2570 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2571 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2572
2573 ASSERT_NO_FATAL_FAILURE(
2574 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2575 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2576 AKEYCODE_DPAD_RIGHT, newDisplayId));
2577 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2578 AKEYCODE_DPAD_DOWN, newDisplayId));
2579 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2580 AKEYCODE_DPAD_LEFT, newDisplayId));
2581}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002582
2583// --- CursorInputMapperTest ---
2584
2585class CursorInputMapperTest : public InputMapperTest {
2586protected:
2587 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2588
2589 sp<FakePointerController> mFakePointerController;
2590
Prabir Pradhan28efc192019-11-05 01:10:04 +00002591 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002592 InputMapperTest::SetUp();
2593
2594 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002595 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002596 }
2597
2598 void testMotionRotation(CursorInputMapper* mapper,
2599 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002600
2601 void prepareDisplay(int32_t orientation) {
2602 const std::string uniqueId = "local:0";
2603 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2604 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2605 orientation, uniqueId, NO_PORT, viewportType);
2606 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607};
2608
2609const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2610
2611void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2612 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2613 NotifyMotionArgs args;
2614
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002615 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2616 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2617 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2621 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2622 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2623 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2624}
2625
2626TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2627 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2628 addConfigurationProperty("cursor.mode", "pointer");
2629 addMapperAndConfigure(mapper);
2630
2631 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2632}
2633
2634TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2635 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2636 addConfigurationProperty("cursor.mode", "navigation");
2637 addMapperAndConfigure(mapper);
2638
2639 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2640}
2641
2642TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2643 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2644 addConfigurationProperty("cursor.mode", "pointer");
2645 addMapperAndConfigure(mapper);
2646
2647 InputDeviceInfo info;
2648 mapper->populateDeviceInfo(&info);
2649
2650 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002651 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2652 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2654 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2655
2656 // When the bounds are set, then there should be a valid motion range.
2657 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2658
2659 InputDeviceInfo info2;
2660 mapper->populateDeviceInfo(&info2);
2661
2662 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2663 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2664 1, 800 - 1, 0.0f, 0.0f));
2665 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2666 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2667 2, 480 - 1, 0.0f, 0.0f));
2668 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2669 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2670 0.0f, 1.0f, 0.0f, 0.0f));
2671}
2672
2673TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2674 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2675 addConfigurationProperty("cursor.mode", "navigation");
2676 addMapperAndConfigure(mapper);
2677
2678 InputDeviceInfo info;
2679 mapper->populateDeviceInfo(&info);
2680
2681 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2682 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2683 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2684 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2685 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2686 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2687 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2688 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2689 0.0f, 1.0f, 0.0f, 0.0f));
2690}
2691
2692TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2693 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2694 addConfigurationProperty("cursor.mode", "navigation");
2695 addMapperAndConfigure(mapper);
2696
2697 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2698
2699 NotifyMotionArgs args;
2700
2701 // Button press.
2702 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002703 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2704 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2706 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2707 ASSERT_EQ(DEVICE_ID, args.deviceId);
2708 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2709 ASSERT_EQ(uint32_t(0), args.policyFlags);
2710 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2711 ASSERT_EQ(0, args.flags);
2712 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2713 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2714 ASSERT_EQ(0, args.edgeFlags);
2715 ASSERT_EQ(uint32_t(1), args.pointerCount);
2716 ASSERT_EQ(0, args.pointerProperties[0].id);
2717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2719 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2720 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2721 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2722 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2723
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2725 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2726 ASSERT_EQ(DEVICE_ID, args.deviceId);
2727 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2728 ASSERT_EQ(uint32_t(0), args.policyFlags);
2729 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2730 ASSERT_EQ(0, args.flags);
2731 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2732 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2733 ASSERT_EQ(0, args.edgeFlags);
2734 ASSERT_EQ(uint32_t(1), args.pointerCount);
2735 ASSERT_EQ(0, args.pointerProperties[0].id);
2736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2738 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2739 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2740 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2741 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2742
Michael Wrightd02c5b62014-02-10 15:10:22 -08002743 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002744 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2745 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2747 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2748 ASSERT_EQ(DEVICE_ID, args.deviceId);
2749 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2750 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002751 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2752 ASSERT_EQ(0, args.flags);
2753 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2754 ASSERT_EQ(0, args.buttonState);
2755 ASSERT_EQ(0, args.edgeFlags);
2756 ASSERT_EQ(uint32_t(1), args.pointerCount);
2757 ASSERT_EQ(0, args.pointerProperties[0].id);
2758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2760 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2761 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2762 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2763 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2764
2765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2766 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2767 ASSERT_EQ(DEVICE_ID, args.deviceId);
2768 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2769 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2771 ASSERT_EQ(0, args.flags);
2772 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2773 ASSERT_EQ(0, args.buttonState);
2774 ASSERT_EQ(0, args.edgeFlags);
2775 ASSERT_EQ(uint32_t(1), args.pointerCount);
2776 ASSERT_EQ(0, args.pointerProperties[0].id);
2777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2779 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2780 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2781 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2782 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2783}
2784
2785TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2786 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2787 addConfigurationProperty("cursor.mode", "navigation");
2788 addMapperAndConfigure(mapper);
2789
2790 NotifyMotionArgs args;
2791
2792 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002793 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2794 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2798 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2799
2800 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002801 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2802 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2806 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2807}
2808
2809TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2810 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2811 addConfigurationProperty("cursor.mode", "navigation");
2812 addMapperAndConfigure(mapper);
2813
2814 NotifyMotionArgs args;
2815
2816 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002817 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2818 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2820 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2822 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2823
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2825 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2827 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2828
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002830 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2831 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2835 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2836
2837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2840 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2841}
2842
2843TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2844 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2845 addConfigurationProperty("cursor.mode", "navigation");
2846 addMapperAndConfigure(mapper);
2847
2848 NotifyMotionArgs args;
2849
2850 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002851 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2852 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2853 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2854 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2856 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2858 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2859 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2860
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2864 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2865 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2866
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002868 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2869 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
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));
2872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2874 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2875 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2876
2877 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002878 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2879 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002881 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2883 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2884
2885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002886 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2888 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2889}
2890
2891TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2892 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2893 addConfigurationProperty("cursor.mode", "navigation");
2894 addMapperAndConfigure(mapper);
2895
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002896 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2898 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2899 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2900 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2901 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2902 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2903 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2904 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2905}
2906
2907TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2908 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2909 addConfigurationProperty("cursor.mode", "navigation");
2910 addConfigurationProperty("cursor.orientationAware", "1");
2911 addMapperAndConfigure(mapper);
2912
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002913 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002914 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2915 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2916 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2917 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2918 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2919 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2920 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2921 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2922
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002923 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2925 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2926 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2927 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2928 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2929 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2930 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2931 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2932
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002933 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2935 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2936 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2937 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2938 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2939 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2940 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2941 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2942
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002943 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2945 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2946 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2947 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2948 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2949 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2950 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2951 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2952}
2953
2954TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2955 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2956 addConfigurationProperty("cursor.mode", "pointer");
2957 addMapperAndConfigure(mapper);
2958
2959 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2960 mFakePointerController->setPosition(100, 200);
2961 mFakePointerController->setButtonState(0);
2962
2963 NotifyMotionArgs motionArgs;
2964 NotifyKeyArgs keyArgs;
2965
2966 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002967 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2968 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2970 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2971 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2972 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2974 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2975
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2977 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2978 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2979 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2981 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2982
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002983 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2984 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987 ASSERT_EQ(0, motionArgs.buttonState);
2988 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2990 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2991
2992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002993 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 ASSERT_EQ(0, motionArgs.buttonState);
2995 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2997 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2998
2999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003001 ASSERT_EQ(0, motionArgs.buttonState);
3002 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3004 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3005
3006 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003007 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3008 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3009 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3011 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3012 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3013 motionArgs.buttonState);
3014 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3015 mFakePointerController->getButtonState());
3016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3017 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3018
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3020 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3021 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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
3027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3029 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3030 motionArgs.buttonState);
3031 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3032 mFakePointerController->getButtonState());
3033 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
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003036 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3037 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3041 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3043 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3044
3045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003047 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3048 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3050 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3051
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003052 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3053 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3056 ASSERT_EQ(0, motionArgs.buttonState);
3057 ASSERT_EQ(0, mFakePointerController->getButtonState());
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));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003060 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3061 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003062
3063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064 ASSERT_EQ(0, motionArgs.buttonState);
3065 ASSERT_EQ(0, mFakePointerController->getButtonState());
3066 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3068 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 -08003069
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3071 ASSERT_EQ(0, motionArgs.buttonState);
3072 ASSERT_EQ(0, mFakePointerController->getButtonState());
3073 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3075 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3076
3077 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003078 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3079 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3081 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3082 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003083
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003085 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3087 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3089 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3090
3091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3093 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3094 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -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
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003098 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3099 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 ASSERT_EQ(0, motionArgs.buttonState);
3103 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3105 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3106
3107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003109 ASSERT_EQ(0, motionArgs.buttonState);
3110 ASSERT_EQ(0, mFakePointerController->getButtonState());
3111
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3113 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3115 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3116 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3117
3118 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003119 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3120 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3122 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3123 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003124
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003126 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3128 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3130 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3131
3132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3134 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3135 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, 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));
3138
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003139 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3140 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 ASSERT_EQ(0, motionArgs.buttonState);
3144 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3146 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 -08003147
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3149 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3150 ASSERT_EQ(0, motionArgs.buttonState);
3151 ASSERT_EQ(0, mFakePointerController->getButtonState());
3152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3153 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3154
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3156 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3157 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3158
3159 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003160 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3161 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3163 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3164 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003165
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003167 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3169 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3171 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3172
3173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3175 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3176 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3178 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3179
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003180 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3181 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 ASSERT_EQ(0, motionArgs.buttonState);
3185 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3187 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003188
3189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3190 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3191 ASSERT_EQ(0, motionArgs.buttonState);
3192 ASSERT_EQ(0, mFakePointerController->getButtonState());
3193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3194 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3195
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3197 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3198 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3199
3200 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003201 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3202 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3204 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3205 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003206
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003208 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3210 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3212 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3213
3214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3216 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3217 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3219 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3220
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003221 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3222 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225 ASSERT_EQ(0, motionArgs.buttonState);
3226 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3228 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003229
3230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3231 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3232 ASSERT_EQ(0, motionArgs.buttonState);
3233 ASSERT_EQ(0, mFakePointerController->getButtonState());
3234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3235 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3236
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3238 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3239 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3240}
3241
3242TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3243 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3244 addConfigurationProperty("cursor.mode", "pointer");
3245 addMapperAndConfigure(mapper);
3246
3247 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3248 mFakePointerController->setPosition(100, 200);
3249 mFakePointerController->setButtonState(0);
3250
3251 NotifyMotionArgs args;
3252
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003253 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3254 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3255 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003257 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3258 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3260 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3261 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3262}
3263
3264TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3265 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3266 addConfigurationProperty("cursor.mode", "pointer");
3267 mFakePolicy->setPointerCapture(true);
3268 addMapperAndConfigure(mapper);
3269
3270 NotifyDeviceResetArgs resetArgs;
3271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3272 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3273 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3274
3275 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3276 mFakePointerController->setPosition(100, 200);
3277 mFakePointerController->setButtonState(0);
3278
3279 NotifyMotionArgs args;
3280
3281 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003282 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3283 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3284 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3286 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3289 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3290 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3291
3292 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003293 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3294 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3296 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3297 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3299 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3301 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3304 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3305
3306 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003307 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3308 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3310 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3311 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3313 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3315 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3316 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3318 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3319
3320 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003321 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3322 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3323 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3325 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3328 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3329 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3330
3331 // Disable pointer capture and check that the device generation got bumped
3332 // and events are generated the usual way.
3333 const uint32_t generation = mFakeContext->getGeneration();
3334 mFakePolicy->setPointerCapture(false);
3335 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3336 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3337
3338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3339 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3340 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3341
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003342 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3343 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3344 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3346 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3349 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3350 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3351}
3352
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003353TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3354 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3355 addMapperAndConfigure(mapper);
3356
3357 // Setup PointerController for second display.
3358 constexpr int32_t SECOND_DISPLAY_ID = 1;
3359 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3360 mFakePointerController->setPosition(100, 200);
3361 mFakePointerController->setButtonState(0);
3362 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3363
3364 NotifyMotionArgs args;
3365 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3366 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3367 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3369 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3372 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3373 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3374 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3375}
3376
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377
3378// --- TouchInputMapperTest ---
3379
3380class TouchInputMapperTest : public InputMapperTest {
3381protected:
3382 static const int32_t RAW_X_MIN;
3383 static const int32_t RAW_X_MAX;
3384 static const int32_t RAW_Y_MIN;
3385 static const int32_t RAW_Y_MAX;
3386 static const int32_t RAW_TOUCH_MIN;
3387 static const int32_t RAW_TOUCH_MAX;
3388 static const int32_t RAW_TOOL_MIN;
3389 static const int32_t RAW_TOOL_MAX;
3390 static const int32_t RAW_PRESSURE_MIN;
3391 static const int32_t RAW_PRESSURE_MAX;
3392 static const int32_t RAW_ORIENTATION_MIN;
3393 static const int32_t RAW_ORIENTATION_MAX;
3394 static const int32_t RAW_DISTANCE_MIN;
3395 static const int32_t RAW_DISTANCE_MAX;
3396 static const int32_t RAW_TILT_MIN;
3397 static const int32_t RAW_TILT_MAX;
3398 static const int32_t RAW_ID_MIN;
3399 static const int32_t RAW_ID_MAX;
3400 static const int32_t RAW_SLOT_MIN;
3401 static const int32_t RAW_SLOT_MAX;
3402 static const float X_PRECISION;
3403 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003404 static const float X_PRECISION_VIRTUAL;
3405 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406
3407 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003408 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409
3410 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3411
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003412 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003413 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003414
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415 enum Axes {
3416 POSITION = 1 << 0,
3417 TOUCH = 1 << 1,
3418 TOOL = 1 << 2,
3419 PRESSURE = 1 << 3,
3420 ORIENTATION = 1 << 4,
3421 MINOR = 1 << 5,
3422 ID = 1 << 6,
3423 DISTANCE = 1 << 7,
3424 TILT = 1 << 8,
3425 SLOT = 1 << 9,
3426 TOOL_TYPE = 1 << 10,
3427 };
3428
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003429 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3430 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003431 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003433 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434 int32_t toRawX(float displayX);
3435 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003436 float toCookedX(float rawX, float rawY);
3437 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003439 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003441 float toDisplayY(int32_t rawY, int32_t displayHeight);
3442
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443};
3444
3445const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3446const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3447const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3448const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3449const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3450const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3451const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3452const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003453const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3454const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3456const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3457const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3458const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3459const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3460const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3461const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3462const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3463const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3464const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3465const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3466const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003467const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3468 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3469const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3470 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003471const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3472 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003473
3474const float TouchInputMapperTest::GEOMETRIC_SCALE =
3475 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3476 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3477
3478const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3479 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3480 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3481};
3482
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003483void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003484 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003485 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3486}
3487
3488void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3489 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3490 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491}
3492
Santos Cordonfa5cf462017-04-05 10:37:00 -07003493void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003494 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3495 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003496 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003497}
3498
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499void TouchInputMapperTest::prepareVirtualKeys() {
3500 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3501 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3502 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3503 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3504}
3505
Jason Gerecke489fda82012-09-07 17:19:40 -07003506void TouchInputMapperTest::prepareLocationCalibration() {
3507 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3508}
3509
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510int32_t TouchInputMapperTest::toRawX(float displayX) {
3511 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3512}
3513
3514int32_t TouchInputMapperTest::toRawY(float displayY) {
3515 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3516}
3517
Jason Gerecke489fda82012-09-07 17:19:40 -07003518float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3519 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3520 return rawX;
3521}
3522
3523float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3524 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3525 return rawY;
3526}
3527
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003529 return toDisplayX(rawX, DISPLAY_WIDTH);
3530}
3531
3532float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3533 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534}
3535
3536float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003537 return toDisplayY(rawY, DISPLAY_HEIGHT);
3538}
3539
3540float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3541 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542}
3543
3544
3545// --- SingleTouchInputMapperTest ---
3546
3547class SingleTouchInputMapperTest : public TouchInputMapperTest {
3548protected:
3549 void prepareButtons();
3550 void prepareAxes(int axes);
3551
3552 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3553 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3554 void processUp(SingleTouchInputMapper* mappery);
3555 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3556 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3557 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3558 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3559 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3560 void processSync(SingleTouchInputMapper* mapper);
3561};
3562
3563void SingleTouchInputMapperTest::prepareButtons() {
3564 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3565}
3566
3567void SingleTouchInputMapperTest::prepareAxes(int axes) {
3568 if (axes & POSITION) {
3569 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3570 RAW_X_MIN, RAW_X_MAX, 0, 0);
3571 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3572 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3573 }
3574 if (axes & PRESSURE) {
3575 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3576 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3577 }
3578 if (axes & TOOL) {
3579 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3580 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3581 }
3582 if (axes & DISTANCE) {
3583 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3584 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3585 }
3586 if (axes & TILT) {
3587 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3588 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3589 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3590 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3591 }
3592}
3593
3594void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003595 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3596 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3597 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598}
3599
3600void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003601 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3602 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603}
3604
3605void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003606 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607}
3608
3609void SingleTouchInputMapperTest::processPressure(
3610 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003611 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612}
3613
3614void SingleTouchInputMapperTest::processToolMajor(
3615 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003616 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617}
3618
3619void SingleTouchInputMapperTest::processDistance(
3620 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003621 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622}
3623
3624void SingleTouchInputMapperTest::processTilt(
3625 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003626 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3627 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628}
3629
3630void SingleTouchInputMapperTest::processKey(
3631 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003632 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003633}
3634
3635void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003636 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003637}
3638
3639
3640TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3641 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3642 prepareButtons();
3643 prepareAxes(POSITION);
3644 addMapperAndConfigure(mapper);
3645
3646 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3647}
3648
3649TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3650 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3651 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3652 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3653 prepareButtons();
3654 prepareAxes(POSITION);
3655 addMapperAndConfigure(mapper);
3656
3657 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3658}
3659
3660TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3661 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3662 prepareButtons();
3663 prepareAxes(POSITION);
3664 addConfigurationProperty("touch.deviceType", "touchPad");
3665 addMapperAndConfigure(mapper);
3666
3667 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3668}
3669
3670TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3671 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3672 prepareButtons();
3673 prepareAxes(POSITION);
3674 addConfigurationProperty("touch.deviceType", "touchScreen");
3675 addMapperAndConfigure(mapper);
3676
3677 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3678}
3679
3680TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3681 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3682 addConfigurationProperty("touch.deviceType", "touchScreen");
3683 prepareDisplay(DISPLAY_ORIENTATION_0);
3684 prepareButtons();
3685 prepareAxes(POSITION);
3686 prepareVirtualKeys();
3687 addMapperAndConfigure(mapper);
3688
3689 // Unknown key.
3690 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3691
3692 // Virtual key is down.
3693 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3694 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3695 processDown(mapper, x, y);
3696 processSync(mapper);
3697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3698
3699 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3700
3701 // Virtual key is up.
3702 processUp(mapper);
3703 processSync(mapper);
3704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3705
3706 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3707}
3708
3709TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3710 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3711 addConfigurationProperty("touch.deviceType", "touchScreen");
3712 prepareDisplay(DISPLAY_ORIENTATION_0);
3713 prepareButtons();
3714 prepareAxes(POSITION);
3715 prepareVirtualKeys();
3716 addMapperAndConfigure(mapper);
3717
3718 // Unknown key.
3719 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3720
3721 // Virtual key is down.
3722 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3723 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3724 processDown(mapper, x, y);
3725 processSync(mapper);
3726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3727
3728 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3729
3730 // Virtual key is up.
3731 processUp(mapper);
3732 processSync(mapper);
3733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3734
3735 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3736}
3737
3738TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3739 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3740 addConfigurationProperty("touch.deviceType", "touchScreen");
3741 prepareDisplay(DISPLAY_ORIENTATION_0);
3742 prepareButtons();
3743 prepareAxes(POSITION);
3744 prepareVirtualKeys();
3745 addMapperAndConfigure(mapper);
3746
3747 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3748 uint8_t flags[2] = { 0, 0 };
3749 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3750 ASSERT_TRUE(flags[0]);
3751 ASSERT_FALSE(flags[1]);
3752}
3753
3754TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3755 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3756 addConfigurationProperty("touch.deviceType", "touchScreen");
3757 prepareDisplay(DISPLAY_ORIENTATION_0);
3758 prepareButtons();
3759 prepareAxes(POSITION);
3760 prepareVirtualKeys();
3761 addMapperAndConfigure(mapper);
3762
3763 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3764
3765 NotifyKeyArgs args;
3766
3767 // Press virtual key.
3768 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3769 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3770 processDown(mapper, x, y);
3771 processSync(mapper);
3772
3773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3774 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3775 ASSERT_EQ(DEVICE_ID, args.deviceId);
3776 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3777 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3778 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3779 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3780 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3781 ASSERT_EQ(KEY_HOME, args.scanCode);
3782 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3783 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3784
3785 // Release virtual key.
3786 processUp(mapper);
3787 processSync(mapper);
3788
3789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3790 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3791 ASSERT_EQ(DEVICE_ID, args.deviceId);
3792 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3793 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3794 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3795 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3796 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3797 ASSERT_EQ(KEY_HOME, args.scanCode);
3798 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3799 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3800
3801 // Should not have sent any motions.
3802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3803}
3804
3805TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3806 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3807 addConfigurationProperty("touch.deviceType", "touchScreen");
3808 prepareDisplay(DISPLAY_ORIENTATION_0);
3809 prepareButtons();
3810 prepareAxes(POSITION);
3811 prepareVirtualKeys();
3812 addMapperAndConfigure(mapper);
3813
3814 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3815
3816 NotifyKeyArgs keyArgs;
3817
3818 // Press virtual key.
3819 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3820 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3821 processDown(mapper, x, y);
3822 processSync(mapper);
3823
3824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3825 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3826 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3827 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3828 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3829 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3830 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3831 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3832 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3833 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3834 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3835
3836 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3837 // into the display area.
3838 y -= 100;
3839 processMove(mapper, x, y);
3840 processSync(mapper);
3841
3842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3843 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3844 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3845 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3846 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3847 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3848 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3849 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3850 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3851 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3852 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3853 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3854
3855 NotifyMotionArgs motionArgs;
3856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3857 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3858 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3859 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3860 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3862 ASSERT_EQ(0, motionArgs.flags);
3863 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3864 ASSERT_EQ(0, motionArgs.buttonState);
3865 ASSERT_EQ(0, motionArgs.edgeFlags);
3866 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3867 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3870 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3871 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3872 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3874
3875 // Keep moving out of bounds. Should generate a pointer move.
3876 y -= 50;
3877 processMove(mapper, x, y);
3878 processSync(mapper);
3879
3880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3881 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3882 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3883 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3884 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3886 ASSERT_EQ(0, motionArgs.flags);
3887 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3888 ASSERT_EQ(0, motionArgs.buttonState);
3889 ASSERT_EQ(0, motionArgs.edgeFlags);
3890 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3891 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3892 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3894 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3895 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3896 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3897 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3898
3899 // Release out of bounds. Should generate a pointer up.
3900 processUp(mapper);
3901 processSync(mapper);
3902
3903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3904 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3905 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3906 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3907 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3908 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3909 ASSERT_EQ(0, motionArgs.flags);
3910 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3911 ASSERT_EQ(0, motionArgs.buttonState);
3912 ASSERT_EQ(0, motionArgs.edgeFlags);
3913 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3914 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3915 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3917 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3918 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3919 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3920 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3921
3922 // Should not have sent any more keys or motions.
3923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3925}
3926
3927TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3928 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3929 addConfigurationProperty("touch.deviceType", "touchScreen");
3930 prepareDisplay(DISPLAY_ORIENTATION_0);
3931 prepareButtons();
3932 prepareAxes(POSITION);
3933 prepareVirtualKeys();
3934 addMapperAndConfigure(mapper);
3935
3936 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3937
3938 NotifyMotionArgs motionArgs;
3939
3940 // Initially go down out of bounds.
3941 int32_t x = -10;
3942 int32_t y = -10;
3943 processDown(mapper, x, y);
3944 processSync(mapper);
3945
3946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3947
3948 // Move into the display area. Should generate a pointer down.
3949 x = 50;
3950 y = 75;
3951 processMove(mapper, x, y);
3952 processSync(mapper);
3953
3954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3955 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3956 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3957 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3958 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3959 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3960 ASSERT_EQ(0, motionArgs.flags);
3961 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3962 ASSERT_EQ(0, motionArgs.buttonState);
3963 ASSERT_EQ(0, motionArgs.edgeFlags);
3964 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3965 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3968 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3969 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3970 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3971 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3972
3973 // Release. Should generate a pointer up.
3974 processUp(mapper);
3975 processSync(mapper);
3976
3977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3978 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3979 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3980 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3981 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3982 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3983 ASSERT_EQ(0, motionArgs.flags);
3984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3985 ASSERT_EQ(0, motionArgs.buttonState);
3986 ASSERT_EQ(0, motionArgs.edgeFlags);
3987 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3988 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3991 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3992 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3993 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3994 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3995
3996 // Should not have sent any more keys or motions.
3997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3999}
4000
Santos Cordonfa5cf462017-04-05 10:37:00 -07004001TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
4002 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4003 addConfigurationProperty("touch.deviceType", "touchScreen");
4004 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4005
4006 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4007 prepareButtons();
4008 prepareAxes(POSITION);
4009 prepareVirtualKeys();
4010 addMapperAndConfigure(mapper);
4011
4012 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4013
4014 NotifyMotionArgs motionArgs;
4015
4016 // Down.
4017 int32_t x = 100;
4018 int32_t y = 125;
4019 processDown(mapper, x, y);
4020 processSync(mapper);
4021
4022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4023 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4024 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4025 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4026 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4027 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4028 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4029 ASSERT_EQ(0, motionArgs.flags);
4030 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4031 ASSERT_EQ(0, motionArgs.buttonState);
4032 ASSERT_EQ(0, motionArgs.edgeFlags);
4033 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4034 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4037 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4038 1, 0, 0, 0, 0, 0, 0, 0));
4039 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4040 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4041 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4042
4043 // Move.
4044 x += 50;
4045 y += 75;
4046 processMove(mapper, x, y);
4047 processSync(mapper);
4048
4049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4050 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4051 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4052 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4053 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4054 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4056 ASSERT_EQ(0, motionArgs.flags);
4057 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4058 ASSERT_EQ(0, motionArgs.buttonState);
4059 ASSERT_EQ(0, motionArgs.edgeFlags);
4060 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4061 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4064 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4065 1, 0, 0, 0, 0, 0, 0, 0));
4066 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4067 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4068 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4069
4070 // Up.
4071 processUp(mapper);
4072 processSync(mapper);
4073
4074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4075 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4076 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4077 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4078 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4079 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4081 ASSERT_EQ(0, motionArgs.flags);
4082 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4083 ASSERT_EQ(0, motionArgs.buttonState);
4084 ASSERT_EQ(0, motionArgs.edgeFlags);
4085 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4086 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4089 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4090 1, 0, 0, 0, 0, 0, 0, 0));
4091 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4092 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4093 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4094
4095 // Should not have sent any more keys or motions.
4096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4098}
4099
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
4101 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4102 addConfigurationProperty("touch.deviceType", "touchScreen");
4103 prepareDisplay(DISPLAY_ORIENTATION_0);
4104 prepareButtons();
4105 prepareAxes(POSITION);
4106 prepareVirtualKeys();
4107 addMapperAndConfigure(mapper);
4108
4109 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4110
4111 NotifyMotionArgs motionArgs;
4112
4113 // Down.
4114 int32_t x = 100;
4115 int32_t y = 125;
4116 processDown(mapper, x, y);
4117 processSync(mapper);
4118
4119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4120 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4121 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4122 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4123 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4124 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4125 ASSERT_EQ(0, motionArgs.flags);
4126 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4127 ASSERT_EQ(0, motionArgs.buttonState);
4128 ASSERT_EQ(0, motionArgs.edgeFlags);
4129 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4130 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4132 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4133 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4134 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4135 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4136 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4137
4138 // Move.
4139 x += 50;
4140 y += 75;
4141 processMove(mapper, x, y);
4142 processSync(mapper);
4143
4144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4145 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4146 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4147 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4148 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4150 ASSERT_EQ(0, motionArgs.flags);
4151 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4152 ASSERT_EQ(0, motionArgs.buttonState);
4153 ASSERT_EQ(0, motionArgs.edgeFlags);
4154 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4155 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4158 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4159 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4160 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4161 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4162
4163 // Up.
4164 processUp(mapper);
4165 processSync(mapper);
4166
4167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4168 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4169 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4170 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4171 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4172 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4173 ASSERT_EQ(0, motionArgs.flags);
4174 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4175 ASSERT_EQ(0, motionArgs.buttonState);
4176 ASSERT_EQ(0, motionArgs.edgeFlags);
4177 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4178 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4181 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4182 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4183 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4185
4186 // Should not have sent any more keys or motions.
4187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4189}
4190
4191TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
4192 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4193 addConfigurationProperty("touch.deviceType", "touchScreen");
4194 prepareButtons();
4195 prepareAxes(POSITION);
4196 addConfigurationProperty("touch.orientationAware", "0");
4197 addMapperAndConfigure(mapper);
4198
4199 NotifyMotionArgs args;
4200
4201 // Rotation 90.
4202 prepareDisplay(DISPLAY_ORIENTATION_90);
4203 processDown(mapper, toRawX(50), toRawY(75));
4204 processSync(mapper);
4205
4206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4207 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4208 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4209
4210 processUp(mapper);
4211 processSync(mapper);
4212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4213}
4214
4215TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4216 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4217 addConfigurationProperty("touch.deviceType", "touchScreen");
4218 prepareButtons();
4219 prepareAxes(POSITION);
4220 addMapperAndConfigure(mapper);
4221
4222 NotifyMotionArgs args;
4223
4224 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004225 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 prepareDisplay(DISPLAY_ORIENTATION_0);
4227 processDown(mapper, toRawX(50), toRawY(75));
4228 processSync(mapper);
4229
4230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4231 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4232 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4233
4234 processUp(mapper);
4235 processSync(mapper);
4236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4237
4238 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004239 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 prepareDisplay(DISPLAY_ORIENTATION_90);
4241 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4242 processSync(mapper);
4243
4244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4245 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4246 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4247
4248 processUp(mapper);
4249 processSync(mapper);
4250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4251
4252 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004253 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 prepareDisplay(DISPLAY_ORIENTATION_180);
4255 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4256 processSync(mapper);
4257
4258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4259 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4260 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4261
4262 processUp(mapper);
4263 processSync(mapper);
4264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4265
4266 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004267 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 prepareDisplay(DISPLAY_ORIENTATION_270);
4269 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4270 processSync(mapper);
4271
4272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4273 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4274 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4275
4276 processUp(mapper);
4277 processSync(mapper);
4278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4279}
4280
4281TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4282 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4283 addConfigurationProperty("touch.deviceType", "touchScreen");
4284 prepareDisplay(DISPLAY_ORIENTATION_0);
4285 prepareButtons();
4286 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4287 addMapperAndConfigure(mapper);
4288
4289 // These calculations are based on the input device calibration documentation.
4290 int32_t rawX = 100;
4291 int32_t rawY = 200;
4292 int32_t rawPressure = 10;
4293 int32_t rawToolMajor = 12;
4294 int32_t rawDistance = 2;
4295 int32_t rawTiltX = 30;
4296 int32_t rawTiltY = 110;
4297
4298 float x = toDisplayX(rawX);
4299 float y = toDisplayY(rawY);
4300 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4301 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4302 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4303 float distance = float(rawDistance);
4304
4305 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4306 float tiltScale = M_PI / 180;
4307 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4308 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4309 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4310 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4311
4312 processDown(mapper, rawX, rawY);
4313 processPressure(mapper, rawPressure);
4314 processToolMajor(mapper, rawToolMajor);
4315 processDistance(mapper, rawDistance);
4316 processTilt(mapper, rawTiltX, rawTiltY);
4317 processSync(mapper);
4318
4319 NotifyMotionArgs args;
4320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4322 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4323 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4324}
4325
Jason Gerecke489fda82012-09-07 17:19:40 -07004326TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4327 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4328 addConfigurationProperty("touch.deviceType", "touchScreen");
4329 prepareDisplay(DISPLAY_ORIENTATION_0);
4330 prepareLocationCalibration();
4331 prepareButtons();
4332 prepareAxes(POSITION);
4333 addMapperAndConfigure(mapper);
4334
4335 int32_t rawX = 100;
4336 int32_t rawY = 200;
4337
4338 float x = toDisplayX(toCookedX(rawX, rawY));
4339 float y = toDisplayY(toCookedY(rawX, rawY));
4340
4341 processDown(mapper, rawX, rawY);
4342 processSync(mapper);
4343
4344 NotifyMotionArgs args;
4345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4347 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4348}
4349
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4351 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4352 addConfigurationProperty("touch.deviceType", "touchScreen");
4353 prepareDisplay(DISPLAY_ORIENTATION_0);
4354 prepareButtons();
4355 prepareAxes(POSITION);
4356 addMapperAndConfigure(mapper);
4357
4358 NotifyMotionArgs motionArgs;
4359 NotifyKeyArgs keyArgs;
4360
4361 processDown(mapper, 100, 200);
4362 processSync(mapper);
4363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4364 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4365 ASSERT_EQ(0, motionArgs.buttonState);
4366
4367 // press BTN_LEFT, release BTN_LEFT
4368 processKey(mapper, BTN_LEFT, 1);
4369 processSync(mapper);
4370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4372 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4373
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4375 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4376 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4377
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 processKey(mapper, BTN_LEFT, 0);
4379 processSync(mapper);
4380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004381 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004383
4384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004386 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387
4388 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4389 processKey(mapper, BTN_RIGHT, 1);
4390 processKey(mapper, BTN_MIDDLE, 1);
4391 processSync(mapper);
4392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4394 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4395 motionArgs.buttonState);
4396
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4399 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4400
4401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4403 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4404 motionArgs.buttonState);
4405
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 processKey(mapper, BTN_RIGHT, 0);
4407 processSync(mapper);
4408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004414 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415
4416 processKey(mapper, BTN_MIDDLE, 0);
4417 processSync(mapper);
4418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004419 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421
4422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004424 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425
4426 // press BTN_BACK, release BTN_BACK
4427 processKey(mapper, BTN_BACK, 1);
4428 processSync(mapper);
4429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4431 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004432
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4436
4437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4439 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440
4441 processKey(mapper, BTN_BACK, 0);
4442 processSync(mapper);
4443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004444 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004446
4447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449 ASSERT_EQ(0, motionArgs.buttonState);
4450
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4452 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4453 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4454
4455 // press BTN_SIDE, release BTN_SIDE
4456 processKey(mapper, BTN_SIDE, 1);
4457 processSync(mapper);
4458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4459 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4460 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004464 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4465
4466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4467 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4468 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469
4470 processKey(mapper, BTN_SIDE, 0);
4471 processSync(mapper);
4472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004473 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004475
4476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004478 ASSERT_EQ(0, motionArgs.buttonState);
4479
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4481 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4482 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4483
4484 // press BTN_FORWARD, release BTN_FORWARD
4485 processKey(mapper, BTN_FORWARD, 1);
4486 processSync(mapper);
4487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4488 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4489 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004490
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4494
4495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4496 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4497 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498
4499 processKey(mapper, BTN_FORWARD, 0);
4500 processSync(mapper);
4501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004502 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004504
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004507 ASSERT_EQ(0, motionArgs.buttonState);
4508
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4510 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4511 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4512
4513 // press BTN_EXTRA, release BTN_EXTRA
4514 processKey(mapper, BTN_EXTRA, 1);
4515 processSync(mapper);
4516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4517 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4518 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004519
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004522 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4523
4524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4525 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4526 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527
4528 processKey(mapper, BTN_EXTRA, 0);
4529 processSync(mapper);
4530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004533
4534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004536 ASSERT_EQ(0, motionArgs.buttonState);
4537
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4539 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4540 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4541
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4543
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544 // press BTN_STYLUS, release BTN_STYLUS
4545 processKey(mapper, BTN_STYLUS, 1);
4546 processSync(mapper);
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4550
4551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4553 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554
4555 processKey(mapper, BTN_STYLUS, 0);
4556 processSync(mapper);
4557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004558 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004560
4561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004563 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
4565 // press BTN_STYLUS2, release BTN_STYLUS2
4566 processKey(mapper, BTN_STYLUS2, 1);
4567 processSync(mapper);
4568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004570 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4571
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4573 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4574 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575
4576 processKey(mapper, BTN_STYLUS2, 0);
4577 processSync(mapper);
4578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004581
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585
4586 // release touch
4587 processUp(mapper);
4588 processSync(mapper);
4589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4591 ASSERT_EQ(0, motionArgs.buttonState);
4592}
4593
4594TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4595 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4596 addConfigurationProperty("touch.deviceType", "touchScreen");
4597 prepareDisplay(DISPLAY_ORIENTATION_0);
4598 prepareButtons();
4599 prepareAxes(POSITION);
4600 addMapperAndConfigure(mapper);
4601
4602 NotifyMotionArgs motionArgs;
4603
4604 // default tool type is finger
4605 processDown(mapper, 100, 200);
4606 processSync(mapper);
4607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4608 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4610
4611 // eraser
4612 processKey(mapper, BTN_TOOL_RUBBER, 1);
4613 processSync(mapper);
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4617
4618 // stylus
4619 processKey(mapper, BTN_TOOL_RUBBER, 0);
4620 processKey(mapper, BTN_TOOL_PEN, 1);
4621 processSync(mapper);
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4625
4626 // brush
4627 processKey(mapper, BTN_TOOL_PEN, 0);
4628 processKey(mapper, BTN_TOOL_BRUSH, 1);
4629 processSync(mapper);
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4633
4634 // pencil
4635 processKey(mapper, BTN_TOOL_BRUSH, 0);
4636 processKey(mapper, BTN_TOOL_PENCIL, 1);
4637 processSync(mapper);
4638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4641
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004642 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 processKey(mapper, BTN_TOOL_PENCIL, 0);
4644 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4645 processSync(mapper);
4646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4647 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4649
4650 // mouse
4651 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4652 processKey(mapper, BTN_TOOL_MOUSE, 1);
4653 processSync(mapper);
4654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4656 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4657
4658 // lens
4659 processKey(mapper, BTN_TOOL_MOUSE, 0);
4660 processKey(mapper, BTN_TOOL_LENS, 1);
4661 processSync(mapper);
4662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4663 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4665
4666 // double-tap
4667 processKey(mapper, BTN_TOOL_LENS, 0);
4668 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4669 processSync(mapper);
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4673
4674 // triple-tap
4675 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4676 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4677 processSync(mapper);
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4681
4682 // quad-tap
4683 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4684 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4685 processSync(mapper);
4686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4689
4690 // finger
4691 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4692 processKey(mapper, BTN_TOOL_FINGER, 1);
4693 processSync(mapper);
4694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4697
4698 // stylus trumps finger
4699 processKey(mapper, BTN_TOOL_PEN, 1);
4700 processSync(mapper);
4701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4704
4705 // eraser trumps stylus
4706 processKey(mapper, BTN_TOOL_RUBBER, 1);
4707 processSync(mapper);
4708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4711
4712 // mouse trumps eraser
4713 processKey(mapper, BTN_TOOL_MOUSE, 1);
4714 processSync(mapper);
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4718
4719 // back to default tool type
4720 processKey(mapper, BTN_TOOL_MOUSE, 0);
4721 processKey(mapper, BTN_TOOL_RUBBER, 0);
4722 processKey(mapper, BTN_TOOL_PEN, 0);
4723 processKey(mapper, BTN_TOOL_FINGER, 0);
4724 processSync(mapper);
4725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4727 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4728}
4729
4730TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4731 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4732 addConfigurationProperty("touch.deviceType", "touchScreen");
4733 prepareDisplay(DISPLAY_ORIENTATION_0);
4734 prepareButtons();
4735 prepareAxes(POSITION);
4736 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4737 addMapperAndConfigure(mapper);
4738
4739 NotifyMotionArgs motionArgs;
4740
4741 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4742 processKey(mapper, BTN_TOOL_FINGER, 1);
4743 processMove(mapper, 100, 200);
4744 processSync(mapper);
4745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4746 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4748 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4749
4750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4751 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4753 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4754
4755 // move a little
4756 processMove(mapper, 150, 250);
4757 processSync(mapper);
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4759 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4761 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4762
4763 // down when BTN_TOUCH is pressed, pressure defaults to 1
4764 processKey(mapper, BTN_TOUCH, 1);
4765 processSync(mapper);
4766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4767 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4769 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4770
4771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4772 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4774 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4775
4776 // up when BTN_TOUCH is released, hover restored
4777 processKey(mapper, BTN_TOUCH, 0);
4778 processSync(mapper);
4779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4780 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4782 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4783
4784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4785 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4787 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4788
4789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4790 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4792 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4793
4794 // exit hover when pointer goes away
4795 processKey(mapper, BTN_TOOL_FINGER, 0);
4796 processSync(mapper);
4797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4798 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4800 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4801}
4802
4803TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4804 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4805 addConfigurationProperty("touch.deviceType", "touchScreen");
4806 prepareDisplay(DISPLAY_ORIENTATION_0);
4807 prepareButtons();
4808 prepareAxes(POSITION | PRESSURE);
4809 addMapperAndConfigure(mapper);
4810
4811 NotifyMotionArgs motionArgs;
4812
4813 // initially hovering because pressure is 0
4814 processDown(mapper, 100, 200);
4815 processPressure(mapper, 0);
4816 processSync(mapper);
4817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4818 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4820 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4821
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4825 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4826
4827 // move a little
4828 processMove(mapper, 150, 250);
4829 processSync(mapper);
4830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4831 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4833 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4834
4835 // down when pressure is non-zero
4836 processPressure(mapper, RAW_PRESSURE_MAX);
4837 processSync(mapper);
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4839 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4841 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4842
4843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4844 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4846 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4847
4848 // up when pressure becomes 0, hover restored
4849 processPressure(mapper, 0);
4850 processSync(mapper);
4851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4852 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4854 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4855
4856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4857 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4859 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4860
4861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4862 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4864 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4865
4866 // exit hover when pointer goes away
4867 processUp(mapper);
4868 processSync(mapper);
4869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4872 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4873}
4874
Dan Harmsaca28402018-12-17 13:55:20 -08004875
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876// --- MultiTouchInputMapperTest ---
4877
4878class MultiTouchInputMapperTest : public TouchInputMapperTest {
4879protected:
4880 void prepareAxes(int axes);
4881
4882 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4883 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4884 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4885 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4886 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4887 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4888 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4889 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4890 void processId(MultiTouchInputMapper* mapper, int32_t id);
4891 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4892 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4893 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4894 void processMTSync(MultiTouchInputMapper* mapper);
4895 void processSync(MultiTouchInputMapper* mapper);
4896};
4897
4898void MultiTouchInputMapperTest::prepareAxes(int axes) {
4899 if (axes & POSITION) {
4900 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4901 RAW_X_MIN, RAW_X_MAX, 0, 0);
4902 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4903 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4904 }
4905 if (axes & TOUCH) {
4906 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4907 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4908 if (axes & MINOR) {
4909 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4910 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4911 }
4912 }
4913 if (axes & TOOL) {
4914 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4915 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4916 if (axes & MINOR) {
4917 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4918 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4919 }
4920 }
4921 if (axes & ORIENTATION) {
4922 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4923 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4924 }
4925 if (axes & PRESSURE) {
4926 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4927 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4928 }
4929 if (axes & DISTANCE) {
4930 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4931 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4932 }
4933 if (axes & ID) {
4934 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4935 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4936 }
4937 if (axes & SLOT) {
4938 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4939 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4940 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4941 }
4942 if (axes & TOOL_TYPE) {
4943 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4944 0, MT_TOOL_MAX, 0, 0);
4945 }
4946}
4947
4948void MultiTouchInputMapperTest::processPosition(
4949 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004950 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4951 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952}
4953
4954void MultiTouchInputMapperTest::processTouchMajor(
4955 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004956 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957}
4958
4959void MultiTouchInputMapperTest::processTouchMinor(
4960 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004961 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962}
4963
4964void MultiTouchInputMapperTest::processToolMajor(
4965 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004966 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967}
4968
4969void MultiTouchInputMapperTest::processToolMinor(
4970 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004971 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972}
4973
4974void MultiTouchInputMapperTest::processOrientation(
4975 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004976 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977}
4978
4979void MultiTouchInputMapperTest::processPressure(
4980 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004981 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982}
4983
4984void MultiTouchInputMapperTest::processDistance(
4985 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004986 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987}
4988
4989void MultiTouchInputMapperTest::processId(
4990 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004991 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992}
4993
4994void MultiTouchInputMapperTest::processSlot(
4995 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004996 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997}
4998
4999void MultiTouchInputMapperTest::processToolType(
5000 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005001 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002}
5003
5004void MultiTouchInputMapperTest::processKey(
5005 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005006 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007}
5008
5009void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005010 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011}
5012
5013void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005014 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015}
5016
5017
5018TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
5019 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5020 addConfigurationProperty("touch.deviceType", "touchScreen");
5021 prepareDisplay(DISPLAY_ORIENTATION_0);
5022 prepareAxes(POSITION);
5023 prepareVirtualKeys();
5024 addMapperAndConfigure(mapper);
5025
5026 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5027
5028 NotifyMotionArgs motionArgs;
5029
5030 // Two fingers down at once.
5031 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5032 processPosition(mapper, x1, y1);
5033 processMTSync(mapper);
5034 processPosition(mapper, x2, y2);
5035 processMTSync(mapper);
5036 processSync(mapper);
5037
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5039 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5040 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5041 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5042 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5043 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5044 ASSERT_EQ(0, motionArgs.flags);
5045 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5046 ASSERT_EQ(0, motionArgs.buttonState);
5047 ASSERT_EQ(0, motionArgs.edgeFlags);
5048 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5049 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5052 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5053 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5054 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5055 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5056
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5058 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5059 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5060 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5061 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5062 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5063 motionArgs.action);
5064 ASSERT_EQ(0, motionArgs.flags);
5065 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5066 ASSERT_EQ(0, motionArgs.buttonState);
5067 ASSERT_EQ(0, motionArgs.edgeFlags);
5068 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5069 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5071 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5074 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5076 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5077 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5078 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5080
5081 // Move.
5082 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5083 processPosition(mapper, x1, y1);
5084 processMTSync(mapper);
5085 processPosition(mapper, x2, y2);
5086 processMTSync(mapper);
5087 processSync(mapper);
5088
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5090 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5091 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5092 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5093 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5095 ASSERT_EQ(0, motionArgs.flags);
5096 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5097 ASSERT_EQ(0, motionArgs.buttonState);
5098 ASSERT_EQ(0, motionArgs.edgeFlags);
5099 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5100 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5102 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5105 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5107 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5108 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5109 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5110 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5111
5112 // First finger up.
5113 x2 += 15; y2 -= 20;
5114 processPosition(mapper, x2, y2);
5115 processMTSync(mapper);
5116 processSync(mapper);
5117
5118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5119 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5120 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5121 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5122 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5123 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5124 motionArgs.action);
5125 ASSERT_EQ(0, motionArgs.flags);
5126 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5127 ASSERT_EQ(0, motionArgs.buttonState);
5128 ASSERT_EQ(0, motionArgs.edgeFlags);
5129 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5130 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5132 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5135 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5137 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5138 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5139 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5140 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5141
5142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5144 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5145 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5146 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5148 ASSERT_EQ(0, motionArgs.flags);
5149 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5150 ASSERT_EQ(0, motionArgs.buttonState);
5151 ASSERT_EQ(0, motionArgs.edgeFlags);
5152 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5153 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5156 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5157 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5158 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5159 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5160
5161 // Move.
5162 x2 += 20; y2 -= 25;
5163 processPosition(mapper, x2, y2);
5164 processMTSync(mapper);
5165 processSync(mapper);
5166
5167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5168 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5169 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5170 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5171 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5173 ASSERT_EQ(0, motionArgs.flags);
5174 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5175 ASSERT_EQ(0, motionArgs.buttonState);
5176 ASSERT_EQ(0, motionArgs.edgeFlags);
5177 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5178 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5181 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5182 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5183 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5185
5186 // New finger down.
5187 int32_t x3 = 700, y3 = 300;
5188 processPosition(mapper, x2, y2);
5189 processMTSync(mapper);
5190 processPosition(mapper, x3, y3);
5191 processMTSync(mapper);
5192 processSync(mapper);
5193
5194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5195 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5196 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5197 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5198 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5199 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5200 motionArgs.action);
5201 ASSERT_EQ(0, motionArgs.flags);
5202 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5203 ASSERT_EQ(0, motionArgs.buttonState);
5204 ASSERT_EQ(0, motionArgs.edgeFlags);
5205 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5206 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5208 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5211 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5213 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5214 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5215 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5217
5218 // Second finger up.
5219 x3 += 30; y3 -= 20;
5220 processPosition(mapper, x3, y3);
5221 processMTSync(mapper);
5222 processSync(mapper);
5223
5224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5225 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5226 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5227 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5228 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5229 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5230 motionArgs.action);
5231 ASSERT_EQ(0, motionArgs.flags);
5232 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5233 ASSERT_EQ(0, motionArgs.buttonState);
5234 ASSERT_EQ(0, motionArgs.edgeFlags);
5235 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5236 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5238 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5241 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5243 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5244 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5245 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5247
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5250 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5251 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5252 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5254 ASSERT_EQ(0, motionArgs.flags);
5255 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5256 ASSERT_EQ(0, motionArgs.buttonState);
5257 ASSERT_EQ(0, motionArgs.edgeFlags);
5258 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5259 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5262 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5263 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5264 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5266
5267 // Last finger up.
5268 processMTSync(mapper);
5269 processSync(mapper);
5270
5271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5273 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5274 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5275 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5276 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5277 ASSERT_EQ(0, motionArgs.flags);
5278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5279 ASSERT_EQ(0, motionArgs.buttonState);
5280 ASSERT_EQ(0, motionArgs.edgeFlags);
5281 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5282 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5285 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5286 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5287 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5289
5290 // Should not have sent any more keys or motions.
5291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5293}
5294
5295TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5296 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5297 addConfigurationProperty("touch.deviceType", "touchScreen");
5298 prepareDisplay(DISPLAY_ORIENTATION_0);
5299 prepareAxes(POSITION | ID);
5300 prepareVirtualKeys();
5301 addMapperAndConfigure(mapper);
5302
5303 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5304
5305 NotifyMotionArgs motionArgs;
5306
5307 // Two fingers down at once.
5308 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5309 processPosition(mapper, x1, y1);
5310 processId(mapper, 1);
5311 processMTSync(mapper);
5312 processPosition(mapper, x2, y2);
5313 processId(mapper, 2);
5314 processMTSync(mapper);
5315 processSync(mapper);
5316
5317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5318 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5319 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5320 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5323 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5324
5325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5326 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5327 motionArgs.action);
5328 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5329 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5331 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5334 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5336 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5337
5338 // Move.
5339 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5340 processPosition(mapper, x1, y1);
5341 processId(mapper, 1);
5342 processMTSync(mapper);
5343 processPosition(mapper, x2, y2);
5344 processId(mapper, 2);
5345 processMTSync(mapper);
5346 processSync(mapper);
5347
5348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5350 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5351 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5353 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5356 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5358 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5359
5360 // First finger up.
5361 x2 += 15; y2 -= 20;
5362 processPosition(mapper, x2, y2);
5363 processId(mapper, 2);
5364 processMTSync(mapper);
5365 processSync(mapper);
5366
5367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5368 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5369 motionArgs.action);
5370 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5371 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5373 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5376 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5378 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5379
5380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5382 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5383 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5386 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5387
5388 // Move.
5389 x2 += 20; y2 -= 25;
5390 processPosition(mapper, x2, y2);
5391 processId(mapper, 2);
5392 processMTSync(mapper);
5393 processSync(mapper);
5394
5395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5397 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5398 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5401 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5402
5403 // New finger down.
5404 int32_t x3 = 700, y3 = 300;
5405 processPosition(mapper, x2, y2);
5406 processId(mapper, 2);
5407 processMTSync(mapper);
5408 processPosition(mapper, x3, y3);
5409 processId(mapper, 3);
5410 processMTSync(mapper);
5411 processSync(mapper);
5412
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5414 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5415 motionArgs.action);
5416 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5417 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5419 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5422 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5424 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5425
5426 // Second finger up.
5427 x3 += 30; y3 -= 20;
5428 processPosition(mapper, x3, y3);
5429 processId(mapper, 3);
5430 processMTSync(mapper);
5431 processSync(mapper);
5432
5433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5434 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5435 motionArgs.action);
5436 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5437 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5439 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5442 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5444 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5445
5446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5448 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5449 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5452 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5453
5454 // Last finger up.
5455 processMTSync(mapper);
5456 processSync(mapper);
5457
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5459 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5460 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5461 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5464 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5465
5466 // Should not have sent any more keys or motions.
5467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5469}
5470
5471TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5472 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5473 addConfigurationProperty("touch.deviceType", "touchScreen");
5474 prepareDisplay(DISPLAY_ORIENTATION_0);
5475 prepareAxes(POSITION | ID | SLOT);
5476 prepareVirtualKeys();
5477 addMapperAndConfigure(mapper);
5478
5479 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5480
5481 NotifyMotionArgs motionArgs;
5482
5483 // Two fingers down at once.
5484 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5485 processPosition(mapper, x1, y1);
5486 processId(mapper, 1);
5487 processSlot(mapper, 1);
5488 processPosition(mapper, x2, y2);
5489 processId(mapper, 2);
5490 processSync(mapper);
5491
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5493 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5494 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5495 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5498 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5499
5500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5501 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5502 motionArgs.action);
5503 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5504 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5506 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5507 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5509 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5511 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5512
5513 // Move.
5514 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5515 processSlot(mapper, 0);
5516 processPosition(mapper, x1, y1);
5517 processSlot(mapper, 1);
5518 processPosition(mapper, x2, y2);
5519 processSync(mapper);
5520
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5523 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5524 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5526 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5529 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5531 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5532
5533 // First finger up.
5534 x2 += 15; y2 -= 20;
5535 processSlot(mapper, 0);
5536 processId(mapper, -1);
5537 processSlot(mapper, 1);
5538 processPosition(mapper, x2, y2);
5539 processSync(mapper);
5540
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5542 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5543 motionArgs.action);
5544 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5545 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5547 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5550 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5552 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5553
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5556 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5557 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5560 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5561
5562 // Move.
5563 x2 += 20; y2 -= 25;
5564 processPosition(mapper, x2, y2);
5565 processSync(mapper);
5566
5567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5569 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5570 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5573 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5574
5575 // New finger down.
5576 int32_t x3 = 700, y3 = 300;
5577 processPosition(mapper, x2, y2);
5578 processSlot(mapper, 0);
5579 processId(mapper, 3);
5580 processPosition(mapper, x3, y3);
5581 processSync(mapper);
5582
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5584 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5585 motionArgs.action);
5586 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5587 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5589 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5592 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5594 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5595
5596 // Second finger up.
5597 x3 += 30; y3 -= 20;
5598 processSlot(mapper, 1);
5599 processId(mapper, -1);
5600 processSlot(mapper, 0);
5601 processPosition(mapper, x3, y3);
5602 processSync(mapper);
5603
5604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5605 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5606 motionArgs.action);
5607 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5608 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5610 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5613 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5615 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5616
5617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5619 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5620 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5623 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5624
5625 // Last finger up.
5626 processId(mapper, -1);
5627 processSync(mapper);
5628
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5631 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5632 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5635 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5636
5637 // Should not have sent any more keys or motions.
5638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5640}
5641
5642TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5643 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5644 addConfigurationProperty("touch.deviceType", "touchScreen");
5645 prepareDisplay(DISPLAY_ORIENTATION_0);
5646 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5647 addMapperAndConfigure(mapper);
5648
5649 // These calculations are based on the input device calibration documentation.
5650 int32_t rawX = 100;
5651 int32_t rawY = 200;
5652 int32_t rawTouchMajor = 7;
5653 int32_t rawTouchMinor = 6;
5654 int32_t rawToolMajor = 9;
5655 int32_t rawToolMinor = 8;
5656 int32_t rawPressure = 11;
5657 int32_t rawDistance = 0;
5658 int32_t rawOrientation = 3;
5659 int32_t id = 5;
5660
5661 float x = toDisplayX(rawX);
5662 float y = toDisplayY(rawY);
5663 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5664 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5665 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5666 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5667 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5668 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5669 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5670 float distance = float(rawDistance);
5671
5672 processPosition(mapper, rawX, rawY);
5673 processTouchMajor(mapper, rawTouchMajor);
5674 processTouchMinor(mapper, rawTouchMinor);
5675 processToolMajor(mapper, rawToolMajor);
5676 processToolMinor(mapper, rawToolMinor);
5677 processPressure(mapper, rawPressure);
5678 processOrientation(mapper, rawOrientation);
5679 processDistance(mapper, rawDistance);
5680 processId(mapper, id);
5681 processMTSync(mapper);
5682 processSync(mapper);
5683
5684 NotifyMotionArgs args;
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5686 ASSERT_EQ(0, args.pointerProperties[0].id);
5687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5688 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5689 orientation, distance));
5690}
5691
5692TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5693 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5694 addConfigurationProperty("touch.deviceType", "touchScreen");
5695 prepareDisplay(DISPLAY_ORIENTATION_0);
5696 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5697 addConfigurationProperty("touch.size.calibration", "geometric");
5698 addMapperAndConfigure(mapper);
5699
5700 // These calculations are based on the input device calibration documentation.
5701 int32_t rawX = 100;
5702 int32_t rawY = 200;
5703 int32_t rawTouchMajor = 140;
5704 int32_t rawTouchMinor = 120;
5705 int32_t rawToolMajor = 180;
5706 int32_t rawToolMinor = 160;
5707
5708 float x = toDisplayX(rawX);
5709 float y = toDisplayY(rawY);
5710 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5711 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5712 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5713 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5714 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5715
5716 processPosition(mapper, rawX, rawY);
5717 processTouchMajor(mapper, rawTouchMajor);
5718 processTouchMinor(mapper, rawTouchMinor);
5719 processToolMajor(mapper, rawToolMajor);
5720 processToolMinor(mapper, rawToolMinor);
5721 processMTSync(mapper);
5722 processSync(mapper);
5723
5724 NotifyMotionArgs args;
5725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5727 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5728}
5729
5730TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5731 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5732 addConfigurationProperty("touch.deviceType", "touchScreen");
5733 prepareDisplay(DISPLAY_ORIENTATION_0);
5734 prepareAxes(POSITION | TOUCH | TOOL);
5735 addConfigurationProperty("touch.size.calibration", "diameter");
5736 addConfigurationProperty("touch.size.scale", "10");
5737 addConfigurationProperty("touch.size.bias", "160");
5738 addConfigurationProperty("touch.size.isSummed", "1");
5739 addMapperAndConfigure(mapper);
5740
5741 // These calculations are based on the input device calibration documentation.
5742 // Note: We only provide a single common touch/tool value because the device is assumed
5743 // not to emit separate values for each pointer (isSummed = 1).
5744 int32_t rawX = 100;
5745 int32_t rawY = 200;
5746 int32_t rawX2 = 150;
5747 int32_t rawY2 = 250;
5748 int32_t rawTouchMajor = 5;
5749 int32_t rawToolMajor = 8;
5750
5751 float x = toDisplayX(rawX);
5752 float y = toDisplayY(rawY);
5753 float x2 = toDisplayX(rawX2);
5754 float y2 = toDisplayY(rawY2);
5755 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5756 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5757 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5758
5759 processPosition(mapper, rawX, rawY);
5760 processTouchMajor(mapper, rawTouchMajor);
5761 processToolMajor(mapper, rawToolMajor);
5762 processMTSync(mapper);
5763 processPosition(mapper, rawX2, rawY2);
5764 processTouchMajor(mapper, rawTouchMajor);
5765 processToolMajor(mapper, rawToolMajor);
5766 processMTSync(mapper);
5767 processSync(mapper);
5768
5769 NotifyMotionArgs args;
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5771 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5772
5773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5774 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5775 args.action);
5776 ASSERT_EQ(size_t(2), args.pointerCount);
5777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5778 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5780 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5781}
5782
5783TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5784 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5785 addConfigurationProperty("touch.deviceType", "touchScreen");
5786 prepareDisplay(DISPLAY_ORIENTATION_0);
5787 prepareAxes(POSITION | TOUCH | TOOL);
5788 addConfigurationProperty("touch.size.calibration", "area");
5789 addConfigurationProperty("touch.size.scale", "43");
5790 addConfigurationProperty("touch.size.bias", "3");
5791 addMapperAndConfigure(mapper);
5792
5793 // These calculations are based on the input device calibration documentation.
5794 int32_t rawX = 100;
5795 int32_t rawY = 200;
5796 int32_t rawTouchMajor = 5;
5797 int32_t rawToolMajor = 8;
5798
5799 float x = toDisplayX(rawX);
5800 float y = toDisplayY(rawY);
5801 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5802 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5803 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5804
5805 processPosition(mapper, rawX, rawY);
5806 processTouchMajor(mapper, rawTouchMajor);
5807 processToolMajor(mapper, rawToolMajor);
5808 processMTSync(mapper);
5809 processSync(mapper);
5810
5811 NotifyMotionArgs args;
5812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5814 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5815}
5816
5817TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5818 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5819 addConfigurationProperty("touch.deviceType", "touchScreen");
5820 prepareDisplay(DISPLAY_ORIENTATION_0);
5821 prepareAxes(POSITION | PRESSURE);
5822 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5823 addConfigurationProperty("touch.pressure.scale", "0.01");
5824 addMapperAndConfigure(mapper);
5825
Michael Wrightaa449c92017-12-13 21:21:43 +00005826 InputDeviceInfo info;
5827 mapper->populateDeviceInfo(&info);
5828 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5829 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5830 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5831
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832 // These calculations are based on the input device calibration documentation.
5833 int32_t rawX = 100;
5834 int32_t rawY = 200;
5835 int32_t rawPressure = 60;
5836
5837 float x = toDisplayX(rawX);
5838 float y = toDisplayY(rawY);
5839 float pressure = float(rawPressure) * 0.01f;
5840
5841 processPosition(mapper, rawX, rawY);
5842 processPressure(mapper, rawPressure);
5843 processMTSync(mapper);
5844 processSync(mapper);
5845
5846 NotifyMotionArgs args;
5847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5849 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5850}
5851
5852TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5853 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5854 addConfigurationProperty("touch.deviceType", "touchScreen");
5855 prepareDisplay(DISPLAY_ORIENTATION_0);
5856 prepareAxes(POSITION | ID | SLOT);
5857 addMapperAndConfigure(mapper);
5858
5859 NotifyMotionArgs motionArgs;
5860 NotifyKeyArgs keyArgs;
5861
5862 processId(mapper, 1);
5863 processPosition(mapper, 100, 200);
5864 processSync(mapper);
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5867 ASSERT_EQ(0, motionArgs.buttonState);
5868
5869 // press BTN_LEFT, release BTN_LEFT
5870 processKey(mapper, BTN_LEFT, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5874 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5875
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5877 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5878 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5879
Michael Wrightd02c5b62014-02-10 15:10:22 -08005880 processKey(mapper, BTN_LEFT, 0);
5881 processSync(mapper);
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005883 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005885
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005888 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889
5890 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5891 processKey(mapper, BTN_RIGHT, 1);
5892 processKey(mapper, BTN_MIDDLE, 1);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5896 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5897 motionArgs.buttonState);
5898
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5900 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5901 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5902
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5904 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5905 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5906 motionArgs.buttonState);
5907
Michael Wrightd02c5b62014-02-10 15:10:22 -08005908 processKey(mapper, BTN_RIGHT, 0);
5909 processSync(mapper);
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005913
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005916 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917
5918 processKey(mapper, BTN_MIDDLE, 0);
5919 processSync(mapper);
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005921 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005923
5924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005926 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005927
5928 // press BTN_BACK, release BTN_BACK
5929 processKey(mapper, BTN_BACK, 1);
5930 processSync(mapper);
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5932 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5933 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005934
Michael Wrightd02c5b62014-02-10 15:10:22 -08005935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005937 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5938
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5941 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942
5943 processKey(mapper, BTN_BACK, 0);
5944 processSync(mapper);
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005946 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005948
5949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005951 ASSERT_EQ(0, motionArgs.buttonState);
5952
Michael Wrightd02c5b62014-02-10 15:10:22 -08005953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5954 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5955 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5956
5957 // press BTN_SIDE, release BTN_SIDE
5958 processKey(mapper, BTN_SIDE, 1);
5959 processSync(mapper);
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5961 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5962 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963
Michael Wrightd02c5b62014-02-10 15:10:22 -08005964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005966 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5967
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5969 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5970 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005971
5972 processKey(mapper, BTN_SIDE, 0);
5973 processSync(mapper);
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005975 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005977
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005980 ASSERT_EQ(0, motionArgs.buttonState);
5981
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5983 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5984 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5985
5986 // press BTN_FORWARD, release BTN_FORWARD
5987 processKey(mapper, BTN_FORWARD, 1);
5988 processSync(mapper);
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5990 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5991 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005992
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005995 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5996
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5998 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5999 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000
6001 processKey(mapper, BTN_FORWARD, 0);
6002 processSync(mapper);
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006006
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006009 ASSERT_EQ(0, motionArgs.buttonState);
6010
Michael Wrightd02c5b62014-02-10 15:10:22 -08006011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6012 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6013 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6014
6015 // press BTN_EXTRA, release BTN_EXTRA
6016 processKey(mapper, BTN_EXTRA, 1);
6017 processSync(mapper);
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6019 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6020 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006021
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006024 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6025
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6028 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006029
6030 processKey(mapper, BTN_EXTRA, 0);
6031 processSync(mapper);
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006035
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006038 ASSERT_EQ(0, motionArgs.buttonState);
6039
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6041 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6042 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6043
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6045
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046 // press BTN_STYLUS, release BTN_STYLUS
6047 processKey(mapper, BTN_STYLUS, 1);
6048 processSync(mapper);
6049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006051 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6052
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6055 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056
6057 processKey(mapper, BTN_STYLUS, 0);
6058 processSync(mapper);
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006061 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006062
6063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006065 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066
6067 // press BTN_STYLUS2, release BTN_STYLUS2
6068 processKey(mapper, BTN_STYLUS2, 1);
6069 processSync(mapper);
6070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6073
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6076 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006077
6078 processKey(mapper, BTN_STYLUS2, 0);
6079 processSync(mapper);
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006083
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006086 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087
6088 // release touch
6089 processId(mapper, -1);
6090 processSync(mapper);
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6092 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6093 ASSERT_EQ(0, motionArgs.buttonState);
6094}
6095
6096TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
6097 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6098 addConfigurationProperty("touch.deviceType", "touchScreen");
6099 prepareDisplay(DISPLAY_ORIENTATION_0);
6100 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6101 addMapperAndConfigure(mapper);
6102
6103 NotifyMotionArgs motionArgs;
6104
6105 // default tool type is finger
6106 processId(mapper, 1);
6107 processPosition(mapper, 100, 200);
6108 processSync(mapper);
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6110 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6112
6113 // eraser
6114 processKey(mapper, BTN_TOOL_RUBBER, 1);
6115 processSync(mapper);
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6119
6120 // stylus
6121 processKey(mapper, BTN_TOOL_RUBBER, 0);
6122 processKey(mapper, BTN_TOOL_PEN, 1);
6123 processSync(mapper);
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6127
6128 // brush
6129 processKey(mapper, BTN_TOOL_PEN, 0);
6130 processKey(mapper, BTN_TOOL_BRUSH, 1);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6135
6136 // pencil
6137 processKey(mapper, BTN_TOOL_BRUSH, 0);
6138 processKey(mapper, BTN_TOOL_PENCIL, 1);
6139 processSync(mapper);
6140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6143
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006144 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145 processKey(mapper, BTN_TOOL_PENCIL, 0);
6146 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6147 processSync(mapper);
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6151
6152 // mouse
6153 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6154 processKey(mapper, BTN_TOOL_MOUSE, 1);
6155 processSync(mapper);
6156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6158 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6159
6160 // lens
6161 processKey(mapper, BTN_TOOL_MOUSE, 0);
6162 processKey(mapper, BTN_TOOL_LENS, 1);
6163 processSync(mapper);
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6167
6168 // double-tap
6169 processKey(mapper, BTN_TOOL_LENS, 0);
6170 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6171 processSync(mapper);
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6175
6176 // triple-tap
6177 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6178 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6179 processSync(mapper);
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6183
6184 // quad-tap
6185 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6186 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6191
6192 // finger
6193 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6194 processKey(mapper, BTN_TOOL_FINGER, 1);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6199
6200 // stylus trumps finger
6201 processKey(mapper, BTN_TOOL_PEN, 1);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6206
6207 // eraser trumps stylus
6208 processKey(mapper, BTN_TOOL_RUBBER, 1);
6209 processSync(mapper);
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6213
6214 // mouse trumps eraser
6215 processKey(mapper, BTN_TOOL_MOUSE, 1);
6216 processSync(mapper);
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6220
6221 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6222 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6223 processSync(mapper);
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6227
6228 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6229 processToolType(mapper, MT_TOOL_PEN);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6234
6235 // back to default tool type
6236 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6237 processKey(mapper, BTN_TOOL_MOUSE, 0);
6238 processKey(mapper, BTN_TOOL_RUBBER, 0);
6239 processKey(mapper, BTN_TOOL_PEN, 0);
6240 processKey(mapper, BTN_TOOL_FINGER, 0);
6241 processSync(mapper);
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6245}
6246
6247TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6248 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6249 addConfigurationProperty("touch.deviceType", "touchScreen");
6250 prepareDisplay(DISPLAY_ORIENTATION_0);
6251 prepareAxes(POSITION | ID | SLOT);
6252 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6253 addMapperAndConfigure(mapper);
6254
6255 NotifyMotionArgs motionArgs;
6256
6257 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6258 processId(mapper, 1);
6259 processPosition(mapper, 100, 200);
6260 processSync(mapper);
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6262 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6264 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6265
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6269 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6270
6271 // move a little
6272 processPosition(mapper, 150, 250);
6273 processSync(mapper);
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6275 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6277 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6278
6279 // down when BTN_TOUCH is pressed, pressure defaults to 1
6280 processKey(mapper, BTN_TOUCH, 1);
6281 processSync(mapper);
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6283 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6285 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6286
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6290 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6291
6292 // up when BTN_TOUCH is released, hover restored
6293 processKey(mapper, BTN_TOUCH, 0);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6298 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6299
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6301 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6303 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6304
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6308 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6309
6310 // exit hover when pointer goes away
6311 processId(mapper, -1);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6316 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6317}
6318
6319TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6320 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6321 addConfigurationProperty("touch.deviceType", "touchScreen");
6322 prepareDisplay(DISPLAY_ORIENTATION_0);
6323 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6324 addMapperAndConfigure(mapper);
6325
6326 NotifyMotionArgs motionArgs;
6327
6328 // initially hovering because pressure is 0
6329 processId(mapper, 1);
6330 processPosition(mapper, 100, 200);
6331 processPressure(mapper, 0);
6332 processSync(mapper);
6333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6334 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6336 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6337
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6341 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6342
6343 // move a little
6344 processPosition(mapper, 150, 250);
6345 processSync(mapper);
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6347 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6349 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6350
6351 // down when pressure becomes non-zero
6352 processPressure(mapper, RAW_PRESSURE_MAX);
6353 processSync(mapper);
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6355 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6357 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6358
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6362 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6363
6364 // up when pressure becomes 0, hover restored
6365 processPressure(mapper, 0);
6366 processSync(mapper);
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6370 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6371
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6373 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6375 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6376
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6380 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6381
6382 // exit hover when pointer goes away
6383 processId(mapper, -1);
6384 processSync(mapper);
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6388 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6389}
6390
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006391/**
6392 * Set the input device port <--> display port associations, and check that the
6393 * events are routed to the display that matches the display port.
6394 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6395 */
6396TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6397 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6398 const std::string usb2 = "USB2";
6399 const uint8_t hdmi1 = 0;
6400 const uint8_t hdmi2 = 1;
6401 const std::string secondaryUniqueId = "uniqueId2";
6402 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6403
6404 addConfigurationProperty("touch.deviceType", "touchScreen");
6405 prepareAxes(POSITION);
6406 addMapperAndConfigure(mapper);
6407
6408 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6409 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6410
6411 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6412 // for this input device is specified, and the matching viewport is not present,
6413 // the input device should be disabled (at the mapper level).
6414
6415 // Add viewport for display 2 on hdmi2
6416 prepareSecondaryDisplay(type, hdmi2);
6417 // Send a touch event
6418 processPosition(mapper, 100, 100);
6419 processSync(mapper);
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6421
6422 // Add viewport for display 1 on hdmi1
6423 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6424 // Send a touch event again
6425 processPosition(mapper, 100, 100);
6426 processSync(mapper);
6427
6428 NotifyMotionArgs args;
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6430 ASSERT_EQ(DISPLAY_ID, args.displayId);
6431}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432
Arthur Hung41a712e2018-11-22 19:41:03 +08006433/**
6434 * Expect fallback to internal viewport if device is external and external viewport is not present.
6435 */
6436TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6437 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6438 prepareAxes(POSITION);
6439 addConfigurationProperty("touch.deviceType", "touchScreen");
6440 prepareDisplay(DISPLAY_ORIENTATION_0);
6441 mDevice->setExternal(true);
6442 addMapperAndConfigure(mapper);
6443
6444 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6445
6446 NotifyMotionArgs motionArgs;
6447
6448 // Expect the event to be sent to the internal viewport,
6449 // because an external viewport is not present.
6450 processPosition(mapper, 100, 100);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6454
6455 // Expect the event to be sent to the external viewport if it is present.
6456 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6457 processPosition(mapper, 100, 100);
6458 processSync(mapper);
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6460 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6461}
6462
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006463TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6464 // Setup PointerController for second display.
6465 sp<FakePointerController> fakePointerController = new FakePointerController();
6466 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6467 fakePointerController->setPosition(100, 200);
6468 fakePointerController->setButtonState(0);
6469 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6470 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6471
6472 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6473 prepareDisplay(DISPLAY_ORIENTATION_0);
6474 prepareAxes(POSITION);
6475 addMapperAndConfigure(mapper);
6476
6477 // Check source is mouse that would obtain the PointerController.
6478 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6479
6480 NotifyMotionArgs motionArgs;
6481 processPosition(mapper, 100, 100);
6482 processSync(mapper);
6483
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6486 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6487}
6488
Arthur Hung7c645402019-01-25 17:45:42 +08006489TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6490 // Setup the first touch screen device.
6491 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6492 prepareAxes(POSITION | ID | SLOT);
6493 addConfigurationProperty("touch.deviceType", "touchScreen");
6494 addMapperAndConfigure(mapper);
6495
6496 // Create the second touch screen device, and enable multi fingers.
6497 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006498 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006499 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006500 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006501 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006502 std::unique_ptr<InputDevice> device2 =
6503 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6504 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006505 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6506 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6507 0 /*flat*/, 0 /*fuzz*/);
6508 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6509 0 /*flat*/, 0 /*fuzz*/);
6510 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6511 0 /*flat*/, 0 /*fuzz*/);
6512 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6513 0 /*flat*/, 0 /*fuzz*/);
6514 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6515 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6516 String8("touchScreen"));
6517
6518 // Setup the second touch screen device.
Arthur Hung2c9a3342019-07-23 14:18:59 +08006519 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2.get());
Arthur Hung7c645402019-01-25 17:45:42 +08006520 device2->addMapper(mapper2);
6521 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6522 device2->reset(ARBITRARY_TIME);
6523
6524 // Setup PointerController.
6525 sp<FakePointerController> fakePointerController = new FakePointerController();
6526 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6527 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6528
6529 // Setup policy for associated displays and show touches.
6530 const uint8_t hdmi1 = 0;
6531 const uint8_t hdmi2 = 1;
6532 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6533 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6534 mFakePolicy->setShowTouches(true);
6535
6536 // Create displays.
6537 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6538 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6539
6540 // Default device will reconfigure above, need additional reconfiguration for another device.
6541 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6542 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6543
6544 // Two fingers down at default display.
6545 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6546 processPosition(mapper, x1, y1);
6547 processId(mapper, 1);
6548 processSlot(mapper, 1);
6549 processPosition(mapper, x2, y2);
6550 processId(mapper, 2);
6551 processSync(mapper);
6552
6553 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6554 fakePointerController->getSpots().find(DISPLAY_ID);
6555 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6556 ASSERT_EQ(size_t(2), iter->second.size());
6557
6558 // Two fingers down at second display.
6559 processPosition(mapper2, x1, y1);
6560 processId(mapper2, 1);
6561 processSlot(mapper2, 1);
6562 processPosition(mapper2, x2, y2);
6563 processId(mapper2, 2);
6564 processSync(mapper2);
6565
6566 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6567 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6568 ASSERT_EQ(size_t(2), iter->second.size());
6569}
6570
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006571TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6572 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6573 prepareAxes(POSITION);
6574 addConfigurationProperty("touch.deviceType", "touchScreen");
6575 prepareDisplay(DISPLAY_ORIENTATION_0);
6576 addMapperAndConfigure(mapper);
6577
6578 NotifyMotionArgs motionArgs;
6579 // Unrotated video frame
6580 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6581 std::vector<TouchVideoFrame> frames{frame};
6582 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6583 processPosition(mapper, 100, 200);
6584 processSync(mapper);
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(frames, motionArgs.videoFrames);
6587
6588 // Subsequent touch events should not have any videoframes
6589 // This is implemented separately in FakeEventHub,
6590 // but that should match the behaviour of TouchVideoDevice.
6591 processPosition(mapper, 200, 200);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6595}
6596
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006597TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6598 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6599 prepareAxes(POSITION);
6600 addConfigurationProperty("touch.deviceType", "touchScreen");
6601 addMapperAndConfigure(mapper);
6602 // Unrotated video frame
6603 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6604 NotifyMotionArgs motionArgs;
6605
6606 // Test all 4 orientations
6607 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6608 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6609 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6610 clearViewports();
6611 prepareDisplay(orientation);
6612 std::vector<TouchVideoFrame> frames{frame};
6613 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6614 processPosition(mapper, 100, 200);
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 frames[0].rotate(orientation);
6618 ASSERT_EQ(frames, motionArgs.videoFrames);
6619 }
6620}
6621
6622TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6623 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6624 prepareAxes(POSITION);
6625 addConfigurationProperty("touch.deviceType", "touchScreen");
6626 addMapperAndConfigure(mapper);
6627 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6628 // so mix these.
6629 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6630 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6631 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6632 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6633 NotifyMotionArgs motionArgs;
6634
6635 prepareDisplay(DISPLAY_ORIENTATION_90);
6636 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6637 processPosition(mapper, 100, 200);
6638 processSync(mapper);
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6640 std::for_each(frames.begin(), frames.end(),
6641 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6642 ASSERT_EQ(frames, motionArgs.videoFrames);
6643}
6644
Arthur Hung9da14732019-09-02 16:16:58 +08006645/**
6646 * If we had defined port associations, but the viewport is not ready, the touch device would be
6647 * expected to be disabled, and it should be enabled after the viewport has found.
6648 */
6649TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
6650 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6651 constexpr uint8_t hdmi2 = 1;
6652 const std::string secondaryUniqueId = "uniqueId2";
6653 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6654
6655 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6656
6657 addConfigurationProperty("touch.deviceType", "touchScreen");
6658 prepareAxes(POSITION);
6659 addMapperAndConfigure(mapper);
6660
6661 ASSERT_EQ(mDevice->isEnabled(), false);
6662
6663 // Add display on hdmi2, the device should be enabled and can receive touch event.
6664 prepareSecondaryDisplay(type, hdmi2);
6665 ASSERT_EQ(mDevice->isEnabled(), true);
6666
6667 // Send a touch event.
6668 processPosition(mapper, 100, 100);
6669 processSync(mapper);
6670
6671 NotifyMotionArgs args;
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6673 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6674}
6675
Arthur Hung6cd19a42019-08-30 19:04:12 +08006676/**
6677 * Test touch should not work if outside of surface.
6678 */
6679TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
6680 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6681 addConfigurationProperty("touch.deviceType", "touchScreen");
6682 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006683 prepareAxes(POSITION);
6684 addMapperAndConfigure(mapper);
6685
Arthur Hung05de5772019-09-26 18:31:26 +08006686 // Touch on left-top area should work.
6687 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6688 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6689 processPosition(mapper, rawX, rawY);
6690 processSync(mapper);
6691
6692 NotifyMotionArgs args;
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6694
6695 // Reset.
6696 mapper->reset(ARBITRARY_TIME);
6697
6698 // Let logical display be different to physical display and rotate 90-degrees.
6699 std::optional<DisplayViewport> internalViewport =
6700 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6701 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6702 internalViewport->logicalLeft = 0;
6703 internalViewport->logicalTop = 0;
6704 internalViewport->logicalRight = DISPLAY_HEIGHT;
6705 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6706
6707 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6708 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6709 internalViewport->physicalRight = DISPLAY_HEIGHT;
6710 internalViewport->physicalBottom = DISPLAY_WIDTH;
6711
6712 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6713 internalViewport->deviceHeight = DISPLAY_WIDTH;
6714 mFakePolicy->updateViewport(internalViewport.value());
6715 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6716
6717 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006718 processPosition(mapper, rawX, rawY);
6719 processSync(mapper);
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6721}
6722
Michael Wrightd02c5b62014-02-10 15:10:22 -08006723} // namespace android