blob: 1fc8217df8d6f10ee22bea4fbc14ecdc52b284f9 [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
Powei Fengd041c5d2019-05-03 17:11:33 -07002583TEST_F(KeyboardInputMapperTest, ExternalDevice_WakeBehavior) {
2584 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2585 // marked as WAKE in the keylayout file to trigger wake.
2586 mDevice->setExternal(true);
2587
2588 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2589 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2590 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE, POLICY_FLAG_WAKE);
2591
2592 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2593 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2594 addMapperAndConfigure(mapper);
2595
2596 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2597 NotifyKeyArgs args;
2598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2599 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2600
2601 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2603 ASSERT_EQ(uint32_t(0), args.policyFlags);
2604
2605 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2607 ASSERT_EQ(uint32_t(0), args.policyFlags);
2608
2609 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2611 ASSERT_EQ(uint32_t(0), args.policyFlags);
2612
2613 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2615 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2616
2617 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2619 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2620}
2621
2622TEST_F(KeyboardInputMapperTest, ExternalDevice_DoNotWakeByDefaultBehavior) {
2623 // Tv Remote key's wake behavior is prescribed by the keylayout file.
2624 mDevice->setExternal(true);
2625
2626 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2627 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2628 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
2629
2630 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2631 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2632 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
2633 addMapperAndConfigure(mapper);
2634
2635 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2636 NotifyKeyArgs args;
2637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2638 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2639
2640 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2642 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2643
2644 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2646 ASSERT_EQ(uint32_t(0), args.policyFlags);
2647
2648 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2650 ASSERT_EQ(uint32_t(0), args.policyFlags);
2651
2652 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2654 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2655
2656 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2658 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2659}
2660
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661// --- CursorInputMapperTest ---
2662
2663class CursorInputMapperTest : public InputMapperTest {
2664protected:
2665 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2666
2667 sp<FakePointerController> mFakePointerController;
2668
Prabir Pradhan28efc192019-11-05 01:10:04 +00002669 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670 InputMapperTest::SetUp();
2671
2672 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002673 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 }
2675
2676 void testMotionRotation(CursorInputMapper* mapper,
2677 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002678
2679 void prepareDisplay(int32_t orientation) {
2680 const std::string uniqueId = "local:0";
2681 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2682 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2683 orientation, uniqueId, NO_PORT, viewportType);
2684 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685};
2686
2687const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2688
2689void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2690 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2691 NotifyMotionArgs args;
2692
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002693 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2694 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2695 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2699 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2700 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2701 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2702}
2703
2704TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2705 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2706 addConfigurationProperty("cursor.mode", "pointer");
2707 addMapperAndConfigure(mapper);
2708
2709 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2710}
2711
2712TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2713 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2714 addConfigurationProperty("cursor.mode", "navigation");
2715 addMapperAndConfigure(mapper);
2716
2717 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2718}
2719
2720TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2721 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2722 addConfigurationProperty("cursor.mode", "pointer");
2723 addMapperAndConfigure(mapper);
2724
2725 InputDeviceInfo info;
2726 mapper->populateDeviceInfo(&info);
2727
2728 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002729 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2730 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2732 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2733
2734 // When the bounds are set, then there should be a valid motion range.
2735 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2736
2737 InputDeviceInfo info2;
2738 mapper->populateDeviceInfo(&info2);
2739
2740 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2741 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2742 1, 800 - 1, 0.0f, 0.0f));
2743 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2744 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2745 2, 480 - 1, 0.0f, 0.0f));
2746 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2747 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2748 0.0f, 1.0f, 0.0f, 0.0f));
2749}
2750
2751TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2752 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2753 addConfigurationProperty("cursor.mode", "navigation");
2754 addMapperAndConfigure(mapper);
2755
2756 InputDeviceInfo info;
2757 mapper->populateDeviceInfo(&info);
2758
2759 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2760 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2761 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2762 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2763 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2764 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2765 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2766 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2767 0.0f, 1.0f, 0.0f, 0.0f));
2768}
2769
2770TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2771 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2772 addConfigurationProperty("cursor.mode", "navigation");
2773 addMapperAndConfigure(mapper);
2774
2775 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2776
2777 NotifyMotionArgs args;
2778
2779 // Button press.
2780 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002781 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2782 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2784 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2785 ASSERT_EQ(DEVICE_ID, args.deviceId);
2786 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2787 ASSERT_EQ(uint32_t(0), args.policyFlags);
2788 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2789 ASSERT_EQ(0, args.flags);
2790 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2791 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2792 ASSERT_EQ(0, args.edgeFlags);
2793 ASSERT_EQ(uint32_t(1), args.pointerCount);
2794 ASSERT_EQ(0, args.pointerProperties[0].id);
2795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2797 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2798 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2799 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2800 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2801
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2803 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2804 ASSERT_EQ(DEVICE_ID, args.deviceId);
2805 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2806 ASSERT_EQ(uint32_t(0), args.policyFlags);
2807 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2808 ASSERT_EQ(0, args.flags);
2809 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2810 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2811 ASSERT_EQ(0, args.edgeFlags);
2812 ASSERT_EQ(uint32_t(1), args.pointerCount);
2813 ASSERT_EQ(0, args.pointerProperties[0].id);
2814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2816 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2817 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2818 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2819 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2820
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002822 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2823 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2825 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2826 ASSERT_EQ(DEVICE_ID, args.deviceId);
2827 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2828 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002829 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2830 ASSERT_EQ(0, args.flags);
2831 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2832 ASSERT_EQ(0, args.buttonState);
2833 ASSERT_EQ(0, args.edgeFlags);
2834 ASSERT_EQ(uint32_t(1), args.pointerCount);
2835 ASSERT_EQ(0, args.pointerProperties[0].id);
2836 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2838 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2839 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2840 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2841 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2842
2843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2844 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2845 ASSERT_EQ(DEVICE_ID, args.deviceId);
2846 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2847 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2849 ASSERT_EQ(0, args.flags);
2850 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2851 ASSERT_EQ(0, args.buttonState);
2852 ASSERT_EQ(0, args.edgeFlags);
2853 ASSERT_EQ(uint32_t(1), args.pointerCount);
2854 ASSERT_EQ(0, args.pointerProperties[0].id);
2855 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2857 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2858 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2859 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2860 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2861}
2862
2863TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2864 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2865 addConfigurationProperty("cursor.mode", "navigation");
2866 addMapperAndConfigure(mapper);
2867
2868 NotifyMotionArgs args;
2869
2870 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002871 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2872 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2876 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2877
2878 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002879 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2880 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2884 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2885}
2886
2887TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2888 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2889 addConfigurationProperty("cursor.mode", "navigation");
2890 addMapperAndConfigure(mapper);
2891
2892 NotifyMotionArgs args;
2893
2894 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002895 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2896 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2898 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2900 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2901
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2903 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2905 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2906
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002908 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2909 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2913 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2914
2915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2918 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2919}
2920
2921TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2922 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2923 addConfigurationProperty("cursor.mode", "navigation");
2924 addMapperAndConfigure(mapper);
2925
2926 NotifyMotionArgs args;
2927
2928 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002929 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2930 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2931 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2932 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2934 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2936 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2937 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2938
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2942 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2943 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2944
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002946 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2947 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2948 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2952 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2953 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2954
2955 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002956 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2957 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2961 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2962
2963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2966 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2967}
2968
2969TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2970 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2971 addConfigurationProperty("cursor.mode", "navigation");
2972 addMapperAndConfigure(mapper);
2973
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002974 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2976 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2977 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2978 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2979 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2980 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2981 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2982 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2983}
2984
2985TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2986 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2987 addConfigurationProperty("cursor.mode", "navigation");
2988 addConfigurationProperty("cursor.orientationAware", "1");
2989 addMapperAndConfigure(mapper);
2990
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002991 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2993 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2994 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2995 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2996 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2997 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2998 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2999 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3000
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003001 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003002 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3003 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3004 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3005 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3006 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3007 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3008 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3009 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3010
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003011 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3013 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3014 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3015 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3016 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3017 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3018 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3019 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3020
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003021 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3023 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3024 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3025 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3026 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3027 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3028 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3029 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3030}
3031
3032TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
3033 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3034 addConfigurationProperty("cursor.mode", "pointer");
3035 addMapperAndConfigure(mapper);
3036
3037 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3038 mFakePointerController->setPosition(100, 200);
3039 mFakePointerController->setButtonState(0);
3040
3041 NotifyMotionArgs motionArgs;
3042 NotifyKeyArgs keyArgs;
3043
3044 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003045 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3046 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3048 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3049 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3050 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3052 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3053
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3056 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3057 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3059 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3060
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003061 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3062 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 ASSERT_EQ(0, motionArgs.buttonState);
3066 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 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));
3069
3070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003071 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072 ASSERT_EQ(0, motionArgs.buttonState);
3073 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003074 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003079 ASSERT_EQ(0, motionArgs.buttonState);
3080 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3082 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3083
3084 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003085 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3086 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3087 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3089 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3090 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3091 motionArgs.buttonState);
3092 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3093 mFakePointerController->getButtonState());
3094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3095 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3096
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3099 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3100 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3101 mFakePointerController->getButtonState());
3102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3103 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3104
3105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3107 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3108 motionArgs.buttonState);
3109 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3110 mFakePointerController->getButtonState());
3111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3112 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3113
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003114 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3115 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3119 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3121 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3122
3123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003125 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3126 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3128 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3129
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003130 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3131 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3134 ASSERT_EQ(0, motionArgs.buttonState);
3135 ASSERT_EQ(0, mFakePointerController->getButtonState());
3136 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));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003138 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3139 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003140
3141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142 ASSERT_EQ(0, motionArgs.buttonState);
3143 ASSERT_EQ(0, mFakePointerController->getButtonState());
3144 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3145 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3149 ASSERT_EQ(0, motionArgs.buttonState);
3150 ASSERT_EQ(0, mFakePointerController->getButtonState());
3151 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
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
3155 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003156 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3157 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3159 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3160 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003161
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003163 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003164 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3165 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3167 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3168
3169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3170 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3171 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3172 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3174 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3175
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003176 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3177 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180 ASSERT_EQ(0, motionArgs.buttonState);
3181 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3183 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3184
3185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003186 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003187 ASSERT_EQ(0, motionArgs.buttonState);
3188 ASSERT_EQ(0, mFakePointerController->getButtonState());
3189
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3191 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3193 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3194 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3195
3196 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003197 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3198 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3200 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3201 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003202
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003204 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003205 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3206 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3208 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3209
3210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3211 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3212 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3213 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3215 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3216
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003217 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3218 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221 ASSERT_EQ(0, motionArgs.buttonState);
3222 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3224 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003225
3226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3227 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3228 ASSERT_EQ(0, motionArgs.buttonState);
3229 ASSERT_EQ(0, mFakePointerController->getButtonState());
3230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3231 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3232
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3234 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3235 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3236
3237 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003238 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3239 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3241 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3242 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003243
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003245 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3247 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3249 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3250
3251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3253 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3254 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3256 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3257
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003258 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3259 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262 ASSERT_EQ(0, motionArgs.buttonState);
3263 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3265 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 -08003266
3267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3269 ASSERT_EQ(0, motionArgs.buttonState);
3270 ASSERT_EQ(0, mFakePointerController->getButtonState());
3271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3272 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3273
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3275 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3276 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3277
3278 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003279 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3280 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3282 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3283 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003284
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3288 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3290 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3291
3292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3294 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3295 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3297 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3298
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003299 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3300 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303 ASSERT_EQ(0, motionArgs.buttonState);
3304 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3306 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 -08003307
3308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3309 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3310 ASSERT_EQ(0, motionArgs.buttonState);
3311 ASSERT_EQ(0, mFakePointerController->getButtonState());
3312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3313 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3314
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3316 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3317 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3318}
3319
3320TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3321 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3322 addConfigurationProperty("cursor.mode", "pointer");
3323 addMapperAndConfigure(mapper);
3324
3325 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3326 mFakePointerController->setPosition(100, 200);
3327 mFakePointerController->setButtonState(0);
3328
3329 NotifyMotionArgs args;
3330
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003331 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3332 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3333 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003335 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3336 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3338 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3339 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3340}
3341
3342TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3343 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3344 addConfigurationProperty("cursor.mode", "pointer");
3345 mFakePolicy->setPointerCapture(true);
3346 addMapperAndConfigure(mapper);
3347
3348 NotifyDeviceResetArgs resetArgs;
3349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3350 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3351 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3352
3353 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3354 mFakePointerController->setPosition(100, 200);
3355 mFakePointerController->setButtonState(0);
3356
3357 NotifyMotionArgs args;
3358
3359 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003360 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3361 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3362 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3364 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3367 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3368 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3369
3370 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003371 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3372 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3374 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3375 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3377 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3379 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3382 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3383
3384 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003385 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3386 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3388 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3391 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3393 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3394 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3396 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3397
3398 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003399 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3400 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3401 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3403 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3406 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3407 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3408
3409 // Disable pointer capture and check that the device generation got bumped
3410 // and events are generated the usual way.
3411 const uint32_t generation = mFakeContext->getGeneration();
3412 mFakePolicy->setPointerCapture(false);
3413 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3414 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3415
3416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3417 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3418 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3419
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003420 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3421 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3422 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3424 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3427 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3428 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3429}
3430
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003431TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3432 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3433 addMapperAndConfigure(mapper);
3434
3435 // Setup PointerController for second display.
3436 constexpr int32_t SECOND_DISPLAY_ID = 1;
3437 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3438 mFakePointerController->setPosition(100, 200);
3439 mFakePointerController->setButtonState(0);
3440 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3441
3442 NotifyMotionArgs args;
3443 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3444 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3445 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3447 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3448 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3450 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3451 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3452 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3453}
3454
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455
3456// --- TouchInputMapperTest ---
3457
3458class TouchInputMapperTest : public InputMapperTest {
3459protected:
3460 static const int32_t RAW_X_MIN;
3461 static const int32_t RAW_X_MAX;
3462 static const int32_t RAW_Y_MIN;
3463 static const int32_t RAW_Y_MAX;
3464 static const int32_t RAW_TOUCH_MIN;
3465 static const int32_t RAW_TOUCH_MAX;
3466 static const int32_t RAW_TOOL_MIN;
3467 static const int32_t RAW_TOOL_MAX;
3468 static const int32_t RAW_PRESSURE_MIN;
3469 static const int32_t RAW_PRESSURE_MAX;
3470 static const int32_t RAW_ORIENTATION_MIN;
3471 static const int32_t RAW_ORIENTATION_MAX;
3472 static const int32_t RAW_DISTANCE_MIN;
3473 static const int32_t RAW_DISTANCE_MAX;
3474 static const int32_t RAW_TILT_MIN;
3475 static const int32_t RAW_TILT_MAX;
3476 static const int32_t RAW_ID_MIN;
3477 static const int32_t RAW_ID_MAX;
3478 static const int32_t RAW_SLOT_MIN;
3479 static const int32_t RAW_SLOT_MAX;
3480 static const float X_PRECISION;
3481 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003482 static const float X_PRECISION_VIRTUAL;
3483 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484
3485 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003486 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487
3488 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3489
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003490 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003491 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003492
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493 enum Axes {
3494 POSITION = 1 << 0,
3495 TOUCH = 1 << 1,
3496 TOOL = 1 << 2,
3497 PRESSURE = 1 << 3,
3498 ORIENTATION = 1 << 4,
3499 MINOR = 1 << 5,
3500 ID = 1 << 6,
3501 DISTANCE = 1 << 7,
3502 TILT = 1 << 8,
3503 SLOT = 1 << 9,
3504 TOOL_TYPE = 1 << 10,
3505 };
3506
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003507 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3508 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003509 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003511 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512 int32_t toRawX(float displayX);
3513 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003514 float toCookedX(float rawX, float rawY);
3515 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003517 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003519 float toDisplayY(int32_t rawY, int32_t displayHeight);
3520
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521};
3522
3523const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3524const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3525const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3526const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3527const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3528const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3529const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3530const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003531const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3532const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3534const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3535const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3536const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3537const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3538const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3539const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3540const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3541const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3542const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3543const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3544const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003545const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3546 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3547const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3548 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003549const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3550 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551
3552const float TouchInputMapperTest::GEOMETRIC_SCALE =
3553 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3554 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3555
3556const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3557 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3558 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3559};
3560
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003561void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003562 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003563 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3564}
3565
3566void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3567 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3568 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569}
3570
Santos Cordonfa5cf462017-04-05 10:37:00 -07003571void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003572 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3573 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003574 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003575}
3576
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577void TouchInputMapperTest::prepareVirtualKeys() {
3578 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3579 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3580 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3581 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3582}
3583
Jason Gerecke489fda82012-09-07 17:19:40 -07003584void TouchInputMapperTest::prepareLocationCalibration() {
3585 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3586}
3587
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588int32_t TouchInputMapperTest::toRawX(float displayX) {
3589 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3590}
3591
3592int32_t TouchInputMapperTest::toRawY(float displayY) {
3593 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3594}
3595
Jason Gerecke489fda82012-09-07 17:19:40 -07003596float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3597 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3598 return rawX;
3599}
3600
3601float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3602 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3603 return rawY;
3604}
3605
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003607 return toDisplayX(rawX, DISPLAY_WIDTH);
3608}
3609
3610float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3611 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612}
3613
3614float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003615 return toDisplayY(rawY, DISPLAY_HEIGHT);
3616}
3617
3618float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3619 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620}
3621
3622
3623// --- SingleTouchInputMapperTest ---
3624
3625class SingleTouchInputMapperTest : public TouchInputMapperTest {
3626protected:
3627 void prepareButtons();
3628 void prepareAxes(int axes);
3629
3630 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3631 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3632 void processUp(SingleTouchInputMapper* mappery);
3633 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3634 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3635 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3636 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3637 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3638 void processSync(SingleTouchInputMapper* mapper);
3639};
3640
3641void SingleTouchInputMapperTest::prepareButtons() {
3642 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3643}
3644
3645void SingleTouchInputMapperTest::prepareAxes(int axes) {
3646 if (axes & POSITION) {
3647 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3648 RAW_X_MIN, RAW_X_MAX, 0, 0);
3649 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3650 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3651 }
3652 if (axes & PRESSURE) {
3653 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3654 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3655 }
3656 if (axes & TOOL) {
3657 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3658 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3659 }
3660 if (axes & DISTANCE) {
3661 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3662 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3663 }
3664 if (axes & TILT) {
3665 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3666 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3667 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3668 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3669 }
3670}
3671
3672void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003673 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3674 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3675 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003676}
3677
3678void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003679 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3680 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681}
3682
3683void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003684 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003685}
3686
3687void SingleTouchInputMapperTest::processPressure(
3688 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003689 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003690}
3691
3692void SingleTouchInputMapperTest::processToolMajor(
3693 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003694 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003695}
3696
3697void SingleTouchInputMapperTest::processDistance(
3698 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003699 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700}
3701
3702void SingleTouchInputMapperTest::processTilt(
3703 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003704 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3705 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706}
3707
3708void SingleTouchInputMapperTest::processKey(
3709 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003710 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711}
3712
3713void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003714 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715}
3716
3717
3718TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3719 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3720 prepareButtons();
3721 prepareAxes(POSITION);
3722 addMapperAndConfigure(mapper);
3723
3724 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3725}
3726
3727TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3728 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3729 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3730 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3731 prepareButtons();
3732 prepareAxes(POSITION);
3733 addMapperAndConfigure(mapper);
3734
3735 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3736}
3737
3738TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3739 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3740 prepareButtons();
3741 prepareAxes(POSITION);
3742 addConfigurationProperty("touch.deviceType", "touchPad");
3743 addMapperAndConfigure(mapper);
3744
3745 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3746}
3747
3748TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3749 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3750 prepareButtons();
3751 prepareAxes(POSITION);
3752 addConfigurationProperty("touch.deviceType", "touchScreen");
3753 addMapperAndConfigure(mapper);
3754
3755 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3756}
3757
3758TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3759 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3760 addConfigurationProperty("touch.deviceType", "touchScreen");
3761 prepareDisplay(DISPLAY_ORIENTATION_0);
3762 prepareButtons();
3763 prepareAxes(POSITION);
3764 prepareVirtualKeys();
3765 addMapperAndConfigure(mapper);
3766
3767 // Unknown key.
3768 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3769
3770 // Virtual key is down.
3771 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3772 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3773 processDown(mapper, x, y);
3774 processSync(mapper);
3775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3776
3777 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3778
3779 // Virtual key is up.
3780 processUp(mapper);
3781 processSync(mapper);
3782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3783
3784 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3785}
3786
3787TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3788 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3789 addConfigurationProperty("touch.deviceType", "touchScreen");
3790 prepareDisplay(DISPLAY_ORIENTATION_0);
3791 prepareButtons();
3792 prepareAxes(POSITION);
3793 prepareVirtualKeys();
3794 addMapperAndConfigure(mapper);
3795
3796 // Unknown key.
3797 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3798
3799 // Virtual key is down.
3800 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3801 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3802 processDown(mapper, x, y);
3803 processSync(mapper);
3804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3805
3806 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3807
3808 // Virtual key is up.
3809 processUp(mapper);
3810 processSync(mapper);
3811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3812
3813 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3814}
3815
3816TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3817 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3818 addConfigurationProperty("touch.deviceType", "touchScreen");
3819 prepareDisplay(DISPLAY_ORIENTATION_0);
3820 prepareButtons();
3821 prepareAxes(POSITION);
3822 prepareVirtualKeys();
3823 addMapperAndConfigure(mapper);
3824
3825 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3826 uint8_t flags[2] = { 0, 0 };
3827 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3828 ASSERT_TRUE(flags[0]);
3829 ASSERT_FALSE(flags[1]);
3830}
3831
3832TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3833 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3834 addConfigurationProperty("touch.deviceType", "touchScreen");
3835 prepareDisplay(DISPLAY_ORIENTATION_0);
3836 prepareButtons();
3837 prepareAxes(POSITION);
3838 prepareVirtualKeys();
3839 addMapperAndConfigure(mapper);
3840
3841 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3842
3843 NotifyKeyArgs args;
3844
3845 // Press virtual key.
3846 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3847 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3848 processDown(mapper, x, y);
3849 processSync(mapper);
3850
3851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3852 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3853 ASSERT_EQ(DEVICE_ID, args.deviceId);
3854 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3855 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3856 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3857 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3858 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3859 ASSERT_EQ(KEY_HOME, args.scanCode);
3860 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3861 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3862
3863 // Release virtual key.
3864 processUp(mapper);
3865 processSync(mapper);
3866
3867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3868 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3869 ASSERT_EQ(DEVICE_ID, args.deviceId);
3870 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3871 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3872 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3873 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3874 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3875 ASSERT_EQ(KEY_HOME, args.scanCode);
3876 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3877 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3878
3879 // Should not have sent any motions.
3880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3881}
3882
3883TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3884 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3885 addConfigurationProperty("touch.deviceType", "touchScreen");
3886 prepareDisplay(DISPLAY_ORIENTATION_0);
3887 prepareButtons();
3888 prepareAxes(POSITION);
3889 prepareVirtualKeys();
3890 addMapperAndConfigure(mapper);
3891
3892 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3893
3894 NotifyKeyArgs keyArgs;
3895
3896 // Press virtual key.
3897 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3898 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3899 processDown(mapper, x, y);
3900 processSync(mapper);
3901
3902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3903 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3904 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3905 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3906 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3907 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3908 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3909 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3910 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3911 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3912 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3913
3914 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3915 // into the display area.
3916 y -= 100;
3917 processMove(mapper, x, y);
3918 processSync(mapper);
3919
3920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3921 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3922 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3923 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3924 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3925 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3926 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3927 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3928 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3929 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3930 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3931 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3932
3933 NotifyMotionArgs motionArgs;
3934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3935 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3936 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3937 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3938 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3939 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3940 ASSERT_EQ(0, motionArgs.flags);
3941 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3942 ASSERT_EQ(0, motionArgs.buttonState);
3943 ASSERT_EQ(0, motionArgs.edgeFlags);
3944 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3945 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3948 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3949 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3950 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3951 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3952
3953 // Keep moving out of bounds. Should generate a pointer move.
3954 y -= 50;
3955 processMove(mapper, x, y);
3956 processSync(mapper);
3957
3958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3959 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3960 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3961 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3962 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3964 ASSERT_EQ(0, motionArgs.flags);
3965 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3966 ASSERT_EQ(0, motionArgs.buttonState);
3967 ASSERT_EQ(0, motionArgs.edgeFlags);
3968 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3969 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3972 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3973 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3974 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3975 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3976
3977 // Release out of bounds. Should generate a pointer up.
3978 processUp(mapper);
3979 processSync(mapper);
3980
3981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3982 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3983 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3984 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3985 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3986 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3987 ASSERT_EQ(0, motionArgs.flags);
3988 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3989 ASSERT_EQ(0, motionArgs.buttonState);
3990 ASSERT_EQ(0, motionArgs.edgeFlags);
3991 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3992 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3995 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3996 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3997 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3998 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3999
4000 // Should not have sent any more keys or motions.
4001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4003}
4004
4005TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
4006 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4007 addConfigurationProperty("touch.deviceType", "touchScreen");
4008 prepareDisplay(DISPLAY_ORIENTATION_0);
4009 prepareButtons();
4010 prepareAxes(POSITION);
4011 prepareVirtualKeys();
4012 addMapperAndConfigure(mapper);
4013
4014 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4015
4016 NotifyMotionArgs motionArgs;
4017
4018 // Initially go down out of bounds.
4019 int32_t x = -10;
4020 int32_t y = -10;
4021 processDown(mapper, x, y);
4022 processSync(mapper);
4023
4024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4025
4026 // Move into the display area. Should generate a pointer down.
4027 x = 50;
4028 y = 75;
4029 processMove(mapper, x, y);
4030 processSync(mapper);
4031
4032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4033 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4034 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4035 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4036 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4037 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4038 ASSERT_EQ(0, motionArgs.flags);
4039 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4040 ASSERT_EQ(0, motionArgs.buttonState);
4041 ASSERT_EQ(0, motionArgs.edgeFlags);
4042 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4043 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4044 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4046 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4047 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4048 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4049 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4050
4051 // Release. Should generate a pointer up.
4052 processUp(mapper);
4053 processSync(mapper);
4054
4055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4056 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4057 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4058 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4059 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4060 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4061 ASSERT_EQ(0, motionArgs.flags);
4062 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4063 ASSERT_EQ(0, motionArgs.buttonState);
4064 ASSERT_EQ(0, motionArgs.edgeFlags);
4065 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4066 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4069 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4070 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4071 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4072 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4073
4074 // Should not have sent any more keys or motions.
4075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4077}
4078
Santos Cordonfa5cf462017-04-05 10:37:00 -07004079TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
4080 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4081 addConfigurationProperty("touch.deviceType", "touchScreen");
4082 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4083
4084 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4085 prepareButtons();
4086 prepareAxes(POSITION);
4087 prepareVirtualKeys();
4088 addMapperAndConfigure(mapper);
4089
4090 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4091
4092 NotifyMotionArgs motionArgs;
4093
4094 // Down.
4095 int32_t x = 100;
4096 int32_t y = 125;
4097 processDown(mapper, x, y);
4098 processSync(mapper);
4099
4100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4101 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4102 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4103 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4104 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4105 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4106 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4107 ASSERT_EQ(0, motionArgs.flags);
4108 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4109 ASSERT_EQ(0, motionArgs.buttonState);
4110 ASSERT_EQ(0, motionArgs.edgeFlags);
4111 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4112 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4115 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4116 1, 0, 0, 0, 0, 0, 0, 0));
4117 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4118 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4119 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4120
4121 // Move.
4122 x += 50;
4123 y += 75;
4124 processMove(mapper, x, y);
4125 processSync(mapper);
4126
4127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4128 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4129 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4130 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4131 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4132 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4134 ASSERT_EQ(0, motionArgs.flags);
4135 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4136 ASSERT_EQ(0, motionArgs.buttonState);
4137 ASSERT_EQ(0, motionArgs.edgeFlags);
4138 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4139 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4142 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4143 1, 0, 0, 0, 0, 0, 0, 0));
4144 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4145 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4146 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4147
4148 // Up.
4149 processUp(mapper);
4150 processSync(mapper);
4151
4152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4153 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4154 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4155 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4156 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4157 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4158 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4159 ASSERT_EQ(0, motionArgs.flags);
4160 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4161 ASSERT_EQ(0, motionArgs.buttonState);
4162 ASSERT_EQ(0, motionArgs.edgeFlags);
4163 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4164 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4167 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4168 1, 0, 0, 0, 0, 0, 0, 0));
4169 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4170 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4171 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4172
4173 // Should not have sent any more keys or motions.
4174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4176}
4177
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
4179 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4180 addConfigurationProperty("touch.deviceType", "touchScreen");
4181 prepareDisplay(DISPLAY_ORIENTATION_0);
4182 prepareButtons();
4183 prepareAxes(POSITION);
4184 prepareVirtualKeys();
4185 addMapperAndConfigure(mapper);
4186
4187 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4188
4189 NotifyMotionArgs motionArgs;
4190
4191 // Down.
4192 int32_t x = 100;
4193 int32_t y = 125;
4194 processDown(mapper, x, y);
4195 processSync(mapper);
4196
4197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4198 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4199 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4200 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4201 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4202 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4203 ASSERT_EQ(0, motionArgs.flags);
4204 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4205 ASSERT_EQ(0, motionArgs.buttonState);
4206 ASSERT_EQ(0, motionArgs.edgeFlags);
4207 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4208 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4211 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4212 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4213 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4214 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4215
4216 // Move.
4217 x += 50;
4218 y += 75;
4219 processMove(mapper, x, y);
4220 processSync(mapper);
4221
4222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4224 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4225 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4226 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4228 ASSERT_EQ(0, motionArgs.flags);
4229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4230 ASSERT_EQ(0, motionArgs.buttonState);
4231 ASSERT_EQ(0, motionArgs.edgeFlags);
4232 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4236 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4237 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4238 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4240
4241 // Up.
4242 processUp(mapper);
4243 processSync(mapper);
4244
4245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4247 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4248 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4249 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4250 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4251 ASSERT_EQ(0, motionArgs.flags);
4252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4253 ASSERT_EQ(0, motionArgs.buttonState);
4254 ASSERT_EQ(0, motionArgs.edgeFlags);
4255 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4256 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4259 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4260 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4261 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4263
4264 // Should not have sent any more keys or motions.
4265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4267}
4268
4269TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
4270 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4271 addConfigurationProperty("touch.deviceType", "touchScreen");
4272 prepareButtons();
4273 prepareAxes(POSITION);
4274 addConfigurationProperty("touch.orientationAware", "0");
4275 addMapperAndConfigure(mapper);
4276
4277 NotifyMotionArgs args;
4278
4279 // Rotation 90.
4280 prepareDisplay(DISPLAY_ORIENTATION_90);
4281 processDown(mapper, toRawX(50), toRawY(75));
4282 processSync(mapper);
4283
4284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4285 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4286 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4287
4288 processUp(mapper);
4289 processSync(mapper);
4290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4291}
4292
4293TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4294 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4295 addConfigurationProperty("touch.deviceType", "touchScreen");
4296 prepareButtons();
4297 prepareAxes(POSITION);
4298 addMapperAndConfigure(mapper);
4299
4300 NotifyMotionArgs args;
4301
4302 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004303 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 prepareDisplay(DISPLAY_ORIENTATION_0);
4305 processDown(mapper, toRawX(50), toRawY(75));
4306 processSync(mapper);
4307
4308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4309 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4310 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4311
4312 processUp(mapper);
4313 processSync(mapper);
4314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4315
4316 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004317 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 prepareDisplay(DISPLAY_ORIENTATION_90);
4319 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4320 processSync(mapper);
4321
4322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4323 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4324 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4325
4326 processUp(mapper);
4327 processSync(mapper);
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4329
4330 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004331 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 prepareDisplay(DISPLAY_ORIENTATION_180);
4333 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4334 processSync(mapper);
4335
4336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4337 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4338 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4339
4340 processUp(mapper);
4341 processSync(mapper);
4342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4343
4344 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004345 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 prepareDisplay(DISPLAY_ORIENTATION_270);
4347 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4348 processSync(mapper);
4349
4350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4351 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4352 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4353
4354 processUp(mapper);
4355 processSync(mapper);
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4357}
4358
4359TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4360 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4361 addConfigurationProperty("touch.deviceType", "touchScreen");
4362 prepareDisplay(DISPLAY_ORIENTATION_0);
4363 prepareButtons();
4364 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4365 addMapperAndConfigure(mapper);
4366
4367 // These calculations are based on the input device calibration documentation.
4368 int32_t rawX = 100;
4369 int32_t rawY = 200;
4370 int32_t rawPressure = 10;
4371 int32_t rawToolMajor = 12;
4372 int32_t rawDistance = 2;
4373 int32_t rawTiltX = 30;
4374 int32_t rawTiltY = 110;
4375
4376 float x = toDisplayX(rawX);
4377 float y = toDisplayY(rawY);
4378 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4379 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4380 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4381 float distance = float(rawDistance);
4382
4383 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4384 float tiltScale = M_PI / 180;
4385 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4386 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4387 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4388 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4389
4390 processDown(mapper, rawX, rawY);
4391 processPressure(mapper, rawPressure);
4392 processToolMajor(mapper, rawToolMajor);
4393 processDistance(mapper, rawDistance);
4394 processTilt(mapper, rawTiltX, rawTiltY);
4395 processSync(mapper);
4396
4397 NotifyMotionArgs args;
4398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4400 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4401 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4402}
4403
Jason Gerecke489fda82012-09-07 17:19:40 -07004404TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4405 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4406 addConfigurationProperty("touch.deviceType", "touchScreen");
4407 prepareDisplay(DISPLAY_ORIENTATION_0);
4408 prepareLocationCalibration();
4409 prepareButtons();
4410 prepareAxes(POSITION);
4411 addMapperAndConfigure(mapper);
4412
4413 int32_t rawX = 100;
4414 int32_t rawY = 200;
4415
4416 float x = toDisplayX(toCookedX(rawX, rawY));
4417 float y = toDisplayY(toCookedY(rawX, rawY));
4418
4419 processDown(mapper, rawX, rawY);
4420 processSync(mapper);
4421
4422 NotifyMotionArgs args;
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4425 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4426}
4427
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4429 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4430 addConfigurationProperty("touch.deviceType", "touchScreen");
4431 prepareDisplay(DISPLAY_ORIENTATION_0);
4432 prepareButtons();
4433 prepareAxes(POSITION);
4434 addMapperAndConfigure(mapper);
4435
4436 NotifyMotionArgs motionArgs;
4437 NotifyKeyArgs keyArgs;
4438
4439 processDown(mapper, 100, 200);
4440 processSync(mapper);
4441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4442 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4443 ASSERT_EQ(0, motionArgs.buttonState);
4444
4445 // press BTN_LEFT, release BTN_LEFT
4446 processKey(mapper, BTN_LEFT, 1);
4447 processSync(mapper);
4448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4450 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4451
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4453 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4454 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4455
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 processKey(mapper, BTN_LEFT, 0);
4457 processSync(mapper);
4458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461
4462 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(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465
4466 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4467 processKey(mapper, BTN_RIGHT, 1);
4468 processKey(mapper, BTN_MIDDLE, 1);
4469 processSync(mapper);
4470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4472 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4473 motionArgs.buttonState);
4474
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4477 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4478
4479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4481 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4482 motionArgs.buttonState);
4483
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 processKey(mapper, BTN_RIGHT, 0);
4485 processSync(mapper);
4486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004489
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493
4494 processKey(mapper, BTN_MIDDLE, 0);
4495 processSync(mapper);
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004499
4500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004502 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503
4504 // press BTN_BACK, release BTN_BACK
4505 processKey(mapper, BTN_BACK, 1);
4506 processSync(mapper);
4507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4508 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4509 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004510
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004513 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4514
4515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4516 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4517 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518
4519 processKey(mapper, BTN_BACK, 0);
4520 processSync(mapper);
4521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004522 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004524
4525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004527 ASSERT_EQ(0, motionArgs.buttonState);
4528
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4530 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4531 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4532
4533 // press BTN_SIDE, release BTN_SIDE
4534 processKey(mapper, BTN_SIDE, 1);
4535 processSync(mapper);
4536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4537 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4538 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004539
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004542 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4543
4544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4546 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004547
4548 processKey(mapper, BTN_SIDE, 0);
4549 processSync(mapper);
4550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004551 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004552 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004553
4554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004556 ASSERT_EQ(0, motionArgs.buttonState);
4557
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4559 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4560 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4561
4562 // press BTN_FORWARD, release BTN_FORWARD
4563 processKey(mapper, BTN_FORWARD, 1);
4564 processSync(mapper);
4565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4566 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4567 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004568
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004571 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4572
4573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4574 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4575 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576
4577 processKey(mapper, BTN_FORWARD, 0);
4578 processSync(mapper);
4579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004580 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004582
4583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004585 ASSERT_EQ(0, motionArgs.buttonState);
4586
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4588 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4589 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4590
4591 // press BTN_EXTRA, release BTN_EXTRA
4592 processKey(mapper, BTN_EXTRA, 1);
4593 processSync(mapper);
4594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4595 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4596 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004597
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004600 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4601
4602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4604 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605
4606 processKey(mapper, BTN_EXTRA, 0);
4607 processSync(mapper);
4608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004609 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004611
4612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004614 ASSERT_EQ(0, motionArgs.buttonState);
4615
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4617 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4618 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4619
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4621
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622 // press BTN_STYLUS, release BTN_STYLUS
4623 processKey(mapper, BTN_STYLUS, 1);
4624 processSync(mapper);
4625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4628
4629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4631 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632
4633 processKey(mapper, BTN_STYLUS, 0);
4634 processSync(mapper);
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004638
4639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004641 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642
4643 // press BTN_STYLUS2, release BTN_STYLUS2
4644 processKey(mapper, BTN_STYLUS2, 1);
4645 processSync(mapper);
4646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4647 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004648 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4649
4650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4651 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4652 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653
4654 processKey(mapper, BTN_STYLUS2, 0);
4655 processSync(mapper);
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004657 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004659
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004662 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663
4664 // release touch
4665 processUp(mapper);
4666 processSync(mapper);
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4668 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4669 ASSERT_EQ(0, motionArgs.buttonState);
4670}
4671
4672TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4673 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4674 addConfigurationProperty("touch.deviceType", "touchScreen");
4675 prepareDisplay(DISPLAY_ORIENTATION_0);
4676 prepareButtons();
4677 prepareAxes(POSITION);
4678 addMapperAndConfigure(mapper);
4679
4680 NotifyMotionArgs motionArgs;
4681
4682 // default tool type is finger
4683 processDown(mapper, 100, 200);
4684 processSync(mapper);
4685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4686 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4688
4689 // eraser
4690 processKey(mapper, BTN_TOOL_RUBBER, 1);
4691 processSync(mapper);
4692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4695
4696 // stylus
4697 processKey(mapper, BTN_TOOL_RUBBER, 0);
4698 processKey(mapper, BTN_TOOL_PEN, 1);
4699 processSync(mapper);
4700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4703
4704 // brush
4705 processKey(mapper, BTN_TOOL_PEN, 0);
4706 processKey(mapper, BTN_TOOL_BRUSH, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
4711
4712 // pencil
4713 processKey(mapper, BTN_TOOL_BRUSH, 0);
4714 processKey(mapper, BTN_TOOL_PENCIL, 1);
4715 processSync(mapper);
4716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4719
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004720 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 processKey(mapper, BTN_TOOL_PENCIL, 0);
4722 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4723 processSync(mapper);
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4727
4728 // mouse
4729 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4730 processKey(mapper, BTN_TOOL_MOUSE, 1);
4731 processSync(mapper);
4732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4735
4736 // lens
4737 processKey(mapper, BTN_TOOL_MOUSE, 0);
4738 processKey(mapper, BTN_TOOL_LENS, 1);
4739 processSync(mapper);
4740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4743
4744 // double-tap
4745 processKey(mapper, BTN_TOOL_LENS, 0);
4746 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4747 processSync(mapper);
4748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4751
4752 // triple-tap
4753 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4754 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4755 processSync(mapper);
4756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4759
4760 // quad-tap
4761 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4762 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4763 processSync(mapper);
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4767
4768 // finger
4769 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4770 processKey(mapper, BTN_TOOL_FINGER, 1);
4771 processSync(mapper);
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4775
4776 // stylus trumps finger
4777 processKey(mapper, BTN_TOOL_PEN, 1);
4778 processSync(mapper);
4779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4782
4783 // eraser trumps stylus
4784 processKey(mapper, BTN_TOOL_RUBBER, 1);
4785 processSync(mapper);
4786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4789
4790 // mouse trumps eraser
4791 processKey(mapper, BTN_TOOL_MOUSE, 1);
4792 processSync(mapper);
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4796
4797 // back to default tool type
4798 processKey(mapper, BTN_TOOL_MOUSE, 0);
4799 processKey(mapper, BTN_TOOL_RUBBER, 0);
4800 processKey(mapper, BTN_TOOL_PEN, 0);
4801 processKey(mapper, BTN_TOOL_FINGER, 0);
4802 processSync(mapper);
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4806}
4807
4808TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4809 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4810 addConfigurationProperty("touch.deviceType", "touchScreen");
4811 prepareDisplay(DISPLAY_ORIENTATION_0);
4812 prepareButtons();
4813 prepareAxes(POSITION);
4814 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4815 addMapperAndConfigure(mapper);
4816
4817 NotifyMotionArgs motionArgs;
4818
4819 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4820 processKey(mapper, BTN_TOOL_FINGER, 1);
4821 processMove(mapper, 100, 200);
4822 processSync(mapper);
4823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4824 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4826 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4827
4828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4829 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4831 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4832
4833 // move a little
4834 processMove(mapper, 150, 250);
4835 processSync(mapper);
4836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4837 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4839 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4840
4841 // down when BTN_TOUCH is pressed, pressure defaults to 1
4842 processKey(mapper, BTN_TOUCH, 1);
4843 processSync(mapper);
4844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4845 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4847 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4848
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4850 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4852 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4853
4854 // up when BTN_TOUCH is released, hover restored
4855 processKey(mapper, BTN_TOUCH, 0);
4856 processSync(mapper);
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4860 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4861
4862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4863 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4865 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4866
4867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4868 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4870 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4871
4872 // exit hover when pointer goes away
4873 processKey(mapper, BTN_TOOL_FINGER, 0);
4874 processSync(mapper);
4875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4876 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4878 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4879}
4880
4881TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4882 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4883 addConfigurationProperty("touch.deviceType", "touchScreen");
4884 prepareDisplay(DISPLAY_ORIENTATION_0);
4885 prepareButtons();
4886 prepareAxes(POSITION | PRESSURE);
4887 addMapperAndConfigure(mapper);
4888
4889 NotifyMotionArgs motionArgs;
4890
4891 // initially hovering because pressure is 0
4892 processDown(mapper, 100, 200);
4893 processPressure(mapper, 0);
4894 processSync(mapper);
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4896 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4898 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4899
4900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4901 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4903 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4904
4905 // move a little
4906 processMove(mapper, 150, 250);
4907 processSync(mapper);
4908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4909 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4911 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4912
4913 // down when pressure is non-zero
4914 processPressure(mapper, RAW_PRESSURE_MAX);
4915 processSync(mapper);
4916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4917 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4919 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4920
4921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4922 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4924 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4925
4926 // up when pressure becomes 0, hover restored
4927 processPressure(mapper, 0);
4928 processSync(mapper);
4929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4930 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4932 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4933
4934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4935 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4937 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4938
4939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4940 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4942 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4943
4944 // exit hover when pointer goes away
4945 processUp(mapper);
4946 processSync(mapper);
4947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4948 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4950 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4951}
4952
Dan Harmsaca28402018-12-17 13:55:20 -08004953
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954// --- MultiTouchInputMapperTest ---
4955
4956class MultiTouchInputMapperTest : public TouchInputMapperTest {
4957protected:
4958 void prepareAxes(int axes);
4959
4960 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4961 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4962 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4963 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4964 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4965 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4966 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4967 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4968 void processId(MultiTouchInputMapper* mapper, int32_t id);
4969 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4970 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4971 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4972 void processMTSync(MultiTouchInputMapper* mapper);
4973 void processSync(MultiTouchInputMapper* mapper);
4974};
4975
4976void MultiTouchInputMapperTest::prepareAxes(int axes) {
4977 if (axes & POSITION) {
4978 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4979 RAW_X_MIN, RAW_X_MAX, 0, 0);
4980 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4981 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4982 }
4983 if (axes & TOUCH) {
4984 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4985 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4986 if (axes & MINOR) {
4987 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4988 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4989 }
4990 }
4991 if (axes & TOOL) {
4992 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4993 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4994 if (axes & MINOR) {
4995 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4996 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4997 }
4998 }
4999 if (axes & ORIENTATION) {
5000 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
5001 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
5002 }
5003 if (axes & PRESSURE) {
5004 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
5005 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
5006 }
5007 if (axes & DISTANCE) {
5008 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
5009 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
5010 }
5011 if (axes & ID) {
5012 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
5013 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
5014 }
5015 if (axes & SLOT) {
5016 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
5017 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5018 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
5019 }
5020 if (axes & TOOL_TYPE) {
5021 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
5022 0, MT_TOOL_MAX, 0, 0);
5023 }
5024}
5025
5026void MultiTouchInputMapperTest::processPosition(
5027 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005028 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5029 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030}
5031
5032void MultiTouchInputMapperTest::processTouchMajor(
5033 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005034 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035}
5036
5037void MultiTouchInputMapperTest::processTouchMinor(
5038 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005039 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040}
5041
5042void MultiTouchInputMapperTest::processToolMajor(
5043 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005044 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045}
5046
5047void MultiTouchInputMapperTest::processToolMinor(
5048 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005049 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050}
5051
5052void MultiTouchInputMapperTest::processOrientation(
5053 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005054 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055}
5056
5057void MultiTouchInputMapperTest::processPressure(
5058 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005059 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060}
5061
5062void MultiTouchInputMapperTest::processDistance(
5063 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005064 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065}
5066
5067void MultiTouchInputMapperTest::processId(
5068 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005069 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070}
5071
5072void MultiTouchInputMapperTest::processSlot(
5073 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005074 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075}
5076
5077void MultiTouchInputMapperTest::processToolType(
5078 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005079 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080}
5081
5082void MultiTouchInputMapperTest::processKey(
5083 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005084 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085}
5086
5087void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005088 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089}
5090
5091void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005092 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093}
5094
5095
5096TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
5097 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5098 addConfigurationProperty("touch.deviceType", "touchScreen");
5099 prepareDisplay(DISPLAY_ORIENTATION_0);
5100 prepareAxes(POSITION);
5101 prepareVirtualKeys();
5102 addMapperAndConfigure(mapper);
5103
5104 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5105
5106 NotifyMotionArgs motionArgs;
5107
5108 // Two fingers down at once.
5109 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5110 processPosition(mapper, x1, y1);
5111 processMTSync(mapper);
5112 processPosition(mapper, x2, y2);
5113 processMTSync(mapper);
5114 processSync(mapper);
5115
5116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5117 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5118 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5119 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5120 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5121 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5122 ASSERT_EQ(0, motionArgs.flags);
5123 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5124 ASSERT_EQ(0, motionArgs.buttonState);
5125 ASSERT_EQ(0, motionArgs.edgeFlags);
5126 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5127 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5130 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5131 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5132 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5133 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5134
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5136 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5137 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5138 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5139 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5140 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5141 motionArgs.action);
5142 ASSERT_EQ(0, motionArgs.flags);
5143 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5144 ASSERT_EQ(0, motionArgs.buttonState);
5145 ASSERT_EQ(0, motionArgs.edgeFlags);
5146 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5147 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5149 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5152 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5154 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5155 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5156 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5157 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5158
5159 // Move.
5160 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5161 processPosition(mapper, x1, y1);
5162 processMTSync(mapper);
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(2), motionArgs.pointerCount);
5178 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5180 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5183 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5185 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5186 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5187 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5188 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5189
5190 // First finger up.
5191 x2 += 15; y2 -= 20;
5192 processPosition(mapper, x2, y2);
5193 processMTSync(mapper);
5194 processSync(mapper);
5195
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5197 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5198 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5199 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5200 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5201 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5202 motionArgs.action);
5203 ASSERT_EQ(0, motionArgs.flags);
5204 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5205 ASSERT_EQ(0, motionArgs.buttonState);
5206 ASSERT_EQ(0, motionArgs.edgeFlags);
5207 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5208 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5210 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5213 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5215 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5216 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5217 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5218 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5219
5220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5221 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5222 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5223 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5224 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5226 ASSERT_EQ(0, motionArgs.flags);
5227 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5228 ASSERT_EQ(0, motionArgs.buttonState);
5229 ASSERT_EQ(0, motionArgs.edgeFlags);
5230 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5231 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5234 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5235 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5236 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5238
5239 // Move.
5240 x2 += 20; y2 -= 25;
5241 processPosition(mapper, x2, y2);
5242 processMTSync(mapper);
5243 processSync(mapper);
5244
5245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5247 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5248 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5249 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5251 ASSERT_EQ(0, motionArgs.flags);
5252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5253 ASSERT_EQ(0, motionArgs.buttonState);
5254 ASSERT_EQ(0, motionArgs.edgeFlags);
5255 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5256 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5259 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5260 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5261 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5263
5264 // New finger down.
5265 int32_t x3 = 700, y3 = 300;
5266 processPosition(mapper, x2, y2);
5267 processMTSync(mapper);
5268 processPosition(mapper, x3, y3);
5269 processMTSync(mapper);
5270 processSync(mapper);
5271
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5277 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5278 motionArgs.action);
5279 ASSERT_EQ(0, motionArgs.flags);
5280 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5281 ASSERT_EQ(0, motionArgs.buttonState);
5282 ASSERT_EQ(0, motionArgs.edgeFlags);
5283 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5286 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5289 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5291 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5292 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5293 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5295
5296 // Second finger up.
5297 x3 += 30; y3 -= 20;
5298 processPosition(mapper, x3, y3);
5299 processMTSync(mapper);
5300 processSync(mapper);
5301
5302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5303 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5304 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5305 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5306 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5307 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5308 motionArgs.action);
5309 ASSERT_EQ(0, motionArgs.flags);
5310 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5311 ASSERT_EQ(0, motionArgs.buttonState);
5312 ASSERT_EQ(0, motionArgs.edgeFlags);
5313 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5314 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5316 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5319 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5321 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5322 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5323 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5324 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5325
5326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5327 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5328 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5329 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5330 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5332 ASSERT_EQ(0, motionArgs.flags);
5333 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5334 ASSERT_EQ(0, motionArgs.buttonState);
5335 ASSERT_EQ(0, motionArgs.edgeFlags);
5336 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5340 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5341 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5342 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5344
5345 // Last finger up.
5346 processMTSync(mapper);
5347 processSync(mapper);
5348
5349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5354 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5355 ASSERT_EQ(0, motionArgs.flags);
5356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5357 ASSERT_EQ(0, motionArgs.buttonState);
5358 ASSERT_EQ(0, motionArgs.edgeFlags);
5359 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5363 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5364 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5365 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5367
5368 // Should not have sent any more keys or motions.
5369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5371}
5372
5373TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5374 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5375 addConfigurationProperty("touch.deviceType", "touchScreen");
5376 prepareDisplay(DISPLAY_ORIENTATION_0);
5377 prepareAxes(POSITION | ID);
5378 prepareVirtualKeys();
5379 addMapperAndConfigure(mapper);
5380
5381 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5382
5383 NotifyMotionArgs motionArgs;
5384
5385 // Two fingers down at once.
5386 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5387 processPosition(mapper, x1, y1);
5388 processId(mapper, 1);
5389 processMTSync(mapper);
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_DOWN, motionArgs.action);
5397 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5398 ASSERT_EQ(0, 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(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5402
5403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5404 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5405 motionArgs.action);
5406 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5407 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5409 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5412 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5414 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5415
5416 // Move.
5417 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5418 processPosition(mapper, x1, y1);
5419 processId(mapper, 1);
5420 processMTSync(mapper);
5421 processPosition(mapper, x2, y2);
5422 processId(mapper, 2);
5423 processMTSync(mapper);
5424 processSync(mapper);
5425
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5428 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5429 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5431 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5434 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5436 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5437
5438 // First finger up.
5439 x2 += 15; y2 -= 20;
5440 processPosition(mapper, x2, y2);
5441 processId(mapper, 2);
5442 processMTSync(mapper);
5443 processSync(mapper);
5444
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5446 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5447 motionArgs.action);
5448 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5449 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5451 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5454 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5456 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5457
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5460 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5461 ASSERT_EQ(1, 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(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5465
5466 // Move.
5467 x2 += 20; y2 -= 25;
5468 processPosition(mapper, x2, y2);
5469 processId(mapper, 2);
5470 processMTSync(mapper);
5471 processSync(mapper);
5472
5473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5475 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5476 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5479 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5480
5481 // New finger down.
5482 int32_t x3 = 700, y3 = 300;
5483 processPosition(mapper, x2, y2);
5484 processId(mapper, 2);
5485 processMTSync(mapper);
5486 processPosition(mapper, x3, y3);
5487 processId(mapper, 3);
5488 processMTSync(mapper);
5489 processSync(mapper);
5490
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5492 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5493 motionArgs.action);
5494 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5495 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5497 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5500 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5502 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5503
5504 // Second finger up.
5505 x3 += 30; y3 -= 20;
5506 processPosition(mapper, x3, y3);
5507 processId(mapper, 3);
5508 processMTSync(mapper);
5509 processSync(mapper);
5510
5511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5512 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5513 motionArgs.action);
5514 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5515 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5517 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5520 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5522 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5523
5524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5526 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5527 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5530 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5531
5532 // Last finger up.
5533 processMTSync(mapper);
5534 processSync(mapper);
5535
5536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5537 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5538 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5539 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5542 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5543
5544 // Should not have sent any more keys or motions.
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5547}
5548
5549TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5550 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5551 addConfigurationProperty("touch.deviceType", "touchScreen");
5552 prepareDisplay(DISPLAY_ORIENTATION_0);
5553 prepareAxes(POSITION | ID | SLOT);
5554 prepareVirtualKeys();
5555 addMapperAndConfigure(mapper);
5556
5557 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5558
5559 NotifyMotionArgs motionArgs;
5560
5561 // Two fingers down at once.
5562 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5563 processPosition(mapper, x1, y1);
5564 processId(mapper, 1);
5565 processSlot(mapper, 1);
5566 processPosition(mapper, x2, y2);
5567 processId(mapper, 2);
5568 processSync(mapper);
5569
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5572 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5573 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5576 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5577
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5579 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5580 motionArgs.action);
5581 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5582 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5583 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5584 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5587 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5589 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5590
5591 // Move.
5592 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5593 processSlot(mapper, 0);
5594 processPosition(mapper, x1, y1);
5595 processSlot(mapper, 1);
5596 processPosition(mapper, x2, y2);
5597 processSync(mapper);
5598
5599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5601 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5602 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5604 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5607 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5609 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5610
5611 // First finger up.
5612 x2 += 15; y2 -= 20;
5613 processSlot(mapper, 0);
5614 processId(mapper, -1);
5615 processSlot(mapper, 1);
5616 processPosition(mapper, x2, y2);
5617 processSync(mapper);
5618
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5620 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5621 motionArgs.action);
5622 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5623 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5625 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5628 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5630 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5631
5632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5634 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5635 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5638 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5639
5640 // Move.
5641 x2 += 20; y2 -= 25;
5642 processPosition(mapper, x2, y2);
5643 processSync(mapper);
5644
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5647 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5648 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5651 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5652
5653 // New finger down.
5654 int32_t x3 = 700, y3 = 300;
5655 processPosition(mapper, x2, y2);
5656 processSlot(mapper, 0);
5657 processId(mapper, 3);
5658 processPosition(mapper, x3, y3);
5659 processSync(mapper);
5660
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5662 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5663 motionArgs.action);
5664 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5665 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5666 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5667 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5670 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5672 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5673
5674 // Second finger up.
5675 x3 += 30; y3 -= 20;
5676 processSlot(mapper, 1);
5677 processId(mapper, -1);
5678 processSlot(mapper, 0);
5679 processPosition(mapper, x3, y3);
5680 processSync(mapper);
5681
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5683 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5684 motionArgs.action);
5685 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5686 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5688 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5691 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5693 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5697 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5698 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5701 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5702
5703 // Last finger up.
5704 processId(mapper, -1);
5705 processSync(mapper);
5706
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5708 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5709 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5710 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5713 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5714
5715 // Should not have sent any more keys or motions.
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5718}
5719
5720TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5721 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5722 addConfigurationProperty("touch.deviceType", "touchScreen");
5723 prepareDisplay(DISPLAY_ORIENTATION_0);
5724 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5725 addMapperAndConfigure(mapper);
5726
5727 // These calculations are based on the input device calibration documentation.
5728 int32_t rawX = 100;
5729 int32_t rawY = 200;
5730 int32_t rawTouchMajor = 7;
5731 int32_t rawTouchMinor = 6;
5732 int32_t rawToolMajor = 9;
5733 int32_t rawToolMinor = 8;
5734 int32_t rawPressure = 11;
5735 int32_t rawDistance = 0;
5736 int32_t rawOrientation = 3;
5737 int32_t id = 5;
5738
5739 float x = toDisplayX(rawX);
5740 float y = toDisplayY(rawY);
5741 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5742 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5743 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5744 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5745 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5746 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5747 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5748 float distance = float(rawDistance);
5749
5750 processPosition(mapper, rawX, rawY);
5751 processTouchMajor(mapper, rawTouchMajor);
5752 processTouchMinor(mapper, rawTouchMinor);
5753 processToolMajor(mapper, rawToolMajor);
5754 processToolMinor(mapper, rawToolMinor);
5755 processPressure(mapper, rawPressure);
5756 processOrientation(mapper, rawOrientation);
5757 processDistance(mapper, rawDistance);
5758 processId(mapper, id);
5759 processMTSync(mapper);
5760 processSync(mapper);
5761
5762 NotifyMotionArgs args;
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5764 ASSERT_EQ(0, args.pointerProperties[0].id);
5765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5766 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5767 orientation, distance));
5768}
5769
5770TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5771 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5772 addConfigurationProperty("touch.deviceType", "touchScreen");
5773 prepareDisplay(DISPLAY_ORIENTATION_0);
5774 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5775 addConfigurationProperty("touch.size.calibration", "geometric");
5776 addMapperAndConfigure(mapper);
5777
5778 // These calculations are based on the input device calibration documentation.
5779 int32_t rawX = 100;
5780 int32_t rawY = 200;
5781 int32_t rawTouchMajor = 140;
5782 int32_t rawTouchMinor = 120;
5783 int32_t rawToolMajor = 180;
5784 int32_t rawToolMinor = 160;
5785
5786 float x = toDisplayX(rawX);
5787 float y = toDisplayY(rawY);
5788 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5789 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5790 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5791 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5792 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5793
5794 processPosition(mapper, rawX, rawY);
5795 processTouchMajor(mapper, rawTouchMajor);
5796 processTouchMinor(mapper, rawTouchMinor);
5797 processToolMajor(mapper, rawToolMajor);
5798 processToolMinor(mapper, rawToolMinor);
5799 processMTSync(mapper);
5800 processSync(mapper);
5801
5802 NotifyMotionArgs args;
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5805 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5806}
5807
5808TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5809 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5810 addConfigurationProperty("touch.deviceType", "touchScreen");
5811 prepareDisplay(DISPLAY_ORIENTATION_0);
5812 prepareAxes(POSITION | TOUCH | TOOL);
5813 addConfigurationProperty("touch.size.calibration", "diameter");
5814 addConfigurationProperty("touch.size.scale", "10");
5815 addConfigurationProperty("touch.size.bias", "160");
5816 addConfigurationProperty("touch.size.isSummed", "1");
5817 addMapperAndConfigure(mapper);
5818
5819 // These calculations are based on the input device calibration documentation.
5820 // Note: We only provide a single common touch/tool value because the device is assumed
5821 // not to emit separate values for each pointer (isSummed = 1).
5822 int32_t rawX = 100;
5823 int32_t rawY = 200;
5824 int32_t rawX2 = 150;
5825 int32_t rawY2 = 250;
5826 int32_t rawTouchMajor = 5;
5827 int32_t rawToolMajor = 8;
5828
5829 float x = toDisplayX(rawX);
5830 float y = toDisplayY(rawY);
5831 float x2 = toDisplayX(rawX2);
5832 float y2 = toDisplayY(rawY2);
5833 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5834 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5835 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5836
5837 processPosition(mapper, rawX, rawY);
5838 processTouchMajor(mapper, rawTouchMajor);
5839 processToolMajor(mapper, rawToolMajor);
5840 processMTSync(mapper);
5841 processPosition(mapper, rawX2, rawY2);
5842 processTouchMajor(mapper, rawTouchMajor);
5843 processToolMajor(mapper, rawToolMajor);
5844 processMTSync(mapper);
5845 processSync(mapper);
5846
5847 NotifyMotionArgs args;
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5850
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5852 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5853 args.action);
5854 ASSERT_EQ(size_t(2), args.pointerCount);
5855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5856 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5858 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5859}
5860
5861TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5862 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5863 addConfigurationProperty("touch.deviceType", "touchScreen");
5864 prepareDisplay(DISPLAY_ORIENTATION_0);
5865 prepareAxes(POSITION | TOUCH | TOOL);
5866 addConfigurationProperty("touch.size.calibration", "area");
5867 addConfigurationProperty("touch.size.scale", "43");
5868 addConfigurationProperty("touch.size.bias", "3");
5869 addMapperAndConfigure(mapper);
5870
5871 // These calculations are based on the input device calibration documentation.
5872 int32_t rawX = 100;
5873 int32_t rawY = 200;
5874 int32_t rawTouchMajor = 5;
5875 int32_t rawToolMajor = 8;
5876
5877 float x = toDisplayX(rawX);
5878 float y = toDisplayY(rawY);
5879 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5880 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5881 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5882
5883 processPosition(mapper, rawX, rawY);
5884 processTouchMajor(mapper, rawTouchMajor);
5885 processToolMajor(mapper, rawToolMajor);
5886 processMTSync(mapper);
5887 processSync(mapper);
5888
5889 NotifyMotionArgs args;
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5892 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5893}
5894
5895TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5896 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5897 addConfigurationProperty("touch.deviceType", "touchScreen");
5898 prepareDisplay(DISPLAY_ORIENTATION_0);
5899 prepareAxes(POSITION | PRESSURE);
5900 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5901 addConfigurationProperty("touch.pressure.scale", "0.01");
5902 addMapperAndConfigure(mapper);
5903
Michael Wrightaa449c92017-12-13 21:21:43 +00005904 InputDeviceInfo info;
5905 mapper->populateDeviceInfo(&info);
5906 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5907 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5908 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5909
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 // These calculations are based on the input device calibration documentation.
5911 int32_t rawX = 100;
5912 int32_t rawY = 200;
5913 int32_t rawPressure = 60;
5914
5915 float x = toDisplayX(rawX);
5916 float y = toDisplayY(rawY);
5917 float pressure = float(rawPressure) * 0.01f;
5918
5919 processPosition(mapper, rawX, rawY);
5920 processPressure(mapper, rawPressure);
5921 processMTSync(mapper);
5922 processSync(mapper);
5923
5924 NotifyMotionArgs args;
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5927 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5928}
5929
5930TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5931 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5932 addConfigurationProperty("touch.deviceType", "touchScreen");
5933 prepareDisplay(DISPLAY_ORIENTATION_0);
5934 prepareAxes(POSITION | ID | SLOT);
5935 addMapperAndConfigure(mapper);
5936
5937 NotifyMotionArgs motionArgs;
5938 NotifyKeyArgs keyArgs;
5939
5940 processId(mapper, 1);
5941 processPosition(mapper, 100, 200);
5942 processSync(mapper);
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5945 ASSERT_EQ(0, motionArgs.buttonState);
5946
5947 // press BTN_LEFT, release BTN_LEFT
5948 processKey(mapper, BTN_LEFT, 1);
5949 processSync(mapper);
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5952 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5953
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5956 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5957
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958 processKey(mapper, BTN_LEFT, 0);
5959 processSync(mapper);
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005962 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963
5964 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(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967
5968 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5969 processKey(mapper, BTN_RIGHT, 1);
5970 processKey(mapper, BTN_MIDDLE, 1);
5971 processSync(mapper);
5972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5974 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5975 motionArgs.buttonState);
5976
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5979 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5980
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5982 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5983 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5984 motionArgs.buttonState);
5985
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986 processKey(mapper, BTN_RIGHT, 0);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005990 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005991
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005994 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995
5996 processKey(mapper, BTN_MIDDLE, 0);
5997 processSync(mapper);
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006001
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006004 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005
6006 // press BTN_BACK, release BTN_BACK
6007 processKey(mapper, BTN_BACK, 1);
6008 processSync(mapper);
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6010 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6011 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006012
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006015 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6016
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6018 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6019 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006020
6021 processKey(mapper, BTN_BACK, 0);
6022 processSync(mapper);
6023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006024 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006025 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006026
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006029 ASSERT_EQ(0, motionArgs.buttonState);
6030
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6032 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6033 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6034
6035 // press BTN_SIDE, release BTN_SIDE
6036 processKey(mapper, BTN_SIDE, 1);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6039 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6040 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006041
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006044 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6045
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6047 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6048 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006049
6050 processKey(mapper, BTN_SIDE, 0);
6051 processSync(mapper);
6052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006053 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006054 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006055
6056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006058 ASSERT_EQ(0, motionArgs.buttonState);
6059
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6061 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6062 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6063
6064 // press BTN_FORWARD, release BTN_FORWARD
6065 processKey(mapper, BTN_FORWARD, 1);
6066 processSync(mapper);
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6068 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6069 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006070
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006073 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6074
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6077 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078
6079 processKey(mapper, BTN_FORWARD, 0);
6080 processSync(mapper);
6081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006083 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006084
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006087 ASSERT_EQ(0, motionArgs.buttonState);
6088
Michael Wrightd02c5b62014-02-10 15:10:22 -08006089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6090 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6091 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6092
6093 // press BTN_EXTRA, release BTN_EXTRA
6094 processKey(mapper, BTN_EXTRA, 1);
6095 processSync(mapper);
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6097 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6098 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006099
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006102 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6103
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6106 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107
6108 processKey(mapper, BTN_EXTRA, 0);
6109 processSync(mapper);
6110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006113
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006116 ASSERT_EQ(0, motionArgs.buttonState);
6117
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6119 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6120 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6121
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6123
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124 // press BTN_STYLUS, release BTN_STYLUS
6125 processKey(mapper, BTN_STYLUS, 1);
6126 processSync(mapper);
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006129 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6130
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6133 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134
6135 processKey(mapper, BTN_STYLUS, 0);
6136 processSync(mapper);
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006140
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006143 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144
6145 // press BTN_STYLUS2, release BTN_STYLUS2
6146 processKey(mapper, BTN_STYLUS2, 1);
6147 processSync(mapper);
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006150 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6151
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6154 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155
6156 processKey(mapper, BTN_STYLUS2, 0);
6157 processSync(mapper);
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006161
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165
6166 // release touch
6167 processId(mapper, -1);
6168 processSync(mapper);
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6170 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6171 ASSERT_EQ(0, motionArgs.buttonState);
6172}
6173
6174TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
6175 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6176 addConfigurationProperty("touch.deviceType", "touchScreen");
6177 prepareDisplay(DISPLAY_ORIENTATION_0);
6178 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6179 addMapperAndConfigure(mapper);
6180
6181 NotifyMotionArgs motionArgs;
6182
6183 // default tool type is finger
6184 processId(mapper, 1);
6185 processPosition(mapper, 100, 200);
6186 processSync(mapper);
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6188 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6190
6191 // eraser
6192 processKey(mapper, BTN_TOOL_RUBBER, 1);
6193 processSync(mapper);
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6197
6198 // stylus
6199 processKey(mapper, BTN_TOOL_RUBBER, 0);
6200 processKey(mapper, BTN_TOOL_PEN, 1);
6201 processSync(mapper);
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6205
6206 // brush
6207 processKey(mapper, BTN_TOOL_PEN, 0);
6208 processKey(mapper, BTN_TOOL_BRUSH, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
6213
6214 // pencil
6215 processKey(mapper, BTN_TOOL_BRUSH, 0);
6216 processKey(mapper, BTN_TOOL_PENCIL, 1);
6217 processSync(mapper);
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6221
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006222 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006223 processKey(mapper, BTN_TOOL_PENCIL, 0);
6224 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6225 processSync(mapper);
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6229
6230 // mouse
6231 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6232 processKey(mapper, BTN_TOOL_MOUSE, 1);
6233 processSync(mapper);
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6237
6238 // lens
6239 processKey(mapper, BTN_TOOL_MOUSE, 0);
6240 processKey(mapper, BTN_TOOL_LENS, 1);
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_MOUSE, motionArgs.pointerProperties[0].toolType);
6245
6246 // double-tap
6247 processKey(mapper, BTN_TOOL_LENS, 0);
6248 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6249 processSync(mapper);
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6253
6254 // triple-tap
6255 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6256 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6257 processSync(mapper);
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6259 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6261
6262 // quad-tap
6263 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6264 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6269
6270 // finger
6271 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6272 processKey(mapper, BTN_TOOL_FINGER, 1);
6273 processSync(mapper);
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6277
6278 // stylus trumps finger
6279 processKey(mapper, BTN_TOOL_PEN, 1);
6280 processSync(mapper);
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6284
6285 // eraser trumps stylus
6286 processKey(mapper, BTN_TOOL_RUBBER, 1);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6291
6292 // mouse trumps eraser
6293 processKey(mapper, BTN_TOOL_MOUSE, 1);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6298
6299 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6300 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6305
6306 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6307 processToolType(mapper, MT_TOOL_PEN);
6308 processSync(mapper);
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6312
6313 // back to default tool type
6314 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6315 processKey(mapper, BTN_TOOL_MOUSE, 0);
6316 processKey(mapper, BTN_TOOL_RUBBER, 0);
6317 processKey(mapper, BTN_TOOL_PEN, 0);
6318 processKey(mapper, BTN_TOOL_FINGER, 0);
6319 processSync(mapper);
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6323}
6324
6325TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6326 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6327 addConfigurationProperty("touch.deviceType", "touchScreen");
6328 prepareDisplay(DISPLAY_ORIENTATION_0);
6329 prepareAxes(POSITION | ID | SLOT);
6330 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6331 addMapperAndConfigure(mapper);
6332
6333 NotifyMotionArgs motionArgs;
6334
6335 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6336 processId(mapper, 1);
6337 processPosition(mapper, 100, 200);
6338 processSync(mapper);
6339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6340 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6342 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6343
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6347 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6348
6349 // move a little
6350 processPosition(mapper, 150, 250);
6351 processSync(mapper);
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6355 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6356
6357 // down when BTN_TOUCH is pressed, pressure defaults to 1
6358 processKey(mapper, BTN_TOUCH, 1);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6363 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6364
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6368 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6369
6370 // up when BTN_TOUCH is released, hover restored
6371 processKey(mapper, BTN_TOUCH, 0);
6372 processSync(mapper);
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6376 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6377
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6379 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6381 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6382
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6384 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6386 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6387
6388 // exit hover when pointer goes away
6389 processId(mapper, -1);
6390 processSync(mapper);
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6394 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6395}
6396
6397TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6398 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6399 addConfigurationProperty("touch.deviceType", "touchScreen");
6400 prepareDisplay(DISPLAY_ORIENTATION_0);
6401 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6402 addMapperAndConfigure(mapper);
6403
6404 NotifyMotionArgs motionArgs;
6405
6406 // initially hovering because pressure is 0
6407 processId(mapper, 1);
6408 processPosition(mapper, 100, 200);
6409 processPressure(mapper, 0);
6410 processSync(mapper);
6411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6412 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6414 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6415
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6419 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6420
6421 // move a little
6422 processPosition(mapper, 150, 250);
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6425 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6427 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6428
6429 // down when pressure becomes non-zero
6430 processPressure(mapper, RAW_PRESSURE_MAX);
6431 processSync(mapper);
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6435 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6436
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6440 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6441
6442 // up when pressure becomes 0, hover restored
6443 processPressure(mapper, 0);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6448 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6449
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6453 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6454
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6458 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6459
6460 // exit hover when pointer goes away
6461 processId(mapper, -1);
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6464 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6466 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6467}
6468
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006469/**
6470 * Set the input device port <--> display port associations, and check that the
6471 * events are routed to the display that matches the display port.
6472 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6473 */
6474TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6475 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6476 const std::string usb2 = "USB2";
6477 const uint8_t hdmi1 = 0;
6478 const uint8_t hdmi2 = 1;
6479 const std::string secondaryUniqueId = "uniqueId2";
6480 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6481
6482 addConfigurationProperty("touch.deviceType", "touchScreen");
6483 prepareAxes(POSITION);
6484 addMapperAndConfigure(mapper);
6485
6486 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6487 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6488
6489 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6490 // for this input device is specified, and the matching viewport is not present,
6491 // the input device should be disabled (at the mapper level).
6492
6493 // Add viewport for display 2 on hdmi2
6494 prepareSecondaryDisplay(type, hdmi2);
6495 // Send a touch event
6496 processPosition(mapper, 100, 100);
6497 processSync(mapper);
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6499
6500 // Add viewport for display 1 on hdmi1
6501 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6502 // Send a touch event again
6503 processPosition(mapper, 100, 100);
6504 processSync(mapper);
6505
6506 NotifyMotionArgs args;
6507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6508 ASSERT_EQ(DISPLAY_ID, args.displayId);
6509}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006510
Arthur Hung41a712e2018-11-22 19:41:03 +08006511/**
6512 * Expect fallback to internal viewport if device is external and external viewport is not present.
6513 */
6514TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6515 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6516 prepareAxes(POSITION);
6517 addConfigurationProperty("touch.deviceType", "touchScreen");
6518 prepareDisplay(DISPLAY_ORIENTATION_0);
6519 mDevice->setExternal(true);
6520 addMapperAndConfigure(mapper);
6521
6522 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6523
6524 NotifyMotionArgs motionArgs;
6525
6526 // Expect the event to be sent to the internal viewport,
6527 // because an external viewport is not present.
6528 processPosition(mapper, 100, 100);
6529 processSync(mapper);
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6531 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6532
6533 // Expect the event to be sent to the external viewport if it is present.
6534 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6535 processPosition(mapper, 100, 100);
6536 processSync(mapper);
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6538 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6539}
6540
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006541TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6542 // Setup PointerController for second display.
6543 sp<FakePointerController> fakePointerController = new FakePointerController();
6544 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6545 fakePointerController->setPosition(100, 200);
6546 fakePointerController->setButtonState(0);
6547 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6548 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6549
6550 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6551 prepareDisplay(DISPLAY_ORIENTATION_0);
6552 prepareAxes(POSITION);
6553 addMapperAndConfigure(mapper);
6554
6555 // Check source is mouse that would obtain the PointerController.
6556 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6557
6558 NotifyMotionArgs motionArgs;
6559 processPosition(mapper, 100, 100);
6560 processSync(mapper);
6561
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6564 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6565}
6566
Arthur Hung7c645402019-01-25 17:45:42 +08006567TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6568 // Setup the first touch screen device.
6569 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6570 prepareAxes(POSITION | ID | SLOT);
6571 addConfigurationProperty("touch.deviceType", "touchScreen");
6572 addMapperAndConfigure(mapper);
6573
6574 // Create the second touch screen device, and enable multi fingers.
6575 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006576 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006577 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006578 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006579 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006580 std::unique_ptr<InputDevice> device2 =
6581 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6582 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006583 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6584 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6585 0 /*flat*/, 0 /*fuzz*/);
6586 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6587 0 /*flat*/, 0 /*fuzz*/);
6588 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6589 0 /*flat*/, 0 /*fuzz*/);
6590 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6591 0 /*flat*/, 0 /*fuzz*/);
6592 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6593 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6594 String8("touchScreen"));
6595
6596 // Setup the second touch screen device.
Arthur Hung2c9a3342019-07-23 14:18:59 +08006597 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2.get());
Arthur Hung7c645402019-01-25 17:45:42 +08006598 device2->addMapper(mapper2);
6599 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6600 device2->reset(ARBITRARY_TIME);
6601
6602 // Setup PointerController.
6603 sp<FakePointerController> fakePointerController = new FakePointerController();
6604 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6605 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6606
6607 // Setup policy for associated displays and show touches.
6608 const uint8_t hdmi1 = 0;
6609 const uint8_t hdmi2 = 1;
6610 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6611 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6612 mFakePolicy->setShowTouches(true);
6613
6614 // Create displays.
6615 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6616 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6617
6618 // Default device will reconfigure above, need additional reconfiguration for another device.
6619 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6620 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6621
6622 // Two fingers down at default display.
6623 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6624 processPosition(mapper, x1, y1);
6625 processId(mapper, 1);
6626 processSlot(mapper, 1);
6627 processPosition(mapper, x2, y2);
6628 processId(mapper, 2);
6629 processSync(mapper);
6630
6631 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6632 fakePointerController->getSpots().find(DISPLAY_ID);
6633 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6634 ASSERT_EQ(size_t(2), iter->second.size());
6635
6636 // Two fingers down at second display.
6637 processPosition(mapper2, x1, y1);
6638 processId(mapper2, 1);
6639 processSlot(mapper2, 1);
6640 processPosition(mapper2, x2, y2);
6641 processId(mapper2, 2);
6642 processSync(mapper2);
6643
6644 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6645 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6646 ASSERT_EQ(size_t(2), iter->second.size());
6647}
6648
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006649TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6650 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6651 prepareAxes(POSITION);
6652 addConfigurationProperty("touch.deviceType", "touchScreen");
6653 prepareDisplay(DISPLAY_ORIENTATION_0);
6654 addMapperAndConfigure(mapper);
6655
6656 NotifyMotionArgs motionArgs;
6657 // Unrotated video frame
6658 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6659 std::vector<TouchVideoFrame> frames{frame};
6660 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6661 processPosition(mapper, 100, 200);
6662 processSync(mapper);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6664 ASSERT_EQ(frames, motionArgs.videoFrames);
6665
6666 // Subsequent touch events should not have any videoframes
6667 // This is implemented separately in FakeEventHub,
6668 // but that should match the behaviour of TouchVideoDevice.
6669 processPosition(mapper, 200, 200);
6670 processSync(mapper);
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6672 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6673}
6674
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006675TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6676 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6677 prepareAxes(POSITION);
6678 addConfigurationProperty("touch.deviceType", "touchScreen");
6679 addMapperAndConfigure(mapper);
6680 // Unrotated video frame
6681 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6682 NotifyMotionArgs motionArgs;
6683
6684 // Test all 4 orientations
6685 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6686 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6687 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6688 clearViewports();
6689 prepareDisplay(orientation);
6690 std::vector<TouchVideoFrame> frames{frame};
6691 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6692 processPosition(mapper, 100, 200);
6693 processSync(mapper);
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 frames[0].rotate(orientation);
6696 ASSERT_EQ(frames, motionArgs.videoFrames);
6697 }
6698}
6699
6700TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6701 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6702 prepareAxes(POSITION);
6703 addConfigurationProperty("touch.deviceType", "touchScreen");
6704 addMapperAndConfigure(mapper);
6705 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6706 // so mix these.
6707 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6708 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6709 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6710 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6711 NotifyMotionArgs motionArgs;
6712
6713 prepareDisplay(DISPLAY_ORIENTATION_90);
6714 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6715 processPosition(mapper, 100, 200);
6716 processSync(mapper);
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6718 std::for_each(frames.begin(), frames.end(),
6719 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6720 ASSERT_EQ(frames, motionArgs.videoFrames);
6721}
6722
Arthur Hung9da14732019-09-02 16:16:58 +08006723/**
6724 * If we had defined port associations, but the viewport is not ready, the touch device would be
6725 * expected to be disabled, and it should be enabled after the viewport has found.
6726 */
6727TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
6728 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6729 constexpr uint8_t hdmi2 = 1;
6730 const std::string secondaryUniqueId = "uniqueId2";
6731 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6732
6733 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6734
6735 addConfigurationProperty("touch.deviceType", "touchScreen");
6736 prepareAxes(POSITION);
6737 addMapperAndConfigure(mapper);
6738
6739 ASSERT_EQ(mDevice->isEnabled(), false);
6740
6741 // Add display on hdmi2, the device should be enabled and can receive touch event.
6742 prepareSecondaryDisplay(type, hdmi2);
6743 ASSERT_EQ(mDevice->isEnabled(), true);
6744
6745 // Send a touch event.
6746 processPosition(mapper, 100, 100);
6747 processSync(mapper);
6748
6749 NotifyMotionArgs args;
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6751 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6752}
6753
Arthur Hung6cd19a42019-08-30 19:04:12 +08006754/**
6755 * Test touch should not work if outside of surface.
6756 */
6757TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
6758 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6759 addConfigurationProperty("touch.deviceType", "touchScreen");
6760 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006761 prepareAxes(POSITION);
6762 addMapperAndConfigure(mapper);
6763
Arthur Hung05de5772019-09-26 18:31:26 +08006764 // Touch on left-top area should work.
6765 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6766 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6767 processPosition(mapper, rawX, rawY);
6768 processSync(mapper);
6769
6770 NotifyMotionArgs args;
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6772
6773 // Reset.
6774 mapper->reset(ARBITRARY_TIME);
6775
6776 // Let logical display be different to physical display and rotate 90-degrees.
6777 std::optional<DisplayViewport> internalViewport =
6778 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6779 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6780 internalViewport->logicalLeft = 0;
6781 internalViewport->logicalTop = 0;
6782 internalViewport->logicalRight = DISPLAY_HEIGHT;
6783 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6784
6785 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6786 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6787 internalViewport->physicalRight = DISPLAY_HEIGHT;
6788 internalViewport->physicalBottom = DISPLAY_WIDTH;
6789
6790 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6791 internalViewport->deviceHeight = DISPLAY_WIDTH;
6792 mFakePolicy->updateViewport(internalViewport.value());
6793 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6794
6795 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006796 processPosition(mapper, rawX, rawY);
6797 processSync(mapper);
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6799}
6800
Michael Wrightd02c5b62014-02-10 15:10:22 -08006801} // namespace android