blob: 31b16528060f7c8f9e1fccfdb181d5265850df22 [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
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080029#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include <math.h>
31
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080032
Michael Wrightd02c5b62014-02-10 15:10:22 -080033namespace android {
34
35// An arbitrary time value.
36static const nsecs_t ARBITRARY_TIME = 1234;
37
38// Arbitrary display properties.
39static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070040static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080041static const int32_t DISPLAY_WIDTH = 480;
42static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070043static const int32_t VIRTUAL_DISPLAY_ID = 1;
44static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
45static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070046static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070047static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080048
49// Error tolerance for floating point assertions.
50static const float EPSILON = 0.001f;
51
52template<typename T>
53static inline T min(T a, T b) {
54 return a < b ? a : b;
55}
56
57static inline float avg(float x, float y) {
58 return (x + y) / 2;
59}
60
61
62// --- FakePointerController ---
63
64class FakePointerController : public PointerControllerInterface {
65 bool mHaveBounds;
66 float mMinX, mMinY, mMaxX, mMaxY;
67 float mX, mY;
68 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080069 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080070
71protected:
72 virtual ~FakePointerController() { }
73
74public:
75 FakePointerController() :
76 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080077 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080078 }
79
80 void setBounds(float minX, float minY, float maxX, float maxY) {
81 mHaveBounds = true;
82 mMinX = minX;
83 mMinY = minY;
84 mMaxX = maxX;
85 mMaxY = maxY;
86 }
87
Arthur Hungc7ad2d02018-12-18 17:41:29 +080088 void setDisplayId(int32_t displayId) {
89 mDisplayId = displayId;
90 }
91
Michael Wrightd02c5b62014-02-10 15:10:22 -080092 virtual void setPosition(float x, float y) {
93 mX = x;
94 mY = y;
95 }
96
97 virtual void setButtonState(int32_t buttonState) {
98 mButtonState = buttonState;
99 }
100
101 virtual int32_t getButtonState() const {
102 return mButtonState;
103 }
104
105 virtual void getPosition(float* outX, float* outY) const {
106 *outX = mX;
107 *outY = mY;
108 }
109
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800110 virtual int32_t getDisplayId() const {
111 return mDisplayId;
112 }
113
Arthur Hung7c645402019-01-25 17:45:42 +0800114 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
115 return mSpotsByDisplay;
116 }
117
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118private:
119 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
120 *outMinX = mMinX;
121 *outMinY = mMinY;
122 *outMaxX = mMaxX;
123 *outMaxY = mMaxY;
124 return mHaveBounds;
125 }
126
127 virtual void move(float deltaX, float deltaY) {
128 mX += deltaX;
129 if (mX < mMinX) mX = mMinX;
130 if (mX > mMaxX) mX = mMaxX;
131 mY += deltaY;
132 if (mY < mMinY) mY = mMinY;
133 if (mY > mMaxY) mY = mMaxY;
134 }
135
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100136 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137 }
138
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100139 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140 }
141
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100142 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143 }
144
Arthur Hung7c645402019-01-25 17:45:42 +0800145 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
146 int32_t displayId) {
147 std::vector<int32_t> newSpots;
148 // Add spots for fingers that are down.
149 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
150 uint32_t id = idBits.clearFirstMarkedBit();
151 newSpots.push_back(id);
152 }
153
154 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155 }
156
157 virtual void clearSpots() {
158 }
Arthur Hung7c645402019-01-25 17:45:42 +0800159
160 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161};
162
163
164// --- FakeInputReaderPolicy ---
165
166class FakeInputReaderPolicy : public InputReaderPolicyInterface {
167 InputReaderConfiguration mConfig;
168 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800169 std::vector<InputDeviceInfo> mInputDevices;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100170 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700171 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172
173protected:
174 virtual ~FakeInputReaderPolicy() { }
175
176public:
177 FakeInputReaderPolicy() {
178 }
179
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700180 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100181 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100182 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700183 }
184
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700185 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
186 return mConfig.getDisplayViewportByUniqueId(uniqueId);
187 }
188 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
189 return mConfig.getDisplayViewportByType(type);
190 }
191
192 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
193 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700194 }
195
196 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700197 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
198 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700199 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700200 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700201 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100202 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 }
204
Arthur Hung6cd19a42019-08-30 19:04:12 +0800205 bool updateViewport(const DisplayViewport& viewport) {
206 size_t count = mViewports.size();
207 for (size_t i = 0; i < count; i++) {
208 const DisplayViewport& currentViewport = mViewports[i];
209 if (currentViewport.displayId == viewport.displayId) {
210 mViewports[i] = viewport;
211 mConfig.setDisplayViewports(mViewports);
212 return true;
213 }
214 }
215 // no viewport found.
216 return false;
217 }
218
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100219 void addExcludedDeviceName(const std::string& deviceName) {
220 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800221 }
222
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700223 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
224 mConfig.portAssociations.insert({inputPort, displayPort});
225 }
226
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000227 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700228
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000229 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700230
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
232 mPointerControllers.add(deviceId, controller);
233 }
234
235 const InputReaderConfiguration* getReaderConfiguration() const {
236 return &mConfig;
237 }
238
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800239 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240 return mInputDevices;
241 }
242
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100243 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700244 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700245 return transform;
246 }
247
248 void setTouchAffineTransformation(const TouchAffineTransformation t) {
249 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800250 }
251
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800252 void setPointerCapture(bool enabled) {
253 mConfig.pointerCapture = enabled;
254 }
255
Arthur Hung7c645402019-01-25 17:45:42 +0800256 void setShowTouches(bool enabled) {
257 mConfig.showTouches = enabled;
258 }
259
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700261 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700262 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
263 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700264 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
265 || orientation == DISPLAY_ORIENTATION_270);
266 DisplayViewport v;
267 v.displayId = displayId;
268 v.orientation = orientation;
269 v.logicalLeft = 0;
270 v.logicalTop = 0;
271 v.logicalRight = isRotated ? height : width;
272 v.logicalBottom = isRotated ? width : height;
273 v.physicalLeft = 0;
274 v.physicalTop = 0;
275 v.physicalRight = isRotated ? height : width;
276 v.physicalBottom = isRotated ? width : height;
277 v.deviceWidth = isRotated ? height : width;
278 v.deviceHeight = isRotated ? width : height;
279 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700280 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100281 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700282 return v;
283 }
284
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
286 *outConfig = mConfig;
287 }
288
289 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
290 return mPointerControllers.valueFor(deviceId);
291 }
292
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800293 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294 mInputDevices = inputDevices;
295 }
296
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100297 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700298 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 }
300
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100301 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
302 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 }
304};
305
Michael Wrightd02c5b62014-02-10 15:10:22 -0800306// --- FakeEventHub ---
307
308class FakeEventHub : public EventHubInterface {
309 struct KeyInfo {
310 int32_t keyCode;
311 uint32_t flags;
312 };
313
314 struct Device {
315 InputDeviceIdentifier identifier;
316 uint32_t classes;
317 PropertyMap configuration;
318 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
319 KeyedVector<int, bool> relativeAxes;
320 KeyedVector<int32_t, int32_t> keyCodeStates;
321 KeyedVector<int32_t, int32_t> scanCodeStates;
322 KeyedVector<int32_t, int32_t> switchStates;
323 KeyedVector<int32_t, int32_t> absoluteAxisValue;
324 KeyedVector<int32_t, KeyInfo> keysByScanCode;
325 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
326 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800327 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700328 bool enabled;
329
330 status_t enable() {
331 enabled = true;
332 return OK;
333 }
334
335 status_t disable() {
336 enabled = false;
337 return OK;
338 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700340 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700341 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342 }
343 };
344
345 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100346 std::vector<std::string> mExcludedDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 List<RawEvent> mEvents;
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600348 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700350public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 virtual ~FakeEventHub() {
352 for (size_t i = 0; i < mDevices.size(); i++) {
353 delete mDevices.valueAt(i);
354 }
355 }
356
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357 FakeEventHub() { }
358
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100359 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360 Device* device = new Device(classes);
361 device->identifier.name = name;
362 mDevices.add(deviceId, device);
363
364 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
365 }
366
367 void removeDevice(int32_t deviceId) {
368 delete mDevices.valueFor(deviceId);
369 mDevices.removeItem(deviceId);
370
371 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
372 }
373
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700374 bool isDeviceEnabled(int32_t deviceId) {
375 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700376 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700377 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
378 return false;
379 }
380 return device->enabled;
381 }
382
383 status_t enableDevice(int32_t deviceId) {
384 status_t result;
385 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700386 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700387 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
388 return BAD_VALUE;
389 }
390 if (device->enabled) {
391 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
392 return OK;
393 }
394 result = device->enable();
395 return result;
396 }
397
398 status_t disableDevice(int32_t deviceId) {
399 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700400 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700401 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
402 return BAD_VALUE;
403 }
404 if (!device->enabled) {
405 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
406 return OK;
407 }
408 return device->disable();
409 }
410
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 void finishDeviceScan() {
412 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
413 }
414
415 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
416 Device* device = getDevice(deviceId);
417 device->configuration.addProperty(key, value);
418 }
419
420 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
421 Device* device = getDevice(deviceId);
422 device->configuration.addAll(configuration);
423 }
424
425 void addAbsoluteAxis(int32_t deviceId, int axis,
426 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
427 Device* device = getDevice(deviceId);
428
429 RawAbsoluteAxisInfo info;
430 info.valid = true;
431 info.minValue = minValue;
432 info.maxValue = maxValue;
433 info.flat = flat;
434 info.fuzz = fuzz;
435 info.resolution = resolution;
436 device->absoluteAxes.add(axis, info);
437 }
438
439 void addRelativeAxis(int32_t deviceId, int32_t axis) {
440 Device* device = getDevice(deviceId);
441 device->relativeAxes.add(axis, true);
442 }
443
444 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
445 Device* device = getDevice(deviceId);
446 device->keyCodeStates.replaceValueFor(keyCode, state);
447 }
448
449 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
450 Device* device = getDevice(deviceId);
451 device->scanCodeStates.replaceValueFor(scanCode, state);
452 }
453
454 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
455 Device* device = getDevice(deviceId);
456 device->switchStates.replaceValueFor(switchCode, state);
457 }
458
459 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
460 Device* device = getDevice(deviceId);
461 device->absoluteAxisValue.replaceValueFor(axis, value);
462 }
463
464 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
465 int32_t keyCode, uint32_t flags) {
466 Device* device = getDevice(deviceId);
467 KeyInfo info;
468 info.keyCode = keyCode;
469 info.flags = flags;
470 if (scanCode) {
471 device->keysByScanCode.add(scanCode, info);
472 }
473 if (usageCode) {
474 device->keysByUsageCode.add(usageCode, info);
475 }
476 }
477
478 void addLed(int32_t deviceId, int32_t led, bool initialState) {
479 Device* device = getDevice(deviceId);
480 device->leds.add(led, initialState);
481 }
482
483 bool getLedState(int32_t deviceId, int32_t led) {
484 Device* device = getDevice(deviceId);
485 return device->leds.valueFor(led);
486 }
487
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100488 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489 return mExcludedDevices;
490 }
491
492 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
493 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800494 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495 }
496
497 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
498 int32_t code, int32_t value) {
499 RawEvent event;
500 event.when = when;
501 event.deviceId = deviceId;
502 event.type = type;
503 event.code = code;
504 event.value = value;
505 mEvents.push_back(event);
506
507 if (type == EV_ABS) {
508 setAbsoluteAxisValue(deviceId, code, value);
509 }
510 }
511
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600512 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
513 std::vector<TouchVideoFrame>> videoFrames) {
514 mVideoFrames = std::move(videoFrames);
515 }
516
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 void assertQueueIsEmpty() {
518 ASSERT_EQ(size_t(0), mEvents.size())
519 << "Expected the event queue to be empty (fully consumed).";
520 }
521
522private:
523 Device* getDevice(int32_t deviceId) const {
524 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100525 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526 }
527
528 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
529 Device* device = getDevice(deviceId);
530 return device ? device->classes : 0;
531 }
532
533 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
534 Device* device = getDevice(deviceId);
535 return device ? device->identifier : InputDeviceIdentifier();
536 }
537
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100538 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 return 0;
540 }
541
542 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
543 Device* device = getDevice(deviceId);
544 if (device) {
545 *outConfiguration = device->configuration;
546 }
547 }
548
549 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
550 RawAbsoluteAxisInfo* outAxisInfo) const {
551 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800552 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553 ssize_t index = device->absoluteAxes.indexOfKey(axis);
554 if (index >= 0) {
555 *outAxisInfo = device->absoluteAxes.valueAt(index);
556 return OK;
557 }
558 }
559 outAxisInfo->clear();
560 return -1;
561 }
562
563 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
564 Device* device = getDevice(deviceId);
565 if (device) {
566 return device->relativeAxes.indexOfKey(axis) >= 0;
567 }
568 return false;
569 }
570
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100571 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572 return false;
573 }
574
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700575 virtual status_t mapKey(int32_t deviceId,
576 int32_t scanCode, int32_t usageCode, int32_t metaState,
577 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 Device* device = getDevice(deviceId);
579 if (device) {
580 const KeyInfo* key = getKey(device, scanCode, usageCode);
581 if (key) {
582 if (outKeycode) {
583 *outKeycode = key->keyCode;
584 }
585 if (outFlags) {
586 *outFlags = key->flags;
587 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700588 if (outMetaState) {
589 *outMetaState = metaState;
590 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 return OK;
592 }
593 }
594 return NAME_NOT_FOUND;
595 }
596
597 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
598 if (usageCode) {
599 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
600 if (index >= 0) {
601 return &device->keysByUsageCode.valueAt(index);
602 }
603 }
604 if (scanCode) {
605 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
606 if (index >= 0) {
607 return &device->keysByScanCode.valueAt(index);
608 }
609 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700610 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 }
612
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100613 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614 return NAME_NOT_FOUND;
615 }
616
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100617 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 mExcludedDevices = devices;
619 }
620
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100621 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 if (mEvents.empty()) {
623 return 0;
624 }
625
626 *buffer = *mEvents.begin();
627 mEvents.erase(mEvents.begin());
628 return 1;
629 }
630
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800631 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600632 auto it = mVideoFrames.find(deviceId);
633 if (it != mVideoFrames.end()) {
634 std::vector<TouchVideoFrame> frames = std::move(it->second);
635 mVideoFrames.erase(deviceId);
636 return frames;
637 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800638 return {};
639 }
640
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
642 Device* device = getDevice(deviceId);
643 if (device) {
644 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
645 if (index >= 0) {
646 return device->scanCodeStates.valueAt(index);
647 }
648 }
649 return AKEY_STATE_UNKNOWN;
650 }
651
652 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
653 Device* device = getDevice(deviceId);
654 if (device) {
655 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
656 if (index >= 0) {
657 return device->keyCodeStates.valueAt(index);
658 }
659 }
660 return AKEY_STATE_UNKNOWN;
661 }
662
663 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
664 Device* device = getDevice(deviceId);
665 if (device) {
666 ssize_t index = device->switchStates.indexOfKey(sw);
667 if (index >= 0) {
668 return device->switchStates.valueAt(index);
669 }
670 }
671 return AKEY_STATE_UNKNOWN;
672 }
673
674 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
675 int32_t* outValue) const {
676 Device* device = getDevice(deviceId);
677 if (device) {
678 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
679 if (index >= 0) {
680 *outValue = device->absoluteAxisValue.valueAt(index);
681 return OK;
682 }
683 }
684 *outValue = 0;
685 return -1;
686 }
687
688 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
689 uint8_t* outFlags) const {
690 bool result = false;
691 Device* device = getDevice(deviceId);
692 if (device) {
693 for (size_t i = 0; i < numCodes; i++) {
694 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
695 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
696 outFlags[i] = 1;
697 result = true;
698 }
699 }
700 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
701 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
702 outFlags[i] = 1;
703 result = true;
704 }
705 }
706 }
707 }
708 return result;
709 }
710
711 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
712 Device* device = getDevice(deviceId);
713 if (device) {
714 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
715 return index >= 0;
716 }
717 return false;
718 }
719
720 virtual bool hasLed(int32_t deviceId, int32_t led) const {
721 Device* device = getDevice(deviceId);
722 return device && device->leds.indexOfKey(led) >= 0;
723 }
724
725 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
726 Device* device = getDevice(deviceId);
727 if (device) {
728 ssize_t index = device->leds.indexOfKey(led);
729 if (index >= 0) {
730 device->leds.replaceValueAt(led, on);
731 } else {
732 ADD_FAILURE()
733 << "Attempted to set the state of an LED that the EventHub declared "
734 "was not present. led=" << led;
735 }
736 }
737 }
738
739 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800740 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741 outVirtualKeys.clear();
742
743 Device* device = getDevice(deviceId);
744 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800745 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746 }
747 }
748
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100749 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700750 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751 }
752
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100753 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754 return false;
755 }
756
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100757 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 }
759
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100760 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761 }
762
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100763 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764 return false;
765 }
766
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800767 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800768 }
769
770 virtual void monitor() {
771 }
772
773 virtual void requestReopenDevices() {
774 }
775
776 virtual void wake() {
777 }
778};
779
780
781// --- FakeInputReaderContext ---
782
783class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700784 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800785 sp<InputReaderPolicyInterface> mPolicy;
786 sp<InputListenerInterface> mListener;
787 int32_t mGlobalMetaState;
788 bool mUpdateGlobalMetaStateWasCalled;
789 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800790 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800791
792public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700793 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
794 const sp<InputReaderPolicyInterface>& policy,
795 const sp<InputListenerInterface>& listener)
796 : mEventHub(eventHub),
797 mPolicy(policy),
798 mListener(listener),
799 mGlobalMetaState(0),
800 mNextSequenceNum(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801
802 virtual ~FakeInputReaderContext() { }
803
804 void assertUpdateGlobalMetaStateWasCalled() {
805 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
806 << "Expected updateGlobalMetaState() to have been called.";
807 mUpdateGlobalMetaStateWasCalled = false;
808 }
809
810 void setGlobalMetaState(int32_t state) {
811 mGlobalMetaState = state;
812 }
813
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800814 uint32_t getGeneration() {
815 return mGeneration;
816 }
817
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818private:
819 virtual void updateGlobalMetaState() {
820 mUpdateGlobalMetaStateWasCalled = true;
821 }
822
823 virtual int32_t getGlobalMetaState() {
824 return mGlobalMetaState;
825 }
826
827 virtual EventHubInterface* getEventHub() {
828 return mEventHub.get();
829 }
830
831 virtual InputReaderPolicyInterface* getPolicy() {
832 return mPolicy.get();
833 }
834
835 virtual InputListenerInterface* getListener() {
836 return mListener.get();
837 }
838
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100839 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800840 }
841
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100842 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843 return false;
844 }
845
846 virtual void fadePointer() {
847 }
848
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100849 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850 }
851
852 virtual int32_t bumpGeneration() {
853 return ++mGeneration;
854 }
Michael Wright842500e2015-03-13 17:32:02 -0700855
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800856 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700857
858 }
859
860 virtual void dispatchExternalStylusState(const StylusState&) {
861
862 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800863
864 virtual uint32_t getNextSequenceNum() {
865 return mNextSequenceNum++;
866 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800867};
868
869
870// --- FakeInputMapper ---
871
872class FakeInputMapper : public InputMapper {
873 uint32_t mSources;
874 int32_t mKeyboardType;
875 int32_t mMetaState;
876 KeyedVector<int32_t, int32_t> mKeyCodeStates;
877 KeyedVector<int32_t, int32_t> mScanCodeStates;
878 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800879 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880 RawEvent mLastEvent;
881
882 bool mConfigureWasCalled;
883 bool mResetWasCalled;
884 bool mProcessWasCalled;
885
Arthur Hungc23540e2018-11-29 20:42:11 +0800886 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887public:
888 FakeInputMapper(InputDevice* device, uint32_t sources) :
889 InputMapper(device),
890 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
891 mMetaState(0),
892 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
893 }
894
895 virtual ~FakeInputMapper() { }
896
897 void setKeyboardType(int32_t keyboardType) {
898 mKeyboardType = keyboardType;
899 }
900
901 void setMetaState(int32_t metaState) {
902 mMetaState = metaState;
903 }
904
905 void assertConfigureWasCalled() {
906 ASSERT_TRUE(mConfigureWasCalled)
907 << "Expected configure() to have been called.";
908 mConfigureWasCalled = false;
909 }
910
911 void assertResetWasCalled() {
912 ASSERT_TRUE(mResetWasCalled)
913 << "Expected reset() to have been called.";
914 mResetWasCalled = false;
915 }
916
Yi Kong9b14ac62018-07-17 13:48:38 -0700917 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918 ASSERT_TRUE(mProcessWasCalled)
919 << "Expected process() to have been called.";
920 if (outLastEvent) {
921 *outLastEvent = mLastEvent;
922 }
923 mProcessWasCalled = false;
924 }
925
926 void setKeyCodeState(int32_t keyCode, int32_t state) {
927 mKeyCodeStates.replaceValueFor(keyCode, state);
928 }
929
930 void setScanCodeState(int32_t scanCode, int32_t state) {
931 mScanCodeStates.replaceValueFor(scanCode, state);
932 }
933
934 void setSwitchState(int32_t switchCode, int32_t state) {
935 mSwitchStates.replaceValueFor(switchCode, state);
936 }
937
938 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800939 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 }
941
942private:
943 virtual uint32_t getSources() {
944 return mSources;
945 }
946
947 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
948 InputMapper::populateDeviceInfo(deviceInfo);
949
950 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
951 deviceInfo->setKeyboardType(mKeyboardType);
952 }
953 }
954
Arthur Hungc23540e2018-11-29 20:42:11 +0800955 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800956 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800957
958 // Find the associated viewport if exist.
959 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
960 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
961 mViewport = config->getDisplayViewportByPort(*displayPort);
962 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800963 }
964
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100965 virtual void reset(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966 mResetWasCalled = true;
967 }
968
969 virtual void process(const RawEvent* rawEvent) {
970 mLastEvent = *rawEvent;
971 mProcessWasCalled = true;
972 }
973
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100974 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800975 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
976 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
977 }
978
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100979 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
981 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
982 }
983
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100984 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 ssize_t index = mSwitchStates.indexOfKey(switchCode);
986 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
987 }
988
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100989 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 const int32_t* keyCodes, uint8_t* outFlags) {
991 bool result = false;
992 for (size_t i = 0; i < numCodes; i++) {
993 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
994 if (keyCodes[i] == mSupportedKeyCodes[j]) {
995 outFlags[i] = 1;
996 result = true;
997 }
998 }
999 }
1000 return result;
1001 }
1002
1003 virtual int32_t getMetaState() {
1004 return mMetaState;
1005 }
1006
1007 virtual void fadePointer() {
1008 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001009
1010 virtual std::optional<int32_t> getAssociatedDisplay() {
1011 if (mViewport) {
1012 return std::make_optional(mViewport->displayId);
1013 }
1014 return std::nullopt;
1015 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016};
1017
1018
1019// --- InstrumentedInputReader ---
1020
1021class InstrumentedInputReader : public InputReader {
1022 InputDevice* mNextDevice;
1023
1024public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001025 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1026 const sp<InputReaderPolicyInterface>& policy,
1027 const sp<InputListenerInterface>& listener)
1028 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001029
1030 virtual ~InstrumentedInputReader() {
1031 if (mNextDevice) {
1032 delete mNextDevice;
1033 }
1034 }
1035
1036 void setNextDevice(InputDevice* device) {
1037 mNextDevice = device;
1038 }
1039
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001040 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Arthur Hungc23540e2018-11-29 20:42:11 +08001041 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042 InputDeviceIdentifier identifier;
1043 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001044 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045 int32_t generation = deviceId + 1;
1046 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
1047 classes);
1048 }
1049
1050protected:
1051 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
1052 const InputDeviceIdentifier& identifier, uint32_t classes) {
1053 if (mNextDevice) {
1054 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001055 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056 return device;
1057 }
1058 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1059 }
1060
1061 friend class InputReaderTest;
1062};
1063
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001064// --- InputReaderPolicyTest ---
1065class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001066protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001067 sp<FakeInputReaderPolicy> mFakePolicy;
1068
1069 virtual void SetUp() {
1070 mFakePolicy = new FakeInputReaderPolicy();
1071 }
1072 virtual void TearDown() {
1073 mFakePolicy.clear();
1074 }
1075};
1076
1077/**
1078 * Check that empty set of viewports is an acceptable configuration.
1079 * Also try to get internal viewport two different ways - by type and by uniqueId.
1080 *
1081 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1082 * Such configuration is not currently allowed.
1083 */
1084TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001085 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001086
1087 // We didn't add any viewports yet, so there shouldn't be any.
1088 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001089 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001090 ASSERT_FALSE(internalViewport);
1091
1092 // Add an internal viewport, then clear it
1093 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001094 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001095
1096 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001097 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001098 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001099 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001100
1101 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001102 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001103 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001104 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001105
1106 mFakePolicy->clearViewports();
1107 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001108 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001109 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001110 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001111 ASSERT_FALSE(internalViewport);
1112}
1113
1114TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1115 const std::string internalUniqueId = "local:0";
1116 const std::string externalUniqueId = "local:1";
1117 const std::string virtualUniqueId1 = "virtual:2";
1118 const std::string virtualUniqueId2 = "virtual:3";
1119 constexpr int32_t virtualDisplayId1 = 2;
1120 constexpr int32_t virtualDisplayId2 = 3;
1121
1122 // Add an internal viewport
1123 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001124 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001125 // Add an external viewport
1126 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001127 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001128 // Add an virtual viewport
1129 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001130 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001131 // Add another virtual viewport
1132 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001133 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001134
1135 // Check matching by type for internal
1136 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001137 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001138 ASSERT_TRUE(internalViewport);
1139 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1140
1141 // Check matching by type for external
1142 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001143 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001144 ASSERT_TRUE(externalViewport);
1145 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1146
1147 // Check matching by uniqueId for virtual viewport #1
1148 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001149 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001150 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001151 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001152 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1153 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1154
1155 // Check matching by uniqueId for virtual viewport #2
1156 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001157 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001158 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001159 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001160 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1161 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1162}
1163
1164
1165/**
1166 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1167 * that lookup works by checking display id.
1168 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1169 */
1170TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1171 const std::string uniqueId1 = "uniqueId1";
1172 const std::string uniqueId2 = "uniqueId2";
1173 constexpr int32_t displayId1 = 2;
1174 constexpr int32_t displayId2 = 3;
1175
1176 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1177 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1178 for (const ViewportType& type : types) {
1179 mFakePolicy->clearViewports();
1180 // Add a viewport
1181 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001182 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001183 // Add another viewport
1184 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001185 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001186
1187 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001188 std::optional<DisplayViewport> viewport1 =
1189 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001190 ASSERT_TRUE(viewport1);
1191 ASSERT_EQ(displayId1, viewport1->displayId);
1192 ASSERT_EQ(type, viewport1->type);
1193
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001194 std::optional<DisplayViewport> viewport2 =
1195 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001196 ASSERT_TRUE(viewport2);
1197 ASSERT_EQ(displayId2, viewport2->displayId);
1198 ASSERT_EQ(type, viewport2->type);
1199
1200 // When there are multiple viewports of the same kind, and uniqueId is not specified
1201 // in the call to getDisplayViewport, then that situation is not supported.
1202 // The viewports can be stored in any order, so we cannot rely on the order, since that
1203 // is just implementation detail.
1204 // However, we can check that it still returns *a* viewport, we just cannot assert
1205 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001206 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001207 ASSERT_TRUE(someViewport);
1208 }
1209}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001211/**
1212 * Check getDisplayViewportByPort
1213 */
1214TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1215 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1216 const std::string uniqueId1 = "uniqueId1";
1217 const std::string uniqueId2 = "uniqueId2";
1218 constexpr int32_t displayId1 = 1;
1219 constexpr int32_t displayId2 = 2;
1220 const uint8_t hdmi1 = 0;
1221 const uint8_t hdmi2 = 1;
1222 const uint8_t hdmi3 = 2;
1223
1224 mFakePolicy->clearViewports();
1225 // Add a viewport that's associated with some display port that's not of interest.
1226 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1227 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1228 // Add another viewport, connected to HDMI1 port
1229 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1230 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1231
1232 // Check that correct display viewport was returned by comparing the display ports.
1233 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1234 ASSERT_TRUE(hdmi1Viewport);
1235 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1236 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1237
1238 // Check that we can still get the same viewport using the uniqueId
1239 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1240 ASSERT_TRUE(hdmi1Viewport);
1241 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1242 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1243 ASSERT_EQ(type, hdmi1Viewport->type);
1244
1245 // Check that we cannot find a port with "HDMI2", because we never added one
1246 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1247 ASSERT_FALSE(hdmi2Viewport);
1248}
1249
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250// --- InputReaderTest ---
1251
1252class InputReaderTest : public testing::Test {
1253protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001254 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001256 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257 sp<InstrumentedInputReader> mReader;
1258
1259 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001260 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001262 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263
1264 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
1265 }
1266
1267 virtual void TearDown() {
1268 mReader.clear();
1269
1270 mFakeListener.clear();
1271 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272 }
1273
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001274 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275 const PropertyMap* configuration) {
1276 mFakeEventHub->addDevice(deviceId, name, classes);
1277
1278 if (configuration) {
1279 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1280 }
1281 mFakeEventHub->finishDeviceScan();
1282 mReader->loopOnce();
1283 mReader->loopOnce();
1284 mFakeEventHub->assertQueueIsEmpty();
1285 }
1286
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001287 void disableDevice(int32_t deviceId, InputDevice* device) {
1288 mFakePolicy->addDisabledDevice(deviceId);
1289 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1290 }
1291
1292 void enableDevice(int32_t deviceId, InputDevice* device) {
1293 mFakePolicy->removeDisabledDevice(deviceId);
1294 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1295 }
1296
1297 void configureDevice(uint32_t changes, InputDevice* device) {
1298 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1299 }
1300
Michael Wrightd02c5b62014-02-10 15:10:22 -08001301 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001302 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001303 const PropertyMap* configuration) {
1304 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1305 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1306 device->addMapper(mapper);
1307 mReader->setNextDevice(device);
1308 addDevice(deviceId, name, classes, configuration);
1309 return mapper;
1310 }
1311};
1312
1313TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001314 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001315 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001316 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001317 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001319
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001320 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321 mReader->getInputDevices(inputDevices);
1322
1323 ASSERT_EQ(1U, inputDevices.size());
1324 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001325 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001326 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1327 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1328 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1329
1330 // Should also have received a notification describing the new input devices.
1331 inputDevices = mFakePolicy->getInputDevices();
1332 ASSERT_EQ(1U, inputDevices.size());
1333 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001334 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001335 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1336 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1337 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1338}
1339
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001340TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1341 constexpr int32_t deviceId = 1;
1342 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001343 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001344 // Must add at least one mapper or the device will be ignored!
1345 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1346 device->addMapper(mapper);
1347 mReader->setNextDevice(device);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001348 addDevice(deviceId, "fake", deviceClass, nullptr);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001349
Yi Kong9b14ac62018-07-17 13:48:38 -07001350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001351
1352 NotifyDeviceResetArgs resetArgs;
1353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1354 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1355 ASSERT_EQ(deviceId, resetArgs.deviceId);
1356
1357 ASSERT_EQ(device->isEnabled(), true);
1358 disableDevice(deviceId, device);
1359 mReader->loopOnce();
1360
1361 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1362 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1363 ASSERT_EQ(deviceId, resetArgs.deviceId);
1364 ASSERT_EQ(device->isEnabled(), false);
1365
1366 disableDevice(deviceId, device);
1367 mReader->loopOnce();
1368 mFakeListener->assertNotifyDeviceResetWasNotCalled();
1369 mFakeListener->assertNotifyConfigurationChangedWasNotCalled();
1370 ASSERT_EQ(device->isEnabled(), false);
1371
1372 enableDevice(deviceId, device);
1373 mReader->loopOnce();
1374 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1375 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1376 ASSERT_EQ(deviceId, resetArgs.deviceId);
1377 ASSERT_EQ(device->isEnabled(), true);
1378}
1379
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001381 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001382 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001383 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1385
1386 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1387 AINPUT_SOURCE_ANY, AKEYCODE_A))
1388 << "Should return unknown when the device id is >= 0 but unknown.";
1389
1390 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1391 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1392 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1393
1394 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1395 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1396 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1397
1398 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1399 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1400 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1401
1402 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1403 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1404 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1405}
1406
1407TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001408 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001409 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001410 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001411 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1412
1413 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1414 AINPUT_SOURCE_ANY, KEY_A))
1415 << "Should return unknown when the device id is >= 0 but unknown.";
1416
1417 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1418 AINPUT_SOURCE_TRACKBALL, KEY_A))
1419 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1420
1421 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1422 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1423 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1424
1425 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1426 AINPUT_SOURCE_TRACKBALL, KEY_A))
1427 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1428
1429 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1430 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1431 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1432}
1433
1434TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001435 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001436 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001437 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001438 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1439
1440 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1441 AINPUT_SOURCE_ANY, SW_LID))
1442 << "Should return unknown when the device id is >= 0 but unknown.";
1443
1444 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1445 AINPUT_SOURCE_TRACKBALL, SW_LID))
1446 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1447
1448 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1449 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1450 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1451
1452 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1453 AINPUT_SOURCE_TRACKBALL, SW_LID))
1454 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1455
1456 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1457 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1458 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1459}
1460
1461TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001462 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001463 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001464 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001465
Michael Wrightd02c5b62014-02-10 15:10:22 -08001466 mapper->addSupportedKeyCode(AKEYCODE_A);
1467 mapper->addSupportedKeyCode(AKEYCODE_B);
1468
1469 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1470 uint8_t flags[4] = { 0, 0, 0, 1 };
1471
1472 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1473 << "Should return false when device id is >= 0 but unknown.";
1474 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1475
1476 flags[3] = 1;
1477 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1478 << "Should return false when device id is valid but the sources are not supported by the device.";
1479 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1480
1481 flags[3] = 1;
1482 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1483 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1484 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1485
1486 flags[3] = 1;
1487 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1488 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1489 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1490
1491 flags[3] = 1;
1492 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1493 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1494 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1495}
1496
1497TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001498 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001499
1500 NotifyConfigurationChangedArgs args;
1501
1502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1503 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1504}
1505
1506TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001507 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001508 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001509 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001510
1511 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
1512 mReader->loopOnce();
1513 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1514
1515 RawEvent event;
1516 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1517 ASSERT_EQ(0, event.when);
1518 ASSERT_EQ(1, event.deviceId);
1519 ASSERT_EQ(EV_KEY, event.type);
1520 ASSERT_EQ(KEY_A, event.code);
1521 ASSERT_EQ(1, event.value);
1522}
1523
Prabir Pradhan42611e02018-11-27 14:04:02 -08001524TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1525 constexpr int32_t deviceId = 1;
1526 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001527 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001528 // Must add at least one mapper or the device will be ignored!
1529 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1530 device->addMapper(mapper);
1531 mReader->setNextDevice(device);
1532 addDevice(deviceId, "fake", deviceClass, nullptr);
1533
1534 NotifyDeviceResetArgs resetArgs;
1535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1536 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1537
1538 disableDevice(deviceId, device);
1539 mReader->loopOnce();
1540 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1541 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1542 prevSequenceNum = resetArgs.sequenceNum;
1543
1544 enableDevice(deviceId, device);
1545 mReader->loopOnce();
1546 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1547 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1548 prevSequenceNum = resetArgs.sequenceNum;
1549
1550 disableDevice(deviceId, device);
1551 mReader->loopOnce();
1552 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1553 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1554 prevSequenceNum = resetArgs.sequenceNum;
1555}
1556
Arthur Hungc23540e2018-11-29 20:42:11 +08001557TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1558 constexpr int32_t deviceId = 1;
1559 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1560 const char* DEVICE_LOCATION = "USB1";
1561 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1562 DEVICE_LOCATION);
1563 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1564 device->addMapper(mapper);
1565 mReader->setNextDevice(device);
1566 addDevice(deviceId, "fake", deviceClass, nullptr);
1567
1568 const uint8_t hdmi1 = 1;
1569
1570 // Associated touch screen with second display.
1571 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1572
1573 // Add default and second display.
1574 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1575 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1576 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1577 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1578 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1579 mReader->loopOnce();
1580
Arthur Hung2c9a3342019-07-23 14:18:59 +08001581 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001582 ASSERT_EQ(deviceId, device->getId());
1583 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1584 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001585
1586 // Can't dispatch event from a disabled device.
1587 disableDevice(deviceId, device);
1588 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001589}
1590
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591
1592// --- InputDeviceTest ---
1593
1594class InputDeviceTest : public testing::Test {
1595protected:
1596 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001597 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598 static const int32_t DEVICE_ID;
1599 static const int32_t DEVICE_GENERATION;
1600 static const int32_t DEVICE_CONTROLLER_NUMBER;
1601 static const uint32_t DEVICE_CLASSES;
1602
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001603 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001605 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606 FakeInputReaderContext* mFakeContext;
1607
1608 InputDevice* mDevice;
1609
1610 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001611 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001612 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001613 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001614 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1615
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001616 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001617 InputDeviceIdentifier identifier;
1618 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001619 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001620 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1621 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1622 }
1623
1624 virtual void TearDown() {
1625 delete mDevice;
1626
1627 delete mFakeContext;
1628 mFakeListener.clear();
1629 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630 }
1631};
1632
1633const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001634const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001635const int32_t InputDeviceTest::DEVICE_ID = 1;
1636const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1637const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1638const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1639 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1640
1641TEST_F(InputDeviceTest, ImmutableProperties) {
1642 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001643 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001644 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1645}
1646
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001647TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1648 ASSERT_EQ(mDevice->isEnabled(), true);
1649}
1650
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1652 // Configuration.
1653 InputReaderConfiguration config;
1654 mDevice->configure(ARBITRARY_TIME, &config, 0);
1655
1656 // Reset.
1657 mDevice->reset(ARBITRARY_TIME);
1658
1659 NotifyDeviceResetArgs resetArgs;
1660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1661 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1662 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1663
1664 // Metadata.
1665 ASSERT_TRUE(mDevice->isIgnored());
1666 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1667
1668 InputDeviceInfo info;
1669 mDevice->getDeviceInfo(&info);
1670 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001671 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001672 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1673 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1674
1675 // State queries.
1676 ASSERT_EQ(0, mDevice->getMetaState());
1677
1678 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1679 << "Ignored device should return unknown key code state.";
1680 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1681 << "Ignored device should return unknown scan code state.";
1682 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1683 << "Ignored device should return unknown switch state.";
1684
1685 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1686 uint8_t flags[2] = { 0, 1 };
1687 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1688 << "Ignored device should never mark any key codes.";
1689 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1690 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1691}
1692
1693TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1694 // Configuration.
1695 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1696
1697 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1698 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1699 mapper1->setMetaState(AMETA_ALT_ON);
1700 mapper1->addSupportedKeyCode(AKEYCODE_A);
1701 mapper1->addSupportedKeyCode(AKEYCODE_B);
1702 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1703 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1704 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1705 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1706 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1707 mDevice->addMapper(mapper1);
1708
1709 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1710 mapper2->setMetaState(AMETA_SHIFT_ON);
1711 mDevice->addMapper(mapper2);
1712
1713 InputReaderConfiguration config;
1714 mDevice->configure(ARBITRARY_TIME, &config, 0);
1715
1716 String8 propertyValue;
1717 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1718 << "Device should have read configuration during configuration phase.";
1719 ASSERT_STREQ("value", propertyValue.string());
1720
1721 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1722 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1723
1724 // Reset
1725 mDevice->reset(ARBITRARY_TIME);
1726 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1727 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1728
1729 NotifyDeviceResetArgs resetArgs;
1730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1731 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1732 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1733
1734 // Metadata.
1735 ASSERT_FALSE(mDevice->isIgnored());
1736 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1737
1738 InputDeviceInfo info;
1739 mDevice->getDeviceInfo(&info);
1740 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001741 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001742 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1743 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1744
1745 // State queries.
1746 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1747 << "Should query mappers and combine meta states.";
1748
1749 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1750 << "Should return unknown key code state when source not supported.";
1751 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1752 << "Should return unknown scan code state when source not supported.";
1753 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1754 << "Should return unknown switch state when source not supported.";
1755
1756 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1757 << "Should query mapper when source is supported.";
1758 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1759 << "Should query mapper when source is supported.";
1760 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1761 << "Should query mapper when source is supported.";
1762
1763 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1764 uint8_t flags[4] = { 0, 0, 0, 1 };
1765 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1766 << "Should do nothing when source is unsupported.";
1767 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1768 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1769 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1770 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1771
1772 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1773 << "Should query mapper when source is supported.";
1774 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1775 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1776 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1777 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1778
1779 // Event handling.
1780 RawEvent event;
1781 mDevice->process(&event, 1);
1782
1783 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1784 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1785}
1786
Arthur Hung2c9a3342019-07-23 14:18:59 +08001787// A single input device is associated with a specific display. Check that:
1788// 1. Device is disabled if the viewport corresponding to the associated display is not found
1789// 2. Device is disabled when setEnabled API is called
1790TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
1791 FakeInputMapper* mapper = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1792 mDevice->addMapper(mapper);
1793
1794 // First Configuration.
1795 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1796
1797 // Device should be enabled by default.
1798 ASSERT_TRUE(mDevice->isEnabled());
1799
1800 // Prepare associated info.
1801 constexpr uint8_t hdmi = 1;
1802 const std::string UNIQUE_ID = "local:1";
1803
1804 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
1805 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1806 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1807 // Device should be disabled because it is associated with a specific display via
1808 // input port <-> display port association, but the corresponding display is not found
1809 ASSERT_FALSE(mDevice->isEnabled());
1810
1811 // Prepare displays.
1812 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1813 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
1814 ViewportType::VIEWPORT_INTERNAL);
1815 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1816 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1817 ASSERT_TRUE(mDevice->isEnabled());
1818
1819 // Device should be disabled after set disable.
1820 mFakePolicy->addDisabledDevice(mDevice->getId());
1821 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1822 InputReaderConfiguration::CHANGE_ENABLED_STATE);
1823 ASSERT_FALSE(mDevice->isEnabled());
1824
1825 // Device should still be disabled even found the associated display.
1826 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1827 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1828 ASSERT_FALSE(mDevice->isEnabled());
1829}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001830
1831// --- InputMapperTest ---
1832
1833class InputMapperTest : public testing::Test {
1834protected:
1835 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001836 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001837 static const int32_t DEVICE_ID;
1838 static const int32_t DEVICE_GENERATION;
1839 static const int32_t DEVICE_CONTROLLER_NUMBER;
1840 static const uint32_t DEVICE_CLASSES;
1841
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001842 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001843 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001844 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001845 FakeInputReaderContext* mFakeContext;
1846 InputDevice* mDevice;
1847
1848 virtual void SetUp() {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001849 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001850 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001851 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001852 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1853 InputDeviceIdentifier identifier;
1854 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001855 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001856 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1857 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1858
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001859 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001860 }
1861
1862 virtual void TearDown() {
1863 delete mDevice;
1864 delete mFakeContext;
1865 mFakeListener.clear();
1866 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001867 }
1868
1869 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001870 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001871 }
1872
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001873 void configureDevice(uint32_t changes) {
1874 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1875 }
1876
Michael Wrightd02c5b62014-02-10 15:10:22 -08001877 void addMapperAndConfigure(InputMapper* mapper) {
1878 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08001879 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880 mDevice->reset(ARBITRARY_TIME);
1881 }
1882
1883 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001884 int32_t orientation, const std::string& uniqueId,
1885 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001886 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001887 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07001888 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1889 }
1890
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001891 void clearViewports() {
1892 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001893 }
1894
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001895 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896 int32_t code, int32_t value) {
1897 RawEvent event;
1898 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001899 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001900 event.type = type;
1901 event.code = code;
1902 event.value = value;
1903 mapper->process(&event);
1904 }
1905
1906 static void assertMotionRange(const InputDeviceInfo& info,
1907 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1908 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07001909 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001910 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1911 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1912 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1913 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1914 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1915 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1916 }
1917
1918 static void assertPointerCoords(const PointerCoords& coords,
1919 float x, float y, float pressure, float size,
1920 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1921 float orientation, float distance) {
1922 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1923 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1924 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1925 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1926 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1927 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1928 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1929 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1930 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1931 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1932 }
1933
1934 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1935 float actualX, actualY;
1936 controller->getPosition(&actualX, &actualY);
1937 ASSERT_NEAR(x, actualX, 1);
1938 ASSERT_NEAR(y, actualY, 1);
1939 }
1940};
1941
1942const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001943const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001944const int32_t InputMapperTest::DEVICE_ID = 1;
1945const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1946const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1947const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1948
1949
1950// --- SwitchInputMapperTest ---
1951
1952class SwitchInputMapperTest : public InputMapperTest {
1953protected:
1954};
1955
1956TEST_F(SwitchInputMapperTest, GetSources) {
1957 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1958 addMapperAndConfigure(mapper);
1959
1960 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1961}
1962
1963TEST_F(SwitchInputMapperTest, GetSwitchState) {
1964 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1965 addMapperAndConfigure(mapper);
1966
1967 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1968 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1969
1970 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1971 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1972}
1973
1974TEST_F(SwitchInputMapperTest, Process) {
1975 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1976 addMapperAndConfigure(mapper);
1977
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08001978 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
1979 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
1980 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
1981 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001982
1983 NotifySwitchArgs args;
1984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
1985 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08001986 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
1987 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001988 args.switchMask);
1989 ASSERT_EQ(uint32_t(0), args.policyFlags);
1990}
1991
1992
1993// --- KeyboardInputMapperTest ---
1994
1995class KeyboardInputMapperTest : public InputMapperTest {
1996protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001997 const std::string UNIQUE_ID = "local:0";
1998
1999 void prepareDisplay(int32_t orientation);
2000
Arthur Hung2c9a3342019-07-23 14:18:59 +08002001 void testDPadKeyRotation(KeyboardInputMapper* mapper, int32_t originalScanCode,
2002 int32_t originalKeyCode, int32_t rotatedKeyCode,
2003 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002004};
2005
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002006/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2007 * orientation.
2008 */
2009void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2010 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002011 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002012}
2013
Michael Wrightd02c5b62014-02-10 15:10:22 -08002014void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002015 int32_t originalScanCode, int32_t originalKeyCode,
2016 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002017 NotifyKeyArgs args;
2018
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002019 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2021 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2022 ASSERT_EQ(originalScanCode, args.scanCode);
2023 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002024 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002025
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002026 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2028 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2029 ASSERT_EQ(originalScanCode, args.scanCode);
2030 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002031 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002032}
2033
Michael Wrightd02c5b62014-02-10 15:10:22 -08002034TEST_F(KeyboardInputMapperTest, GetSources) {
2035 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2036 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2037 addMapperAndConfigure(mapper);
2038
2039 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
2040}
2041
2042TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2043 const int32_t USAGE_A = 0x070004;
2044 const int32_t USAGE_UNKNOWN = 0x07ffff;
2045 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2046 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2047
2048 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2049 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2050 addMapperAndConfigure(mapper);
2051
2052 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002053 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002054 NotifyKeyArgs args;
2055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2056 ASSERT_EQ(DEVICE_ID, args.deviceId);
2057 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2058 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2059 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2060 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2061 ASSERT_EQ(KEY_HOME, args.scanCode);
2062 ASSERT_EQ(AMETA_NONE, args.metaState);
2063 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2064 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2065 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2066
2067 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002068 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2070 ASSERT_EQ(DEVICE_ID, args.deviceId);
2071 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2072 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2073 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2074 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2075 ASSERT_EQ(KEY_HOME, args.scanCode);
2076 ASSERT_EQ(AMETA_NONE, args.metaState);
2077 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2078 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2079 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2080
2081 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002082 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2083 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2085 ASSERT_EQ(DEVICE_ID, args.deviceId);
2086 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2087 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2088 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2089 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2090 ASSERT_EQ(0, args.scanCode);
2091 ASSERT_EQ(AMETA_NONE, args.metaState);
2092 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2093 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2094 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2095
2096 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002097 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2098 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2100 ASSERT_EQ(DEVICE_ID, args.deviceId);
2101 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2102 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2103 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2104 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2105 ASSERT_EQ(0, args.scanCode);
2106 ASSERT_EQ(AMETA_NONE, args.metaState);
2107 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2108 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2109 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2110
2111 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002112 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2113 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2115 ASSERT_EQ(DEVICE_ID, args.deviceId);
2116 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2117 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2118 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2119 ASSERT_EQ(0, args.keyCode);
2120 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2121 ASSERT_EQ(AMETA_NONE, args.metaState);
2122 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2123 ASSERT_EQ(0U, args.policyFlags);
2124 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2125
2126 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002127 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2128 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2130 ASSERT_EQ(DEVICE_ID, args.deviceId);
2131 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2132 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2133 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2134 ASSERT_EQ(0, args.keyCode);
2135 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2136 ASSERT_EQ(AMETA_NONE, args.metaState);
2137 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2138 ASSERT_EQ(0U, args.policyFlags);
2139 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2140}
2141
2142TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2143 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2144 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2145
2146 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2147 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2148 addMapperAndConfigure(mapper);
2149
2150 // Initial metastate.
2151 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2152
2153 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002154 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002155 NotifyKeyArgs args;
2156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2157 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2158 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2159 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2160
2161 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002162 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2164 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2165 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2166
2167 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002168 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2170 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2171 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2172
2173 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002174 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2176 ASSERT_EQ(AMETA_NONE, args.metaState);
2177 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2178 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2179}
2180
2181TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2182 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2183 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2184 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2185 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2186
2187 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2188 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2189 addMapperAndConfigure(mapper);
2190
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002191 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002192 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2193 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2194 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2195 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2196 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2197 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2198 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2199 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2200}
2201
2202TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2203 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2204 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2205 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2206 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2207
2208 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2209 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2210 addConfigurationProperty("keyboard.orientationAware", "1");
2211 addMapperAndConfigure(mapper);
2212
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002213 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002214 ASSERT_NO_FATAL_FAILURE(
2215 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2216 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2217 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2218 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2219 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2220 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2221 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002222
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002223 clearViewports();
2224 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002225 ASSERT_NO_FATAL_FAILURE(
2226 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2227 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2228 AKEYCODE_DPAD_UP, DISPLAY_ID));
2229 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2230 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2231 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2232 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002233
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002234 clearViewports();
2235 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002236 ASSERT_NO_FATAL_FAILURE(
2237 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2238 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2239 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2240 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2241 AKEYCODE_DPAD_UP, DISPLAY_ID));
2242 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2243 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002244
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002245 clearViewports();
2246 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002247 ASSERT_NO_FATAL_FAILURE(
2248 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2249 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2250 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2251 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2252 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2253 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2254 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002255
2256 // Special case: if orientation changes while key is down, we still emit the same keycode
2257 // in the key up as we did in the key down.
2258 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002259 clearViewports();
2260 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002261 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2263 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2264 ASSERT_EQ(KEY_UP, args.scanCode);
2265 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2266
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002267 clearViewports();
2268 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002269 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2271 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2272 ASSERT_EQ(KEY_UP, args.scanCode);
2273 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2274}
2275
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002276TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2277 // If the keyboard is not orientation aware,
2278 // key events should not be associated with a specific display id
2279 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2280
2281 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2282 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2283 addMapperAndConfigure(mapper);
2284 NotifyKeyArgs args;
2285
2286 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002287 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002289 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2291 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2292
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002293 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002294 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002296 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2298 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2299}
2300
2301TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2302 // If the keyboard is orientation aware,
2303 // key events should be associated with the internal viewport
2304 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2305
2306 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2307 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2308 addConfigurationProperty("keyboard.orientationAware", "1");
2309 addMapperAndConfigure(mapper);
2310 NotifyKeyArgs args;
2311
2312 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2313 // ^--- already checked by the previous test
2314
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002315 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002316 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002317 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002319 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2321 ASSERT_EQ(DISPLAY_ID, args.displayId);
2322
2323 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002324 clearViewports();
2325 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002326 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002327 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002329 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2331 ASSERT_EQ(newDisplayId, args.displayId);
2332}
2333
Michael Wrightd02c5b62014-02-10 15:10:22 -08002334TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2335 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2336 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2337 addMapperAndConfigure(mapper);
2338
2339 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2340 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2341
2342 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2343 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2344}
2345
2346TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2347 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2348 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2349 addMapperAndConfigure(mapper);
2350
2351 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2352 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2353
2354 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2355 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2356}
2357
2358TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2359 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2360 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2361 addMapperAndConfigure(mapper);
2362
2363 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2364
2365 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2366 uint8_t flags[2] = { 0, 0 };
2367 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2368 ASSERT_TRUE(flags[0]);
2369 ASSERT_FALSE(flags[1]);
2370}
2371
2372TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2373 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2374 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2375 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2376 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2377 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2378 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2379
2380 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2381 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2382 addMapperAndConfigure(mapper);
2383
2384 // Initialization should have turned all of the lights off.
2385 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2386 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2387 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2388
2389 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002390 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2391 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2393 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2394 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2395 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2396
2397 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002398 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2399 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2401 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2402 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2403 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2404
2405 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002406 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2407 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2409 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2410 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2411 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2412
2413 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002414 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2415 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2417 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2418 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2419 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2420
2421 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002422 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2423 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2425 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2426 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2427 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2428
2429 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002430 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2431 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2433 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2434 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2435 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2436}
2437
Arthur Hung2c9a3342019-07-23 14:18:59 +08002438TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2439 // keyboard 1.
2440 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2441 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2442 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2443 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2444
2445 // keyboard 2.
2446 const std::string USB2 = "USB2";
2447 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2448 InputDeviceIdentifier identifier;
2449 identifier.name = "KEYBOARD2";
2450 identifier.location = USB2;
2451 std::unique_ptr<InputDevice> device2 =
2452 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2453 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2454 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2455 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2456 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2457 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2458 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2459
2460 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2461 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2462 addMapperAndConfigure(mapper);
2463
2464 KeyboardInputMapper* mapper2 = new KeyboardInputMapper(device2.get(), AINPUT_SOURCE_KEYBOARD,
2465 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2466 device2->addMapper(mapper2);
2467 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2468 device2->reset(ARBITRARY_TIME);
2469
2470 // Prepared displays and associated info.
2471 constexpr uint8_t hdmi1 = 0;
2472 constexpr uint8_t hdmi2 = 1;
2473 const std::string SECONDARY_UNIQUE_ID = "local:1";
2474
2475 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2476 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2477
2478 // No associated display viewport found, should disable the device.
2479 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2480 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2481 ASSERT_FALSE(device2->isEnabled());
2482
2483 // Prepare second display.
2484 constexpr int32_t newDisplayId = 2;
2485 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2486 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2487 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2488 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2489 // Default device will reconfigure above, need additional reconfiguration for another device.
2490 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2491 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2492
2493 // Device should be enabled after the associated display is found.
2494 ASSERT_TRUE(mDevice->isEnabled());
2495 ASSERT_TRUE(device2->isEnabled());
2496
2497 // Test pad key events
2498 ASSERT_NO_FATAL_FAILURE(
2499 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2500 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2501 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2502 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2503 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2504 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2505 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2506
2507 ASSERT_NO_FATAL_FAILURE(
2508 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2509 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2510 AKEYCODE_DPAD_RIGHT, newDisplayId));
2511 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2512 AKEYCODE_DPAD_DOWN, newDisplayId));
2513 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2514 AKEYCODE_DPAD_LEFT, newDisplayId));
2515}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516
2517// --- CursorInputMapperTest ---
2518
2519class CursorInputMapperTest : public InputMapperTest {
2520protected:
2521 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2522
2523 sp<FakePointerController> mFakePointerController;
2524
2525 virtual void SetUp() {
2526 InputMapperTest::SetUp();
2527
2528 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002529 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530 }
2531
2532 void testMotionRotation(CursorInputMapper* mapper,
2533 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002534
2535 void prepareDisplay(int32_t orientation) {
2536 const std::string uniqueId = "local:0";
2537 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2538 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2539 orientation, uniqueId, NO_PORT, viewportType);
2540 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002541};
2542
2543const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2544
2545void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2546 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2547 NotifyMotionArgs args;
2548
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002549 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2550 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2551 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2555 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2556 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2557 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2558}
2559
2560TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2561 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2562 addConfigurationProperty("cursor.mode", "pointer");
2563 addMapperAndConfigure(mapper);
2564
2565 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2566}
2567
2568TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2569 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2570 addConfigurationProperty("cursor.mode", "navigation");
2571 addMapperAndConfigure(mapper);
2572
2573 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2574}
2575
2576TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2577 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2578 addConfigurationProperty("cursor.mode", "pointer");
2579 addMapperAndConfigure(mapper);
2580
2581 InputDeviceInfo info;
2582 mapper->populateDeviceInfo(&info);
2583
2584 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002585 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2586 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2588 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2589
2590 // When the bounds are set, then there should be a valid motion range.
2591 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2592
2593 InputDeviceInfo info2;
2594 mapper->populateDeviceInfo(&info2);
2595
2596 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2597 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2598 1, 800 - 1, 0.0f, 0.0f));
2599 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2600 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2601 2, 480 - 1, 0.0f, 0.0f));
2602 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2603 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2604 0.0f, 1.0f, 0.0f, 0.0f));
2605}
2606
2607TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2608 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2609 addConfigurationProperty("cursor.mode", "navigation");
2610 addMapperAndConfigure(mapper);
2611
2612 InputDeviceInfo info;
2613 mapper->populateDeviceInfo(&info);
2614
2615 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2616 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2617 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2618 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2619 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2620 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2621 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2622 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2623 0.0f, 1.0f, 0.0f, 0.0f));
2624}
2625
2626TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2627 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2628 addConfigurationProperty("cursor.mode", "navigation");
2629 addMapperAndConfigure(mapper);
2630
2631 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2632
2633 NotifyMotionArgs args;
2634
2635 // Button press.
2636 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002637 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2638 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2640 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2641 ASSERT_EQ(DEVICE_ID, args.deviceId);
2642 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2643 ASSERT_EQ(uint32_t(0), args.policyFlags);
2644 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2645 ASSERT_EQ(0, args.flags);
2646 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2647 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2648 ASSERT_EQ(0, args.edgeFlags);
2649 ASSERT_EQ(uint32_t(1), args.pointerCount);
2650 ASSERT_EQ(0, args.pointerProperties[0].id);
2651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2653 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2654 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2655 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2656 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2657
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2659 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2660 ASSERT_EQ(DEVICE_ID, args.deviceId);
2661 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2662 ASSERT_EQ(uint32_t(0), args.policyFlags);
2663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2664 ASSERT_EQ(0, args.flags);
2665 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2666 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2667 ASSERT_EQ(0, args.edgeFlags);
2668 ASSERT_EQ(uint32_t(1), args.pointerCount);
2669 ASSERT_EQ(0, args.pointerProperties[0].id);
2670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2672 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2673 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2674 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2675 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2676
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002678 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2679 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2681 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2682 ASSERT_EQ(DEVICE_ID, args.deviceId);
2683 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2684 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2686 ASSERT_EQ(0, args.flags);
2687 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2688 ASSERT_EQ(0, args.buttonState);
2689 ASSERT_EQ(0, args.edgeFlags);
2690 ASSERT_EQ(uint32_t(1), args.pointerCount);
2691 ASSERT_EQ(0, args.pointerProperties[0].id);
2692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2694 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2695 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2696 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2697 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2698
2699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2700 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2701 ASSERT_EQ(DEVICE_ID, args.deviceId);
2702 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2703 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2705 ASSERT_EQ(0, args.flags);
2706 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2707 ASSERT_EQ(0, args.buttonState);
2708 ASSERT_EQ(0, args.edgeFlags);
2709 ASSERT_EQ(uint32_t(1), args.pointerCount);
2710 ASSERT_EQ(0, args.pointerProperties[0].id);
2711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2713 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2714 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2715 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2716 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2717}
2718
2719TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2720 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2721 addConfigurationProperty("cursor.mode", "navigation");
2722 addMapperAndConfigure(mapper);
2723
2724 NotifyMotionArgs args;
2725
2726 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002727 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2728 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2732 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2733
2734 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002735 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2736 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2738 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2740 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2741}
2742
2743TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2744 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2745 addConfigurationProperty("cursor.mode", "navigation");
2746 addMapperAndConfigure(mapper);
2747
2748 NotifyMotionArgs args;
2749
2750 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002751 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2752 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2754 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2756 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2757
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2759 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2761 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2762
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002764 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2765 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002767 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2769 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2770
2771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2774 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2775}
2776
2777TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2778 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2779 addConfigurationProperty("cursor.mode", "navigation");
2780 addMapperAndConfigure(mapper);
2781
2782 NotifyMotionArgs args;
2783
2784 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002785 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
2786 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
2787 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2788 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2790 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2792 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2793 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2794
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2796 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2798 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2799 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2800
Michael Wrightd02c5b62014-02-10 15:10:22 -08002801 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002802 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
2803 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
2804 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2808 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2809 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2810
2811 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002812 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
2813 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002815 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2817 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2818
2819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2822 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2823}
2824
2825TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2826 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2827 addConfigurationProperty("cursor.mode", "navigation");
2828 addMapperAndConfigure(mapper);
2829
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002830 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002831 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2832 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2833 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2834 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2835 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2836 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2837 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2838 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2839}
2840
2841TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2842 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2843 addConfigurationProperty("cursor.mode", "navigation");
2844 addConfigurationProperty("cursor.orientationAware", "1");
2845 addMapperAndConfigure(mapper);
2846
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002847 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2849 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2850 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2851 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2852 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2853 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2854 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2855 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2856
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002857 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2859 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2860 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2861 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2862 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2863 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2864 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2865 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2866
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002867 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002868 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2869 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2870 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2871 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2872 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2873 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2874 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2875 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2876
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002877 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2879 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2880 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2881 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2882 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2883 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2884 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2885 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2886}
2887
2888TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2889 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2890 addConfigurationProperty("cursor.mode", "pointer");
2891 addMapperAndConfigure(mapper);
2892
2893 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2894 mFakePointerController->setPosition(100, 200);
2895 mFakePointerController->setButtonState(0);
2896
2897 NotifyMotionArgs motionArgs;
2898 NotifyKeyArgs keyArgs;
2899
2900 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002901 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
2902 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2904 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2905 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2906 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2908 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2909
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2912 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2913 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2915 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2916
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002917 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
2918 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002920 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002921 ASSERT_EQ(0, motionArgs.buttonState);
2922 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2924 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2925
2926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002927 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928 ASSERT_EQ(0, motionArgs.buttonState);
2929 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2931 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2932
2933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002935 ASSERT_EQ(0, motionArgs.buttonState);
2936 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2938 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2939
2940 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002941 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
2942 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
2943 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2945 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2946 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2947 motionArgs.buttonState);
2948 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2949 mFakePointerController->getButtonState());
2950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2951 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2952
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2954 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2955 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2956 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2957 mFakePointerController->getButtonState());
2958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2959 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2960
2961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2963 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2964 motionArgs.buttonState);
2965 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2966 mFakePointerController->getButtonState());
2967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2968 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2969
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002970 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
2971 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002973 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2975 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2977 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2978
2979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002981 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2982 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2984 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2985
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002986 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2987 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2990 ASSERT_EQ(0, motionArgs.buttonState);
2991 ASSERT_EQ(0, mFakePointerController->getButtonState());
2992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2993 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002994 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
2995 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002996
2997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998 ASSERT_EQ(0, motionArgs.buttonState);
2999 ASSERT_EQ(0, mFakePointerController->getButtonState());
3000 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3002 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 -08003003
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3005 ASSERT_EQ(0, motionArgs.buttonState);
3006 ASSERT_EQ(0, mFakePointerController->getButtonState());
3007 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3009 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3010
3011 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003012 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3013 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3015 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3016 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003017
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003019 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3021 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3023 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3024
3025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3027 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3028 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3030 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3031
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003032 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3033 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003035 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036 ASSERT_EQ(0, motionArgs.buttonState);
3037 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3039 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3040
3041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003043 ASSERT_EQ(0, motionArgs.buttonState);
3044 ASSERT_EQ(0, mFakePointerController->getButtonState());
3045
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3047 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3049 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3050 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3051
3052 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003053 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3054 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3056 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3057 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003058
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003060 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3062 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3064 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3065
3066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3068 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3069 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3071 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3072
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003073 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3074 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077 ASSERT_EQ(0, motionArgs.buttonState);
3078 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3080 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003081
3082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3083 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3084 ASSERT_EQ(0, motionArgs.buttonState);
3085 ASSERT_EQ(0, mFakePointerController->getButtonState());
3086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3087 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3088
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3090 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3091 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3092
3093 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003094 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3095 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3097 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3098 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003099
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003101 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3103 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3105 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3106
3107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3108 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3109 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3110 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3112 100.0f, 200.0f, 0.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_FORWARD, 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(0, motionArgs.buttonState);
3119 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3121 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003122
3123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3124 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3125 ASSERT_EQ(0, motionArgs.buttonState);
3126 ASSERT_EQ(0, mFakePointerController->getButtonState());
3127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3128 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3129
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3131 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3132 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3133
3134 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003135 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3136 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3138 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3139 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003140
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003142 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3144 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3146 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3147
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3150 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3151 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 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
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003155 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3156 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003158 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159 ASSERT_EQ(0, motionArgs.buttonState);
3160 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3162 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003163
3164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3166 ASSERT_EQ(0, motionArgs.buttonState);
3167 ASSERT_EQ(0, mFakePointerController->getButtonState());
3168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3169 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3170
Michael Wrightd02c5b62014-02-10 15:10:22 -08003171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3172 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3173 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3174}
3175
3176TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3177 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3178 addConfigurationProperty("cursor.mode", "pointer");
3179 addMapperAndConfigure(mapper);
3180
3181 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3182 mFakePointerController->setPosition(100, 200);
3183 mFakePointerController->setButtonState(0);
3184
3185 NotifyMotionArgs args;
3186
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003187 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3188 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3189 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003191 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3192 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3194 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3195 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3196}
3197
3198TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3199 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3200 addConfigurationProperty("cursor.mode", "pointer");
3201 mFakePolicy->setPointerCapture(true);
3202 addMapperAndConfigure(mapper);
3203
3204 NotifyDeviceResetArgs resetArgs;
3205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3206 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3207 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3208
3209 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3210 mFakePointerController->setPosition(100, 200);
3211 mFakePointerController->setButtonState(0);
3212
3213 NotifyMotionArgs args;
3214
3215 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003216 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3217 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3218 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3220 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3223 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3224 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3225
3226 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003227 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3228 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3230 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3231 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3233 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3235 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3238 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3239
3240 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003241 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3242 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3244 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3247 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3249 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3250 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3252 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3253
3254 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003255 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3256 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3257 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3259 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3262 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3263 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3264
3265 // Disable pointer capture and check that the device generation got bumped
3266 // and events are generated the usual way.
3267 const uint32_t generation = mFakeContext->getGeneration();
3268 mFakePolicy->setPointerCapture(false);
3269 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3270 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3271
3272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3273 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3274 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3275
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003276 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3277 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3278 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3280 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3283 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3284 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3285}
3286
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003287TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3288 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3289 addMapperAndConfigure(mapper);
3290
3291 // Setup PointerController for second display.
3292 constexpr int32_t SECOND_DISPLAY_ID = 1;
3293 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3294 mFakePointerController->setPosition(100, 200);
3295 mFakePointerController->setButtonState(0);
3296 mFakePointerController->setDisplayId(SECOND_DISPLAY_ID);
3297
3298 NotifyMotionArgs args;
3299 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3300 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3301 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3303 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3306 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3307 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3308 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3309}
3310
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311
3312// --- TouchInputMapperTest ---
3313
3314class TouchInputMapperTest : public InputMapperTest {
3315protected:
3316 static const int32_t RAW_X_MIN;
3317 static const int32_t RAW_X_MAX;
3318 static const int32_t RAW_Y_MIN;
3319 static const int32_t RAW_Y_MAX;
3320 static const int32_t RAW_TOUCH_MIN;
3321 static const int32_t RAW_TOUCH_MAX;
3322 static const int32_t RAW_TOOL_MIN;
3323 static const int32_t RAW_TOOL_MAX;
3324 static const int32_t RAW_PRESSURE_MIN;
3325 static const int32_t RAW_PRESSURE_MAX;
3326 static const int32_t RAW_ORIENTATION_MIN;
3327 static const int32_t RAW_ORIENTATION_MAX;
3328 static const int32_t RAW_DISTANCE_MIN;
3329 static const int32_t RAW_DISTANCE_MAX;
3330 static const int32_t RAW_TILT_MIN;
3331 static const int32_t RAW_TILT_MAX;
3332 static const int32_t RAW_ID_MIN;
3333 static const int32_t RAW_ID_MAX;
3334 static const int32_t RAW_SLOT_MIN;
3335 static const int32_t RAW_SLOT_MAX;
3336 static const float X_PRECISION;
3337 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003338 static const float X_PRECISION_VIRTUAL;
3339 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340
3341 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003342 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343
3344 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003346 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003347 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003348
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349 enum Axes {
3350 POSITION = 1 << 0,
3351 TOUCH = 1 << 1,
3352 TOOL = 1 << 2,
3353 PRESSURE = 1 << 3,
3354 ORIENTATION = 1 << 4,
3355 MINOR = 1 << 5,
3356 ID = 1 << 6,
3357 DISTANCE = 1 << 7,
3358 TILT = 1 << 8,
3359 SLOT = 1 << 9,
3360 TOOL_TYPE = 1 << 10,
3361 };
3362
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003363 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3364 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003365 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003367 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 int32_t toRawX(float displayX);
3369 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003370 float toCookedX(float rawX, float rawY);
3371 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003373 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003375 float toDisplayY(int32_t rawY, int32_t displayHeight);
3376
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377};
3378
3379const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3380const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3381const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3382const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3383const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3384const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3385const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3386const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003387const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3388const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3390const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3391const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3392const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3393const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3394const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3395const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3396const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3397const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3398const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3399const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3400const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003401const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3402 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3403const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3404 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003405const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3406 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407
3408const float TouchInputMapperTest::GEOMETRIC_SCALE =
3409 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3410 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3411
3412const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3413 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3414 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3415};
3416
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003417void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003418 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003419 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3420}
3421
3422void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3423 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3424 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425}
3426
Santos Cordonfa5cf462017-04-05 10:37:00 -07003427void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003428 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3429 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003430 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003431}
3432
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433void TouchInputMapperTest::prepareVirtualKeys() {
3434 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3435 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3436 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3437 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3438}
3439
Jason Gerecke489fda82012-09-07 17:19:40 -07003440void TouchInputMapperTest::prepareLocationCalibration() {
3441 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3442}
3443
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444int32_t TouchInputMapperTest::toRawX(float displayX) {
3445 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3446}
3447
3448int32_t TouchInputMapperTest::toRawY(float displayY) {
3449 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3450}
3451
Jason Gerecke489fda82012-09-07 17:19:40 -07003452float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3453 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3454 return rawX;
3455}
3456
3457float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3458 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3459 return rawY;
3460}
3461
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003463 return toDisplayX(rawX, DISPLAY_WIDTH);
3464}
3465
3466float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3467 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468}
3469
3470float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003471 return toDisplayY(rawY, DISPLAY_HEIGHT);
3472}
3473
3474float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3475 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476}
3477
3478
3479// --- SingleTouchInputMapperTest ---
3480
3481class SingleTouchInputMapperTest : public TouchInputMapperTest {
3482protected:
3483 void prepareButtons();
3484 void prepareAxes(int axes);
3485
3486 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3487 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3488 void processUp(SingleTouchInputMapper* mappery);
3489 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3490 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3491 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3492 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3493 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3494 void processSync(SingleTouchInputMapper* mapper);
3495};
3496
3497void SingleTouchInputMapperTest::prepareButtons() {
3498 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3499}
3500
3501void SingleTouchInputMapperTest::prepareAxes(int axes) {
3502 if (axes & POSITION) {
3503 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3504 RAW_X_MIN, RAW_X_MAX, 0, 0);
3505 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3506 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3507 }
3508 if (axes & PRESSURE) {
3509 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3510 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3511 }
3512 if (axes & TOOL) {
3513 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3514 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3515 }
3516 if (axes & DISTANCE) {
3517 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3518 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3519 }
3520 if (axes & TILT) {
3521 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3522 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3523 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3524 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3525 }
3526}
3527
3528void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003529 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3530 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3531 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532}
3533
3534void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003535 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3536 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537}
3538
3539void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003540 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541}
3542
3543void SingleTouchInputMapperTest::processPressure(
3544 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003545 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546}
3547
3548void SingleTouchInputMapperTest::processToolMajor(
3549 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003550 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551}
3552
3553void SingleTouchInputMapperTest::processDistance(
3554 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003555 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556}
3557
3558void SingleTouchInputMapperTest::processTilt(
3559 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003560 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3561 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562}
3563
3564void SingleTouchInputMapperTest::processKey(
3565 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003566 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567}
3568
3569void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003570 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003571}
3572
3573
3574TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3575 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3576 prepareButtons();
3577 prepareAxes(POSITION);
3578 addMapperAndConfigure(mapper);
3579
3580 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3581}
3582
3583TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3584 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3585 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3586 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3587 prepareButtons();
3588 prepareAxes(POSITION);
3589 addMapperAndConfigure(mapper);
3590
3591 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3592}
3593
3594TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3595 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3596 prepareButtons();
3597 prepareAxes(POSITION);
3598 addConfigurationProperty("touch.deviceType", "touchPad");
3599 addMapperAndConfigure(mapper);
3600
3601 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3602}
3603
3604TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3605 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3606 prepareButtons();
3607 prepareAxes(POSITION);
3608 addConfigurationProperty("touch.deviceType", "touchScreen");
3609 addMapperAndConfigure(mapper);
3610
3611 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3612}
3613
3614TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3615 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3616 addConfigurationProperty("touch.deviceType", "touchScreen");
3617 prepareDisplay(DISPLAY_ORIENTATION_0);
3618 prepareButtons();
3619 prepareAxes(POSITION);
3620 prepareVirtualKeys();
3621 addMapperAndConfigure(mapper);
3622
3623 // Unknown key.
3624 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3625
3626 // Virtual key is down.
3627 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3628 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3629 processDown(mapper, x, y);
3630 processSync(mapper);
3631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3632
3633 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3634
3635 // Virtual key is up.
3636 processUp(mapper);
3637 processSync(mapper);
3638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3639
3640 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3641}
3642
3643TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3644 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3645 addConfigurationProperty("touch.deviceType", "touchScreen");
3646 prepareDisplay(DISPLAY_ORIENTATION_0);
3647 prepareButtons();
3648 prepareAxes(POSITION);
3649 prepareVirtualKeys();
3650 addMapperAndConfigure(mapper);
3651
3652 // Unknown key.
3653 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3654
3655 // Virtual key is down.
3656 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3657 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3658 processDown(mapper, x, y);
3659 processSync(mapper);
3660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3661
3662 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3663
3664 // Virtual key is up.
3665 processUp(mapper);
3666 processSync(mapper);
3667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3668
3669 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3670}
3671
3672TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3673 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3674 addConfigurationProperty("touch.deviceType", "touchScreen");
3675 prepareDisplay(DISPLAY_ORIENTATION_0);
3676 prepareButtons();
3677 prepareAxes(POSITION);
3678 prepareVirtualKeys();
3679 addMapperAndConfigure(mapper);
3680
3681 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3682 uint8_t flags[2] = { 0, 0 };
3683 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3684 ASSERT_TRUE(flags[0]);
3685 ASSERT_FALSE(flags[1]);
3686}
3687
3688TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3689 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3690 addConfigurationProperty("touch.deviceType", "touchScreen");
3691 prepareDisplay(DISPLAY_ORIENTATION_0);
3692 prepareButtons();
3693 prepareAxes(POSITION);
3694 prepareVirtualKeys();
3695 addMapperAndConfigure(mapper);
3696
3697 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3698
3699 NotifyKeyArgs args;
3700
3701 // Press virtual key.
3702 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3703 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3704 processDown(mapper, x, y);
3705 processSync(mapper);
3706
3707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3708 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3709 ASSERT_EQ(DEVICE_ID, args.deviceId);
3710 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3711 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3712 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3713 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3714 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3715 ASSERT_EQ(KEY_HOME, args.scanCode);
3716 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3717 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3718
3719 // Release virtual key.
3720 processUp(mapper);
3721 processSync(mapper);
3722
3723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3724 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3725 ASSERT_EQ(DEVICE_ID, args.deviceId);
3726 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3727 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3728 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3729 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3730 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3731 ASSERT_EQ(KEY_HOME, args.scanCode);
3732 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3733 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3734
3735 // Should not have sent any motions.
3736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3737}
3738
3739TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3740 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3741 addConfigurationProperty("touch.deviceType", "touchScreen");
3742 prepareDisplay(DISPLAY_ORIENTATION_0);
3743 prepareButtons();
3744 prepareAxes(POSITION);
3745 prepareVirtualKeys();
3746 addMapperAndConfigure(mapper);
3747
3748 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3749
3750 NotifyKeyArgs keyArgs;
3751
3752 // Press virtual key.
3753 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3754 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3755 processDown(mapper, x, y);
3756 processSync(mapper);
3757
3758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3759 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3760 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3761 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3762 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3763 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3764 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3765 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3766 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3767 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3768 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3769
3770 // Move out of bounds. This should generate a cancel and a pointer down since we moved
3771 // into the display area.
3772 y -= 100;
3773 processMove(mapper, x, y);
3774 processSync(mapper);
3775
3776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3777 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3778 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3779 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3780 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3781 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3782 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3783 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3784 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3785 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3786 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3787 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3788
3789 NotifyMotionArgs motionArgs;
3790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3791 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3792 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3793 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3794 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3795 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3796 ASSERT_EQ(0, motionArgs.flags);
3797 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3798 ASSERT_EQ(0, motionArgs.buttonState);
3799 ASSERT_EQ(0, motionArgs.edgeFlags);
3800 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3801 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3804 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3805 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3806 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3807 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3808
3809 // Keep moving out of bounds. Should generate a pointer move.
3810 y -= 50;
3811 processMove(mapper, x, y);
3812 processSync(mapper);
3813
3814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3815 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3816 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3817 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3818 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3820 ASSERT_EQ(0, motionArgs.flags);
3821 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3822 ASSERT_EQ(0, motionArgs.buttonState);
3823 ASSERT_EQ(0, motionArgs.edgeFlags);
3824 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3825 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3828 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3829 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3830 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3831 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3832
3833 // Release out of bounds. Should generate a pointer up.
3834 processUp(mapper);
3835 processSync(mapper);
3836
3837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3838 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3839 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3840 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3841 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3842 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3843 ASSERT_EQ(0, motionArgs.flags);
3844 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3845 ASSERT_EQ(0, motionArgs.buttonState);
3846 ASSERT_EQ(0, motionArgs.edgeFlags);
3847 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3848 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3851 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3852 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3853 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3854 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3855
3856 // Should not have sent any more keys or motions.
3857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3859}
3860
3861TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3862 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3863 addConfigurationProperty("touch.deviceType", "touchScreen");
3864 prepareDisplay(DISPLAY_ORIENTATION_0);
3865 prepareButtons();
3866 prepareAxes(POSITION);
3867 prepareVirtualKeys();
3868 addMapperAndConfigure(mapper);
3869
3870 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3871
3872 NotifyMotionArgs motionArgs;
3873
3874 // Initially go down out of bounds.
3875 int32_t x = -10;
3876 int32_t y = -10;
3877 processDown(mapper, x, y);
3878 processSync(mapper);
3879
3880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3881
3882 // Move into the display area. Should generate a pointer down.
3883 x = 50;
3884 y = 75;
3885 processMove(mapper, x, y);
3886 processSync(mapper);
3887
3888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3889 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3890 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3891 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3892 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3893 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3894 ASSERT_EQ(0, motionArgs.flags);
3895 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3896 ASSERT_EQ(0, motionArgs.buttonState);
3897 ASSERT_EQ(0, motionArgs.edgeFlags);
3898 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3899 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3900 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3902 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3903 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3904 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3906
3907 // Release. Should generate a pointer up.
3908 processUp(mapper);
3909 processSync(mapper);
3910
3911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3913 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3914 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3915 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3916 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3917 ASSERT_EQ(0, motionArgs.flags);
3918 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3919 ASSERT_EQ(0, motionArgs.buttonState);
3920 ASSERT_EQ(0, motionArgs.edgeFlags);
3921 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3922 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3923 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3925 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3926 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3927 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3928 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3929
3930 // Should not have sent any more keys or motions.
3931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3933}
3934
Santos Cordonfa5cf462017-04-05 10:37:00 -07003935TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3936 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3937 addConfigurationProperty("touch.deviceType", "touchScreen");
3938 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3939
3940 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
3941 prepareButtons();
3942 prepareAxes(POSITION);
3943 prepareVirtualKeys();
3944 addMapperAndConfigure(mapper);
3945
3946 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3947
3948 NotifyMotionArgs motionArgs;
3949
3950 // Down.
3951 int32_t x = 100;
3952 int32_t y = 125;
3953 processDown(mapper, x, y);
3954 processSync(mapper);
3955
3956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3957 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3958 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3959 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3960 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3961 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3962 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3963 ASSERT_EQ(0, motionArgs.flags);
3964 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3965 ASSERT_EQ(0, motionArgs.buttonState);
3966 ASSERT_EQ(0, motionArgs.edgeFlags);
3967 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3968 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3969 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3971 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3972 1, 0, 0, 0, 0, 0, 0, 0));
3973 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3974 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3975 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3976
3977 // Move.
3978 x += 50;
3979 y += 75;
3980 processMove(mapper, x, y);
3981 processSync(mapper);
3982
3983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3984 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3985 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3986 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3987 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3988 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3990 ASSERT_EQ(0, motionArgs.flags);
3991 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3992 ASSERT_EQ(0, motionArgs.buttonState);
3993 ASSERT_EQ(0, motionArgs.edgeFlags);
3994 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3995 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3996 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3998 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3999 1, 0, 0, 0, 0, 0, 0, 0));
4000 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4001 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4002 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4003
4004 // Up.
4005 processUp(mapper);
4006 processSync(mapper);
4007
4008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4009 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4010 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4011 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4012 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4013 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4014 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4015 ASSERT_EQ(0, motionArgs.flags);
4016 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4017 ASSERT_EQ(0, motionArgs.buttonState);
4018 ASSERT_EQ(0, motionArgs.edgeFlags);
4019 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4020 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4021 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4023 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4024 1, 0, 0, 0, 0, 0, 0, 0));
4025 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4026 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4027 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4028
4029 // Should not have sent any more keys or motions.
4030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4032}
4033
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
4035 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4036 addConfigurationProperty("touch.deviceType", "touchScreen");
4037 prepareDisplay(DISPLAY_ORIENTATION_0);
4038 prepareButtons();
4039 prepareAxes(POSITION);
4040 prepareVirtualKeys();
4041 addMapperAndConfigure(mapper);
4042
4043 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4044
4045 NotifyMotionArgs motionArgs;
4046
4047 // Down.
4048 int32_t x = 100;
4049 int32_t y = 125;
4050 processDown(mapper, x, y);
4051 processSync(mapper);
4052
4053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4054 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4055 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4056 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4057 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4058 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4059 ASSERT_EQ(0, motionArgs.flags);
4060 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4061 ASSERT_EQ(0, motionArgs.buttonState);
4062 ASSERT_EQ(0, motionArgs.edgeFlags);
4063 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4064 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4065 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4067 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4068 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4069 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4070 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4071
4072 // Move.
4073 x += 50;
4074 y += 75;
4075 processMove(mapper, x, y);
4076 processSync(mapper);
4077
4078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4080 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4081 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4082 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4084 ASSERT_EQ(0, motionArgs.flags);
4085 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4086 ASSERT_EQ(0, motionArgs.buttonState);
4087 ASSERT_EQ(0, motionArgs.edgeFlags);
4088 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4089 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4092 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4093 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4094 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4095 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4096
4097 // Up.
4098 processUp(mapper);
4099 processSync(mapper);
4100
4101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4102 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4103 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4104 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4105 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4106 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4116 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4117 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4118 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4119
4120 // Should not have sent any more keys or motions.
4121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4123}
4124
4125TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
4126 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4127 addConfigurationProperty("touch.deviceType", "touchScreen");
4128 prepareButtons();
4129 prepareAxes(POSITION);
4130 addConfigurationProperty("touch.orientationAware", "0");
4131 addMapperAndConfigure(mapper);
4132
4133 NotifyMotionArgs args;
4134
4135 // Rotation 90.
4136 prepareDisplay(DISPLAY_ORIENTATION_90);
4137 processDown(mapper, toRawX(50), toRawY(75));
4138 processSync(mapper);
4139
4140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4141 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4142 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4143
4144 processUp(mapper);
4145 processSync(mapper);
4146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4147}
4148
4149TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4150 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4151 addConfigurationProperty("touch.deviceType", "touchScreen");
4152 prepareButtons();
4153 prepareAxes(POSITION);
4154 addMapperAndConfigure(mapper);
4155
4156 NotifyMotionArgs args;
4157
4158 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004159 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 prepareDisplay(DISPLAY_ORIENTATION_0);
4161 processDown(mapper, toRawX(50), toRawY(75));
4162 processSync(mapper);
4163
4164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4165 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4166 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4167
4168 processUp(mapper);
4169 processSync(mapper);
4170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4171
4172 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004173 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174 prepareDisplay(DISPLAY_ORIENTATION_90);
4175 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4176 processSync(mapper);
4177
4178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4179 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4180 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4181
4182 processUp(mapper);
4183 processSync(mapper);
4184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4185
4186 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004187 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188 prepareDisplay(DISPLAY_ORIENTATION_180);
4189 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4190 processSync(mapper);
4191
4192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4193 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4194 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4195
4196 processUp(mapper);
4197 processSync(mapper);
4198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4199
4200 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004201 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 prepareDisplay(DISPLAY_ORIENTATION_270);
4203 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4204 processSync(mapper);
4205
4206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4207 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4208 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4209
4210 processUp(mapper);
4211 processSync(mapper);
4212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4213}
4214
4215TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4216 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4217 addConfigurationProperty("touch.deviceType", "touchScreen");
4218 prepareDisplay(DISPLAY_ORIENTATION_0);
4219 prepareButtons();
4220 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4221 addMapperAndConfigure(mapper);
4222
4223 // These calculations are based on the input device calibration documentation.
4224 int32_t rawX = 100;
4225 int32_t rawY = 200;
4226 int32_t rawPressure = 10;
4227 int32_t rawToolMajor = 12;
4228 int32_t rawDistance = 2;
4229 int32_t rawTiltX = 30;
4230 int32_t rawTiltY = 110;
4231
4232 float x = toDisplayX(rawX);
4233 float y = toDisplayY(rawY);
4234 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4235 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4236 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4237 float distance = float(rawDistance);
4238
4239 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4240 float tiltScale = M_PI / 180;
4241 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4242 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4243 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4244 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4245
4246 processDown(mapper, rawX, rawY);
4247 processPressure(mapper, rawPressure);
4248 processToolMajor(mapper, rawToolMajor);
4249 processDistance(mapper, rawDistance);
4250 processTilt(mapper, rawTiltX, rawTiltY);
4251 processSync(mapper);
4252
4253 NotifyMotionArgs args;
4254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4256 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4257 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4258}
4259
Jason Gerecke489fda82012-09-07 17:19:40 -07004260TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4261 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4262 addConfigurationProperty("touch.deviceType", "touchScreen");
4263 prepareDisplay(DISPLAY_ORIENTATION_0);
4264 prepareLocationCalibration();
4265 prepareButtons();
4266 prepareAxes(POSITION);
4267 addMapperAndConfigure(mapper);
4268
4269 int32_t rawX = 100;
4270 int32_t rawY = 200;
4271
4272 float x = toDisplayX(toCookedX(rawX, rawY));
4273 float y = toDisplayY(toCookedY(rawX, rawY));
4274
4275 processDown(mapper, rawX, rawY);
4276 processSync(mapper);
4277
4278 NotifyMotionArgs args;
4279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4281 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4282}
4283
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4285 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4286 addConfigurationProperty("touch.deviceType", "touchScreen");
4287 prepareDisplay(DISPLAY_ORIENTATION_0);
4288 prepareButtons();
4289 prepareAxes(POSITION);
4290 addMapperAndConfigure(mapper);
4291
4292 NotifyMotionArgs motionArgs;
4293 NotifyKeyArgs keyArgs;
4294
4295 processDown(mapper, 100, 200);
4296 processSync(mapper);
4297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4298 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4299 ASSERT_EQ(0, motionArgs.buttonState);
4300
4301 // press BTN_LEFT, release BTN_LEFT
4302 processKey(mapper, BTN_LEFT, 1);
4303 processSync(mapper);
4304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4306 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4307
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4309 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4310 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4311
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 processKey(mapper, BTN_LEFT, 0);
4313 processSync(mapper);
4314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317
4318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004320 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321
4322 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4323 processKey(mapper, BTN_RIGHT, 1);
4324 processKey(mapper, BTN_MIDDLE, 1);
4325 processSync(mapper);
4326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4328 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4329 motionArgs.buttonState);
4330
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4332 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4333 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4334
4335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4336 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4337 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4338 motionArgs.buttonState);
4339
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 processKey(mapper, BTN_RIGHT, 0);
4341 processSync(mapper);
4342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004345
4346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349
4350 processKey(mapper, BTN_MIDDLE, 0);
4351 processSync(mapper);
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004353 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004358 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359
4360 // press BTN_BACK, release BTN_BACK
4361 processKey(mapper, BTN_BACK, 1);
4362 processSync(mapper);
4363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4364 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4365 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004366
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4370
4371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4372 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4373 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374
4375 processKey(mapper, BTN_BACK, 0);
4376 processSync(mapper);
4377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380
4381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004383 ASSERT_EQ(0, motionArgs.buttonState);
4384
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4386 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4387 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4388
4389 // press BTN_SIDE, release BTN_SIDE
4390 processKey(mapper, BTN_SIDE, 1);
4391 processSync(mapper);
4392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4393 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4394 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4399
4400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4402 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403
4404 processKey(mapper, BTN_SIDE, 0);
4405 processSync(mapper);
4406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004409
4410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412 ASSERT_EQ(0, motionArgs.buttonState);
4413
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4415 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4416 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4417
4418 // press BTN_FORWARD, release BTN_FORWARD
4419 processKey(mapper, BTN_FORWARD, 1);
4420 processSync(mapper);
4421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4422 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4423 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004424
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004427 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4428
4429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4430 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4431 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432
4433 processKey(mapper, BTN_FORWARD, 0);
4434 processSync(mapper);
4435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004436 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004438
4439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004441 ASSERT_EQ(0, motionArgs.buttonState);
4442
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4444 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4445 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4446
4447 // press BTN_EXTRA, release BTN_EXTRA
4448 processKey(mapper, BTN_EXTRA, 1);
4449 processSync(mapper);
4450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4451 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4452 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004456 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4457
4458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4460 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461
4462 processKey(mapper, BTN_EXTRA, 0);
4463 processSync(mapper);
4464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470 ASSERT_EQ(0, motionArgs.buttonState);
4471
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4473 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4474 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4475
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4477
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478 // press BTN_STYLUS, release BTN_STYLUS
4479 processKey(mapper, BTN_STYLUS, 1);
4480 processSync(mapper);
4481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004483 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4484
4485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4487 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488
4489 processKey(mapper, BTN_STYLUS, 0);
4490 processSync(mapper);
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004494
4495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004496 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004497 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498
4499 // press BTN_STYLUS2, release BTN_STYLUS2
4500 processKey(mapper, BTN_STYLUS2, 1);
4501 processSync(mapper);
4502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004504 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4505
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4508 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509
4510 processKey(mapper, BTN_STYLUS2, 0);
4511 processSync(mapper);
4512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004513 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004515
4516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004518 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519
4520 // release touch
4521 processUp(mapper);
4522 processSync(mapper);
4523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4524 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4525 ASSERT_EQ(0, motionArgs.buttonState);
4526}
4527
4528TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4529 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4530 addConfigurationProperty("touch.deviceType", "touchScreen");
4531 prepareDisplay(DISPLAY_ORIENTATION_0);
4532 prepareButtons();
4533 prepareAxes(POSITION);
4534 addMapperAndConfigure(mapper);
4535
4536 NotifyMotionArgs motionArgs;
4537
4538 // default tool type is finger
4539 processDown(mapper, 100, 200);
4540 processSync(mapper);
4541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4542 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4544
4545 // eraser
4546 processKey(mapper, BTN_TOOL_RUBBER, 1);
4547 processSync(mapper);
4548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4551
4552 // stylus
4553 processKey(mapper, BTN_TOOL_RUBBER, 0);
4554 processKey(mapper, BTN_TOOL_PEN, 1);
4555 processSync(mapper);
4556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4559
4560 // brush
4561 processKey(mapper, BTN_TOOL_PEN, 0);
4562 processKey(mapper, BTN_TOOL_BRUSH, 1);
4563 processSync(mapper);
4564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4567
4568 // pencil
4569 processKey(mapper, BTN_TOOL_BRUSH, 0);
4570 processKey(mapper, BTN_TOOL_PENCIL, 1);
4571 processSync(mapper);
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4575
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004576 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577 processKey(mapper, BTN_TOOL_PENCIL, 0);
4578 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4579 processSync(mapper);
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4583
4584 // mouse
4585 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4586 processKey(mapper, BTN_TOOL_MOUSE, 1);
4587 processSync(mapper);
4588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4591
4592 // lens
4593 processKey(mapper, BTN_TOOL_MOUSE, 0);
4594 processKey(mapper, BTN_TOOL_LENS, 1);
4595 processSync(mapper);
4596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4599
4600 // double-tap
4601 processKey(mapper, BTN_TOOL_LENS, 0);
4602 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4603 processSync(mapper);
4604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4607
4608 // triple-tap
4609 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4610 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4611 processSync(mapper);
4612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4615
4616 // quad-tap
4617 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4618 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4619 processSync(mapper);
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4623
4624 // finger
4625 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4626 processKey(mapper, BTN_TOOL_FINGER, 1);
4627 processSync(mapper);
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4631
4632 // stylus trumps finger
4633 processKey(mapper, BTN_TOOL_PEN, 1);
4634 processSync(mapper);
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4638
4639 // eraser trumps stylus
4640 processKey(mapper, BTN_TOOL_RUBBER, 1);
4641 processSync(mapper);
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4645
4646 // mouse trumps eraser
4647 processKey(mapper, BTN_TOOL_MOUSE, 1);
4648 processSync(mapper);
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4652
4653 // back to default tool type
4654 processKey(mapper, BTN_TOOL_MOUSE, 0);
4655 processKey(mapper, BTN_TOOL_RUBBER, 0);
4656 processKey(mapper, BTN_TOOL_PEN, 0);
4657 processKey(mapper, BTN_TOOL_FINGER, 0);
4658 processSync(mapper);
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4662}
4663
4664TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4665 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4666 addConfigurationProperty("touch.deviceType", "touchScreen");
4667 prepareDisplay(DISPLAY_ORIENTATION_0);
4668 prepareButtons();
4669 prepareAxes(POSITION);
4670 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4671 addMapperAndConfigure(mapper);
4672
4673 NotifyMotionArgs motionArgs;
4674
4675 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4676 processKey(mapper, BTN_TOOL_FINGER, 1);
4677 processMove(mapper, 100, 200);
4678 processSync(mapper);
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4680 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4682 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4683
4684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4685 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4687 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4688
4689 // move a little
4690 processMove(mapper, 150, 250);
4691 processSync(mapper);
4692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4693 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4695 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4696
4697 // down when BTN_TOUCH is pressed, pressure defaults to 1
4698 processKey(mapper, BTN_TOUCH, 1);
4699 processSync(mapper);
4700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4703 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4704
4705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4706 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4708 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4709
4710 // up when BTN_TOUCH is released, hover restored
4711 processKey(mapper, BTN_TOUCH, 0);
4712 processSync(mapper);
4713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4714 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4716 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4717
4718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4719 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4721 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4722
4723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4724 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4726 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4727
4728 // exit hover when pointer goes away
4729 processKey(mapper, BTN_TOOL_FINGER, 0);
4730 processSync(mapper);
4731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4732 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4734 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4735}
4736
4737TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4738 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4739 addConfigurationProperty("touch.deviceType", "touchScreen");
4740 prepareDisplay(DISPLAY_ORIENTATION_0);
4741 prepareButtons();
4742 prepareAxes(POSITION | PRESSURE);
4743 addMapperAndConfigure(mapper);
4744
4745 NotifyMotionArgs motionArgs;
4746
4747 // initially hovering because pressure is 0
4748 processDown(mapper, 100, 200);
4749 processPressure(mapper, 0);
4750 processSync(mapper);
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4754 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4755
4756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4759 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4760
4761 // move a little
4762 processMove(mapper, 150, 250);
4763 processSync(mapper);
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4765 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4767 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4768
4769 // down when pressure is non-zero
4770 processPressure(mapper, RAW_PRESSURE_MAX);
4771 processSync(mapper);
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4773 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4775 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4776
4777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4780 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4781
4782 // up when pressure becomes 0, hover restored
4783 processPressure(mapper, 0);
4784 processSync(mapper);
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4786 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4788 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4789
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4791 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4793 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4794
4795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4798 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4799
4800 // exit hover when pointer goes away
4801 processUp(mapper);
4802 processSync(mapper);
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4804 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4806 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4807}
4808
Dan Harmsaca28402018-12-17 13:55:20 -08004809
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810// --- MultiTouchInputMapperTest ---
4811
4812class MultiTouchInputMapperTest : public TouchInputMapperTest {
4813protected:
4814 void prepareAxes(int axes);
4815
4816 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4817 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4818 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4819 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4820 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4821 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4822 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4823 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4824 void processId(MultiTouchInputMapper* mapper, int32_t id);
4825 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4826 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4827 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4828 void processMTSync(MultiTouchInputMapper* mapper);
4829 void processSync(MultiTouchInputMapper* mapper);
4830};
4831
4832void MultiTouchInputMapperTest::prepareAxes(int axes) {
4833 if (axes & POSITION) {
4834 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4835 RAW_X_MIN, RAW_X_MAX, 0, 0);
4836 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4837 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4838 }
4839 if (axes & TOUCH) {
4840 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4841 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4842 if (axes & MINOR) {
4843 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4844 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4845 }
4846 }
4847 if (axes & TOOL) {
4848 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4849 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4850 if (axes & MINOR) {
4851 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4852 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4853 }
4854 }
4855 if (axes & ORIENTATION) {
4856 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4857 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4858 }
4859 if (axes & PRESSURE) {
4860 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4861 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4862 }
4863 if (axes & DISTANCE) {
4864 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4865 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4866 }
4867 if (axes & ID) {
4868 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4869 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4870 }
4871 if (axes & SLOT) {
4872 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4873 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4874 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4875 }
4876 if (axes & TOOL_TYPE) {
4877 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4878 0, MT_TOOL_MAX, 0, 0);
4879 }
4880}
4881
4882void MultiTouchInputMapperTest::processPosition(
4883 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004884 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
4885 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886}
4887
4888void MultiTouchInputMapperTest::processTouchMajor(
4889 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004890 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891}
4892
4893void MultiTouchInputMapperTest::processTouchMinor(
4894 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004895 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896}
4897
4898void MultiTouchInputMapperTest::processToolMajor(
4899 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004900 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901}
4902
4903void MultiTouchInputMapperTest::processToolMinor(
4904 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004905 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906}
4907
4908void MultiTouchInputMapperTest::processOrientation(
4909 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004910 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911}
4912
4913void MultiTouchInputMapperTest::processPressure(
4914 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004915 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916}
4917
4918void MultiTouchInputMapperTest::processDistance(
4919 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004920 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921}
4922
4923void MultiTouchInputMapperTest::processId(
4924 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004925 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926}
4927
4928void MultiTouchInputMapperTest::processSlot(
4929 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004930 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931}
4932
4933void MultiTouchInputMapperTest::processToolType(
4934 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004935 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936}
4937
4938void MultiTouchInputMapperTest::processKey(
4939 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004940 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941}
4942
4943void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004944 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945}
4946
4947void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004948 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949}
4950
4951
4952TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
4953 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4954 addConfigurationProperty("touch.deviceType", "touchScreen");
4955 prepareDisplay(DISPLAY_ORIENTATION_0);
4956 prepareAxes(POSITION);
4957 prepareVirtualKeys();
4958 addMapperAndConfigure(mapper);
4959
4960 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4961
4962 NotifyMotionArgs motionArgs;
4963
4964 // Two fingers down at once.
4965 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4966 processPosition(mapper, x1, y1);
4967 processMTSync(mapper);
4968 processPosition(mapper, x2, y2);
4969 processMTSync(mapper);
4970 processSync(mapper);
4971
4972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4973 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4974 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4975 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4976 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4977 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4978 ASSERT_EQ(0, motionArgs.flags);
4979 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4980 ASSERT_EQ(0, motionArgs.buttonState);
4981 ASSERT_EQ(0, motionArgs.edgeFlags);
4982 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4983 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4986 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4987 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4988 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4989 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4990
4991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4992 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4993 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4994 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4995 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4996 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4997 motionArgs.action);
4998 ASSERT_EQ(0, motionArgs.flags);
4999 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5000 ASSERT_EQ(0, motionArgs.buttonState);
5001 ASSERT_EQ(0, motionArgs.edgeFlags);
5002 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5003 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5005 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5006 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5008 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5010 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5011 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5012 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5013 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5014
5015 // Move.
5016 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5017 processPosition(mapper, x1, y1);
5018 processMTSync(mapper);
5019 processPosition(mapper, x2, y2);
5020 processMTSync(mapper);
5021 processSync(mapper);
5022
5023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5024 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5025 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5026 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5027 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5029 ASSERT_EQ(0, motionArgs.flags);
5030 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5031 ASSERT_EQ(0, motionArgs.buttonState);
5032 ASSERT_EQ(0, motionArgs.edgeFlags);
5033 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5034 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5036 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5039 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5041 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5042 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5043 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5044 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5045
5046 // First finger up.
5047 x2 += 15; y2 -= 20;
5048 processPosition(mapper, x2, y2);
5049 processMTSync(mapper);
5050 processSync(mapper);
5051
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5053 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5054 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5055 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5056 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5057 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5058 motionArgs.action);
5059 ASSERT_EQ(0, motionArgs.flags);
5060 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5061 ASSERT_EQ(0, motionArgs.buttonState);
5062 ASSERT_EQ(0, motionArgs.edgeFlags);
5063 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5064 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5065 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5066 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5069 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5071 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5072 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5073 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5074 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5075
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5077 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5078 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5079 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5080 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5082 ASSERT_EQ(0, motionArgs.flags);
5083 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5084 ASSERT_EQ(0, motionArgs.buttonState);
5085 ASSERT_EQ(0, motionArgs.edgeFlags);
5086 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5087 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5088 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5090 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5091 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5092 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5093 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5094
5095 // Move.
5096 x2 += 20; y2 -= 25;
5097 processPosition(mapper, x2, y2);
5098 processMTSync(mapper);
5099 processSync(mapper);
5100
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5102 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5103 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5104 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5105 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5107 ASSERT_EQ(0, motionArgs.flags);
5108 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5109 ASSERT_EQ(0, motionArgs.buttonState);
5110 ASSERT_EQ(0, motionArgs.edgeFlags);
5111 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5112 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5115 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5116 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5117 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5118 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5119
5120 // New finger down.
5121 int32_t x3 = 700, y3 = 300;
5122 processPosition(mapper, x2, y2);
5123 processMTSync(mapper);
5124 processPosition(mapper, x3, y3);
5125 processMTSync(mapper);
5126 processSync(mapper);
5127
5128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5130 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5131 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5132 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5133 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5134 motionArgs.action);
5135 ASSERT_EQ(0, motionArgs.flags);
5136 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5137 ASSERT_EQ(0, motionArgs.buttonState);
5138 ASSERT_EQ(0, motionArgs.edgeFlags);
5139 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5140 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5142 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5145 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5147 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5148 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5149 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5150 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5151
5152 // Second finger up.
5153 x3 += 30; y3 -= 20;
5154 processPosition(mapper, x3, y3);
5155 processMTSync(mapper);
5156 processSync(mapper);
5157
5158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5159 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5160 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5161 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5162 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5163 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5164 motionArgs.action);
5165 ASSERT_EQ(0, motionArgs.flags);
5166 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5167 ASSERT_EQ(0, motionArgs.buttonState);
5168 ASSERT_EQ(0, motionArgs.edgeFlags);
5169 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5170 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5172 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5175 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5177 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5178 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5179 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5180 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5181
5182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5183 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5184 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5185 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5186 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5188 ASSERT_EQ(0, motionArgs.flags);
5189 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5190 ASSERT_EQ(0, motionArgs.buttonState);
5191 ASSERT_EQ(0, motionArgs.edgeFlags);
5192 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5193 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5196 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5197 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5198 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5199 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5200
5201 // Last finger up.
5202 processMTSync(mapper);
5203 processSync(mapper);
5204
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5206 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5207 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5208 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5209 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5210 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5211 ASSERT_EQ(0, motionArgs.flags);
5212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5213 ASSERT_EQ(0, motionArgs.buttonState);
5214 ASSERT_EQ(0, motionArgs.edgeFlags);
5215 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5216 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5219 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5220 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5221 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5222 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5223
5224 // Should not have sent any more keys or motions.
5225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5227}
5228
5229TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5230 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5231 addConfigurationProperty("touch.deviceType", "touchScreen");
5232 prepareDisplay(DISPLAY_ORIENTATION_0);
5233 prepareAxes(POSITION | ID);
5234 prepareVirtualKeys();
5235 addMapperAndConfigure(mapper);
5236
5237 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5238
5239 NotifyMotionArgs motionArgs;
5240
5241 // Two fingers down at once.
5242 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5243 processPosition(mapper, x1, y1);
5244 processId(mapper, 1);
5245 processMTSync(mapper);
5246 processPosition(mapper, x2, y2);
5247 processId(mapper, 2);
5248 processMTSync(mapper);
5249 processSync(mapper);
5250
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5253 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5254 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5257 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5258
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5260 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5261 motionArgs.action);
5262 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5263 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5265 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5268 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5270 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5271
5272 // Move.
5273 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5274 processPosition(mapper, x1, y1);
5275 processId(mapper, 1);
5276 processMTSync(mapper);
5277 processPosition(mapper, x2, y2);
5278 processId(mapper, 2);
5279 processMTSync(mapper);
5280 processSync(mapper);
5281
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5284 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5285 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5287 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5288 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5290 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5292 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5293
5294 // First finger up.
5295 x2 += 15; y2 -= 20;
5296 processPosition(mapper, x2, y2);
5297 processId(mapper, 2);
5298 processMTSync(mapper);
5299 processSync(mapper);
5300
5301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5302 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5303 motionArgs.action);
5304 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5305 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5307 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5310 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5312 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5313
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5316 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5317 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5320 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5321
5322 // Move.
5323 x2 += 20; y2 -= 25;
5324 processPosition(mapper, x2, y2);
5325 processId(mapper, 2);
5326 processMTSync(mapper);
5327 processSync(mapper);
5328
5329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5331 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5332 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5335 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5336
5337 // New finger down.
5338 int32_t x3 = 700, y3 = 300;
5339 processPosition(mapper, x2, y2);
5340 processId(mapper, 2);
5341 processMTSync(mapper);
5342 processPosition(mapper, x3, y3);
5343 processId(mapper, 3);
5344 processMTSync(mapper);
5345 processSync(mapper);
5346
5347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5348 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5349 motionArgs.action);
5350 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5351 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5353 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5356 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5358 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5359
5360 // Second finger up.
5361 x3 += 30; y3 -= 20;
5362 processPosition(mapper, x3, y3);
5363 processId(mapper, 3);
5364 processMTSync(mapper);
5365 processSync(mapper);
5366
5367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5368 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5369 motionArgs.action);
5370 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5371 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5373 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5376 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5378 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5379
5380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5382 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5383 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5386 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5387
5388 // Last finger up.
5389 processMTSync(mapper);
5390 processSync(mapper);
5391
5392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5393 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5394 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5395 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5398 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5399
5400 // Should not have sent any more keys or motions.
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5403}
5404
5405TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5406 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5407 addConfigurationProperty("touch.deviceType", "touchScreen");
5408 prepareDisplay(DISPLAY_ORIENTATION_0);
5409 prepareAxes(POSITION | ID | SLOT);
5410 prepareVirtualKeys();
5411 addMapperAndConfigure(mapper);
5412
5413 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5414
5415 NotifyMotionArgs motionArgs;
5416
5417 // Two fingers down at once.
5418 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5419 processPosition(mapper, x1, y1);
5420 processId(mapper, 1);
5421 processSlot(mapper, 1);
5422 processPosition(mapper, x2, y2);
5423 processId(mapper, 2);
5424 processSync(mapper);
5425
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5427 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5428 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5429 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5432 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5433
5434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5435 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5436 motionArgs.action);
5437 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5438 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5440 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5443 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5445 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5446
5447 // Move.
5448 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5449 processSlot(mapper, 0);
5450 processPosition(mapper, x1, y1);
5451 processSlot(mapper, 1);
5452 processPosition(mapper, x2, y2);
5453 processSync(mapper);
5454
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5457 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5458 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5460 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5463 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5465 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5466
5467 // First finger up.
5468 x2 += 15; y2 -= 20;
5469 processSlot(mapper, 0);
5470 processId(mapper, -1);
5471 processSlot(mapper, 1);
5472 processPosition(mapper, x2, y2);
5473 processSync(mapper);
5474
5475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5476 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5477 motionArgs.action);
5478 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5479 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5480 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5481 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5484 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5486 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5487
5488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5490 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5491 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5494 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5495
5496 // Move.
5497 x2 += 20; y2 -= 25;
5498 processPosition(mapper, x2, y2);
5499 processSync(mapper);
5500
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5503 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5504 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5507 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5508
5509 // New finger down.
5510 int32_t x3 = 700, y3 = 300;
5511 processPosition(mapper, x2, y2);
5512 processSlot(mapper, 0);
5513 processId(mapper, 3);
5514 processPosition(mapper, x3, y3);
5515 processSync(mapper);
5516
5517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5518 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5519 motionArgs.action);
5520 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5521 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5523 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5526 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5528 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5529
5530 // Second finger up.
5531 x3 += 30; y3 -= 20;
5532 processSlot(mapper, 1);
5533 processId(mapper, -1);
5534 processSlot(mapper, 0);
5535 processPosition(mapper, x3, y3);
5536 processSync(mapper);
5537
5538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5539 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5540 motionArgs.action);
5541 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5542 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5544 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5547 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5549 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5550
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5553 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5554 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5557 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5558
5559 // Last finger up.
5560 processId(mapper, -1);
5561 processSync(mapper);
5562
5563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5564 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5565 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5566 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5569 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5570
5571 // Should not have sent any more keys or motions.
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5574}
5575
5576TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5577 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5578 addConfigurationProperty("touch.deviceType", "touchScreen");
5579 prepareDisplay(DISPLAY_ORIENTATION_0);
5580 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5581 addMapperAndConfigure(mapper);
5582
5583 // These calculations are based on the input device calibration documentation.
5584 int32_t rawX = 100;
5585 int32_t rawY = 200;
5586 int32_t rawTouchMajor = 7;
5587 int32_t rawTouchMinor = 6;
5588 int32_t rawToolMajor = 9;
5589 int32_t rawToolMinor = 8;
5590 int32_t rawPressure = 11;
5591 int32_t rawDistance = 0;
5592 int32_t rawOrientation = 3;
5593 int32_t id = 5;
5594
5595 float x = toDisplayX(rawX);
5596 float y = toDisplayY(rawY);
5597 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5598 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5599 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5600 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5601 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5602 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5603 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5604 float distance = float(rawDistance);
5605
5606 processPosition(mapper, rawX, rawY);
5607 processTouchMajor(mapper, rawTouchMajor);
5608 processTouchMinor(mapper, rawTouchMinor);
5609 processToolMajor(mapper, rawToolMajor);
5610 processToolMinor(mapper, rawToolMinor);
5611 processPressure(mapper, rawPressure);
5612 processOrientation(mapper, rawOrientation);
5613 processDistance(mapper, rawDistance);
5614 processId(mapper, id);
5615 processMTSync(mapper);
5616 processSync(mapper);
5617
5618 NotifyMotionArgs args;
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5620 ASSERT_EQ(0, args.pointerProperties[0].id);
5621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5622 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5623 orientation, distance));
5624}
5625
5626TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5627 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5628 addConfigurationProperty("touch.deviceType", "touchScreen");
5629 prepareDisplay(DISPLAY_ORIENTATION_0);
5630 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5631 addConfigurationProperty("touch.size.calibration", "geometric");
5632 addMapperAndConfigure(mapper);
5633
5634 // These calculations are based on the input device calibration documentation.
5635 int32_t rawX = 100;
5636 int32_t rawY = 200;
5637 int32_t rawTouchMajor = 140;
5638 int32_t rawTouchMinor = 120;
5639 int32_t rawToolMajor = 180;
5640 int32_t rawToolMinor = 160;
5641
5642 float x = toDisplayX(rawX);
5643 float y = toDisplayY(rawY);
5644 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5645 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5646 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5647 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5648 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5649
5650 processPosition(mapper, rawX, rawY);
5651 processTouchMajor(mapper, rawTouchMajor);
5652 processTouchMinor(mapper, rawTouchMinor);
5653 processToolMajor(mapper, rawToolMajor);
5654 processToolMinor(mapper, rawToolMinor);
5655 processMTSync(mapper);
5656 processSync(mapper);
5657
5658 NotifyMotionArgs args;
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5661 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5662}
5663
5664TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5665 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5666 addConfigurationProperty("touch.deviceType", "touchScreen");
5667 prepareDisplay(DISPLAY_ORIENTATION_0);
5668 prepareAxes(POSITION | TOUCH | TOOL);
5669 addConfigurationProperty("touch.size.calibration", "diameter");
5670 addConfigurationProperty("touch.size.scale", "10");
5671 addConfigurationProperty("touch.size.bias", "160");
5672 addConfigurationProperty("touch.size.isSummed", "1");
5673 addMapperAndConfigure(mapper);
5674
5675 // These calculations are based on the input device calibration documentation.
5676 // Note: We only provide a single common touch/tool value because the device is assumed
5677 // not to emit separate values for each pointer (isSummed = 1).
5678 int32_t rawX = 100;
5679 int32_t rawY = 200;
5680 int32_t rawX2 = 150;
5681 int32_t rawY2 = 250;
5682 int32_t rawTouchMajor = 5;
5683 int32_t rawToolMajor = 8;
5684
5685 float x = toDisplayX(rawX);
5686 float y = toDisplayY(rawY);
5687 float x2 = toDisplayX(rawX2);
5688 float y2 = toDisplayY(rawY2);
5689 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5690 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5691 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5692
5693 processPosition(mapper, rawX, rawY);
5694 processTouchMajor(mapper, rawTouchMajor);
5695 processToolMajor(mapper, rawToolMajor);
5696 processMTSync(mapper);
5697 processPosition(mapper, rawX2, rawY2);
5698 processTouchMajor(mapper, rawTouchMajor);
5699 processToolMajor(mapper, rawToolMajor);
5700 processMTSync(mapper);
5701 processSync(mapper);
5702
5703 NotifyMotionArgs args;
5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5705 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5706
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5708 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5709 args.action);
5710 ASSERT_EQ(size_t(2), args.pointerCount);
5711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5712 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5714 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5715}
5716
5717TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5718 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5719 addConfigurationProperty("touch.deviceType", "touchScreen");
5720 prepareDisplay(DISPLAY_ORIENTATION_0);
5721 prepareAxes(POSITION | TOUCH | TOOL);
5722 addConfigurationProperty("touch.size.calibration", "area");
5723 addConfigurationProperty("touch.size.scale", "43");
5724 addConfigurationProperty("touch.size.bias", "3");
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 = 5;
5731 int32_t rawToolMajor = 8;
5732
5733 float x = toDisplayX(rawX);
5734 float y = toDisplayY(rawY);
5735 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5736 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5737 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5738
5739 processPosition(mapper, rawX, rawY);
5740 processTouchMajor(mapper, rawTouchMajor);
5741 processToolMajor(mapper, rawToolMajor);
5742 processMTSync(mapper);
5743 processSync(mapper);
5744
5745 NotifyMotionArgs args;
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5748 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5749}
5750
5751TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5752 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5753 addConfigurationProperty("touch.deviceType", "touchScreen");
5754 prepareDisplay(DISPLAY_ORIENTATION_0);
5755 prepareAxes(POSITION | PRESSURE);
5756 addConfigurationProperty("touch.pressure.calibration", "amplitude");
5757 addConfigurationProperty("touch.pressure.scale", "0.01");
5758 addMapperAndConfigure(mapper);
5759
Michael Wrightaa449c92017-12-13 21:21:43 +00005760 InputDeviceInfo info;
5761 mapper->populateDeviceInfo(&info);
5762 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5763 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5764 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5765
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766 // These calculations are based on the input device calibration documentation.
5767 int32_t rawX = 100;
5768 int32_t rawY = 200;
5769 int32_t rawPressure = 60;
5770
5771 float x = toDisplayX(rawX);
5772 float y = toDisplayY(rawY);
5773 float pressure = float(rawPressure) * 0.01f;
5774
5775 processPosition(mapper, rawX, rawY);
5776 processPressure(mapper, rawPressure);
5777 processMTSync(mapper);
5778 processSync(mapper);
5779
5780 NotifyMotionArgs args;
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5783 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5784}
5785
5786TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5787 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5788 addConfigurationProperty("touch.deviceType", "touchScreen");
5789 prepareDisplay(DISPLAY_ORIENTATION_0);
5790 prepareAxes(POSITION | ID | SLOT);
5791 addMapperAndConfigure(mapper);
5792
5793 NotifyMotionArgs motionArgs;
5794 NotifyKeyArgs keyArgs;
5795
5796 processId(mapper, 1);
5797 processPosition(mapper, 100, 200);
5798 processSync(mapper);
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5801 ASSERT_EQ(0, motionArgs.buttonState);
5802
5803 // press BTN_LEFT, release BTN_LEFT
5804 processKey(mapper, BTN_LEFT, 1);
5805 processSync(mapper);
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5808 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5809
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5812 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5813
Michael Wrightd02c5b62014-02-10 15:10:22 -08005814 processKey(mapper, BTN_LEFT, 0);
5815 processSync(mapper);
5816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005817 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005819
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005822 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005823
5824 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5825 processKey(mapper, BTN_RIGHT, 1);
5826 processKey(mapper, BTN_MIDDLE, 1);
5827 processSync(mapper);
5828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5830 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5831 motionArgs.buttonState);
5832
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5835 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5836
5837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5838 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5839 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5840 motionArgs.buttonState);
5841
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842 processKey(mapper, BTN_RIGHT, 0);
5843 processSync(mapper);
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005845 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005846 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005847
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005850 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851
5852 processKey(mapper, BTN_MIDDLE, 0);
5853 processSync(mapper);
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005855 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005857
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005860 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861
5862 // press BTN_BACK, release BTN_BACK
5863 processKey(mapper, BTN_BACK, 1);
5864 processSync(mapper);
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5866 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5867 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005868
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005871 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5872
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5874 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5875 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005876
5877 processKey(mapper, BTN_BACK, 0);
5878 processSync(mapper);
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005880 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005881 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005882
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005885 ASSERT_EQ(0, motionArgs.buttonState);
5886
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5888 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5889 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5890
5891 // press BTN_SIDE, release BTN_SIDE
5892 processKey(mapper, BTN_SIDE, 1);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5895 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5896 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005897
Michael Wrightd02c5b62014-02-10 15:10:22 -08005898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005900 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5901
5902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5903 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5904 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905
5906 processKey(mapper, BTN_SIDE, 0);
5907 processSync(mapper);
5908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005909 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005911
5912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005914 ASSERT_EQ(0, motionArgs.buttonState);
5915
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5917 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5918 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5919
5920 // press BTN_FORWARD, release BTN_FORWARD
5921 processKey(mapper, BTN_FORWARD, 1);
5922 processSync(mapper);
5923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5924 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5925 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005926
Michael Wrightd02c5b62014-02-10 15:10:22 -08005927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005929 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5930
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5932 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5933 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934
5935 processKey(mapper, BTN_FORWARD, 0);
5936 processSync(mapper);
5937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005938 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005939 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005940
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005943 ASSERT_EQ(0, motionArgs.buttonState);
5944
Michael Wrightd02c5b62014-02-10 15:10:22 -08005945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5946 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5947 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5948
5949 // press BTN_EXTRA, release BTN_EXTRA
5950 processKey(mapper, BTN_EXTRA, 1);
5951 processSync(mapper);
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5953 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5954 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005955
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005958 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5959
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5962 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005963
5964 processKey(mapper, BTN_EXTRA, 0);
5965 processSync(mapper);
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005967 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005969
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005972 ASSERT_EQ(0, motionArgs.buttonState);
5973
Michael Wrightd02c5b62014-02-10 15:10:22 -08005974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5975 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5976 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5977
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5979
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 // press BTN_STYLUS, release BTN_STYLUS
5981 processKey(mapper, BTN_STYLUS, 1);
5982 processSync(mapper);
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005985 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5986
5987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5989 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005990
5991 processKey(mapper, BTN_STYLUS, 0);
5992 processSync(mapper);
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005994 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005996
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005999 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000
6001 // press BTN_STYLUS2, release BTN_STYLUS2
6002 processKey(mapper, BTN_STYLUS2, 1);
6003 processSync(mapper);
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006006 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6007
6008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6009 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6010 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006011
6012 processKey(mapper, BTN_STYLUS2, 0);
6013 processSync(mapper);
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006015 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006016 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006017
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006020 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006021
6022 // release touch
6023 processId(mapper, -1);
6024 processSync(mapper);
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6027 ASSERT_EQ(0, motionArgs.buttonState);
6028}
6029
6030TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
6031 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6032 addConfigurationProperty("touch.deviceType", "touchScreen");
6033 prepareDisplay(DISPLAY_ORIENTATION_0);
6034 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6035 addMapperAndConfigure(mapper);
6036
6037 NotifyMotionArgs motionArgs;
6038
6039 // default tool type is finger
6040 processId(mapper, 1);
6041 processPosition(mapper, 100, 200);
6042 processSync(mapper);
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6044 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6046
6047 // eraser
6048 processKey(mapper, BTN_TOOL_RUBBER, 1);
6049 processSync(mapper);
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6053
6054 // stylus
6055 processKey(mapper, BTN_TOOL_RUBBER, 0);
6056 processKey(mapper, BTN_TOOL_PEN, 1);
6057 processSync(mapper);
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6061
6062 // brush
6063 processKey(mapper, BTN_TOOL_PEN, 0);
6064 processKey(mapper, BTN_TOOL_BRUSH, 1);
6065 processSync(mapper);
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6069
6070 // pencil
6071 processKey(mapper, BTN_TOOL_BRUSH, 0);
6072 processKey(mapper, BTN_TOOL_PENCIL, 1);
6073 processSync(mapper);
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6077
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006078 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079 processKey(mapper, BTN_TOOL_PENCIL, 0);
6080 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6081 processSync(mapper);
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6085
6086 // mouse
6087 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6088 processKey(mapper, BTN_TOOL_MOUSE, 1);
6089 processSync(mapper);
6090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6093
6094 // lens
6095 processKey(mapper, BTN_TOOL_MOUSE, 0);
6096 processKey(mapper, BTN_TOOL_LENS, 1);
6097 processSync(mapper);
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6101
6102 // double-tap
6103 processKey(mapper, BTN_TOOL_LENS, 0);
6104 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6105 processSync(mapper);
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6109
6110 // triple-tap
6111 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6112 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6113 processSync(mapper);
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6117
6118 // quad-tap
6119 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6120 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6121 processSync(mapper);
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6125
6126 // finger
6127 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6128 processKey(mapper, BTN_TOOL_FINGER, 1);
6129 processSync(mapper);
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6133
6134 // stylus trumps finger
6135 processKey(mapper, BTN_TOOL_PEN, 1);
6136 processSync(mapper);
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6140
6141 // eraser trumps stylus
6142 processKey(mapper, BTN_TOOL_RUBBER, 1);
6143 processSync(mapper);
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6147
6148 // mouse trumps eraser
6149 processKey(mapper, BTN_TOOL_MOUSE, 1);
6150 processSync(mapper);
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6154
6155 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6156 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6157 processSync(mapper);
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6160 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6161
6162 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6163 processToolType(mapper, MT_TOOL_PEN);
6164 processSync(mapper);
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6168
6169 // back to default tool type
6170 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6171 processKey(mapper, BTN_TOOL_MOUSE, 0);
6172 processKey(mapper, BTN_TOOL_RUBBER, 0);
6173 processKey(mapper, BTN_TOOL_PEN, 0);
6174 processKey(mapper, BTN_TOOL_FINGER, 0);
6175 processSync(mapper);
6176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6179}
6180
6181TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6182 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6183 addConfigurationProperty("touch.deviceType", "touchScreen");
6184 prepareDisplay(DISPLAY_ORIENTATION_0);
6185 prepareAxes(POSITION | ID | SLOT);
6186 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6187 addMapperAndConfigure(mapper);
6188
6189 NotifyMotionArgs motionArgs;
6190
6191 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6192 processId(mapper, 1);
6193 processPosition(mapper, 100, 200);
6194 processSync(mapper);
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6196 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6198 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6199
6200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6201 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6203 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6204
6205 // move a little
6206 processPosition(mapper, 150, 250);
6207 processSync(mapper);
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6209 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6211 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6212
6213 // down when BTN_TOUCH is pressed, pressure defaults to 1
6214 processKey(mapper, BTN_TOUCH, 1);
6215 processSync(mapper);
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6217 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6219 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6220
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6222 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6224 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6225
6226 // up when BTN_TOUCH is released, hover restored
6227 processKey(mapper, BTN_TOUCH, 0);
6228 processSync(mapper);
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6230 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6232 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6233
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6235 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6237 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6238
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6242 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6243
6244 // exit hover when pointer goes away
6245 processId(mapper, -1);
6246 processSync(mapper);
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6250 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6251}
6252
6253TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6254 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6255 addConfigurationProperty("touch.deviceType", "touchScreen");
6256 prepareDisplay(DISPLAY_ORIENTATION_0);
6257 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6258 addMapperAndConfigure(mapper);
6259
6260 NotifyMotionArgs motionArgs;
6261
6262 // initially hovering because pressure is 0
6263 processId(mapper, 1);
6264 processPosition(mapper, 100, 200);
6265 processPressure(mapper, 0);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6270 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6271
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6275 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6276
6277 // move a little
6278 processPosition(mapper, 150, 250);
6279 processSync(mapper);
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6283 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6284
6285 // down when pressure becomes non-zero
6286 processPressure(mapper, RAW_PRESSURE_MAX);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6291 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6292
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6296 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6297
6298 // up when pressure becomes 0, hover restored
6299 processPressure(mapper, 0);
6300 processSync(mapper);
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6302 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6304 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6305
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6309 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6310
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6312 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6314 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6315
6316 // exit hover when pointer goes away
6317 processId(mapper, -1);
6318 processSync(mapper);
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6320 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6322 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6323}
6324
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006325/**
6326 * Set the input device port <--> display port associations, and check that the
6327 * events are routed to the display that matches the display port.
6328 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6329 */
6330TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6331 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6332 const std::string usb2 = "USB2";
6333 const uint8_t hdmi1 = 0;
6334 const uint8_t hdmi2 = 1;
6335 const std::string secondaryUniqueId = "uniqueId2";
6336 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6337
6338 addConfigurationProperty("touch.deviceType", "touchScreen");
6339 prepareAxes(POSITION);
6340 addMapperAndConfigure(mapper);
6341
6342 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6343 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6344
6345 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6346 // for this input device is specified, and the matching viewport is not present,
6347 // the input device should be disabled (at the mapper level).
6348
6349 // Add viewport for display 2 on hdmi2
6350 prepareSecondaryDisplay(type, hdmi2);
6351 // Send a touch event
6352 processPosition(mapper, 100, 100);
6353 processSync(mapper);
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6355
6356 // Add viewport for display 1 on hdmi1
6357 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6358 // Send a touch event again
6359 processPosition(mapper, 100, 100);
6360 processSync(mapper);
6361
6362 NotifyMotionArgs args;
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6364 ASSERT_EQ(DISPLAY_ID, args.displayId);
6365}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366
Arthur Hung41a712e2018-11-22 19:41:03 +08006367/**
6368 * Expect fallback to internal viewport if device is external and external viewport is not present.
6369 */
6370TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6371 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6372 prepareAxes(POSITION);
6373 addConfigurationProperty("touch.deviceType", "touchScreen");
6374 prepareDisplay(DISPLAY_ORIENTATION_0);
6375 mDevice->setExternal(true);
6376 addMapperAndConfigure(mapper);
6377
6378 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6379
6380 NotifyMotionArgs motionArgs;
6381
6382 // Expect the event to be sent to the internal viewport,
6383 // because an external viewport is not present.
6384 processPosition(mapper, 100, 100);
6385 processSync(mapper);
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6388
6389 // Expect the event to be sent to the external viewport if it is present.
6390 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6391 processPosition(mapper, 100, 100);
6392 processSync(mapper);
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6395}
6396
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006397TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6398 // Setup PointerController for second display.
6399 sp<FakePointerController> fakePointerController = new FakePointerController();
6400 fakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
6401 fakePointerController->setPosition(100, 200);
6402 fakePointerController->setButtonState(0);
6403 fakePointerController->setDisplayId(SECONDARY_DISPLAY_ID);
6404 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6405
6406 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6407 prepareDisplay(DISPLAY_ORIENTATION_0);
6408 prepareAxes(POSITION);
6409 addMapperAndConfigure(mapper);
6410
6411 // Check source is mouse that would obtain the PointerController.
6412 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6413
6414 NotifyMotionArgs motionArgs;
6415 processPosition(mapper, 100, 100);
6416 processSync(mapper);
6417
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6420 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6421}
6422
Arthur Hung7c645402019-01-25 17:45:42 +08006423TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6424 // Setup the first touch screen device.
6425 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6426 prepareAxes(POSITION | ID | SLOT);
6427 addConfigurationProperty("touch.deviceType", "touchScreen");
6428 addMapperAndConfigure(mapper);
6429
6430 // Create the second touch screen device, and enable multi fingers.
6431 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006432 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006433 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006434 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006435 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006436 std::unique_ptr<InputDevice> device2 =
6437 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6438 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006439 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6440 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6441 0 /*flat*/, 0 /*fuzz*/);
6442 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6443 0 /*flat*/, 0 /*fuzz*/);
6444 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6445 0 /*flat*/, 0 /*fuzz*/);
6446 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6447 0 /*flat*/, 0 /*fuzz*/);
6448 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6449 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6450 String8("touchScreen"));
6451
6452 // Setup the second touch screen device.
Arthur Hung2c9a3342019-07-23 14:18:59 +08006453 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2.get());
Arthur Hung7c645402019-01-25 17:45:42 +08006454 device2->addMapper(mapper2);
6455 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6456 device2->reset(ARBITRARY_TIME);
6457
6458 // Setup PointerController.
6459 sp<FakePointerController> fakePointerController = new FakePointerController();
6460 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6461 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6462
6463 // Setup policy for associated displays and show touches.
6464 const uint8_t hdmi1 = 0;
6465 const uint8_t hdmi2 = 1;
6466 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6467 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6468 mFakePolicy->setShowTouches(true);
6469
6470 // Create displays.
6471 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6472 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6473
6474 // Default device will reconfigure above, need additional reconfiguration for another device.
6475 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6476 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6477
6478 // Two fingers down at default display.
6479 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6480 processPosition(mapper, x1, y1);
6481 processId(mapper, 1);
6482 processSlot(mapper, 1);
6483 processPosition(mapper, x2, y2);
6484 processId(mapper, 2);
6485 processSync(mapper);
6486
6487 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6488 fakePointerController->getSpots().find(DISPLAY_ID);
6489 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6490 ASSERT_EQ(size_t(2), iter->second.size());
6491
6492 // Two fingers down at second display.
6493 processPosition(mapper2, x1, y1);
6494 processId(mapper2, 1);
6495 processSlot(mapper2, 1);
6496 processPosition(mapper2, x2, y2);
6497 processId(mapper2, 2);
6498 processSync(mapper2);
6499
6500 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6501 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6502 ASSERT_EQ(size_t(2), iter->second.size());
6503}
6504
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006505TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6506 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6507 prepareAxes(POSITION);
6508 addConfigurationProperty("touch.deviceType", "touchScreen");
6509 prepareDisplay(DISPLAY_ORIENTATION_0);
6510 addMapperAndConfigure(mapper);
6511
6512 NotifyMotionArgs motionArgs;
6513 // Unrotated video frame
6514 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6515 std::vector<TouchVideoFrame> frames{frame};
6516 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6517 processPosition(mapper, 100, 200);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(frames, motionArgs.videoFrames);
6521
6522 // Subsequent touch events should not have any videoframes
6523 // This is implemented separately in FakeEventHub,
6524 // but that should match the behaviour of TouchVideoDevice.
6525 processPosition(mapper, 200, 200);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6529}
6530
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006531TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6532 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6533 prepareAxes(POSITION);
6534 addConfigurationProperty("touch.deviceType", "touchScreen");
6535 addMapperAndConfigure(mapper);
6536 // Unrotated video frame
6537 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6538 NotifyMotionArgs motionArgs;
6539
6540 // Test all 4 orientations
6541 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6542 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6543 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6544 clearViewports();
6545 prepareDisplay(orientation);
6546 std::vector<TouchVideoFrame> frames{frame};
6547 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6548 processPosition(mapper, 100, 200);
6549 processSync(mapper);
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6551 frames[0].rotate(orientation);
6552 ASSERT_EQ(frames, motionArgs.videoFrames);
6553 }
6554}
6555
6556TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6557 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6558 prepareAxes(POSITION);
6559 addConfigurationProperty("touch.deviceType", "touchScreen");
6560 addMapperAndConfigure(mapper);
6561 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6562 // so mix these.
6563 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6564 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6565 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6566 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6567 NotifyMotionArgs motionArgs;
6568
6569 prepareDisplay(DISPLAY_ORIENTATION_90);
6570 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6571 processPosition(mapper, 100, 200);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 std::for_each(frames.begin(), frames.end(),
6575 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6576 ASSERT_EQ(frames, motionArgs.videoFrames);
6577}
6578
Arthur Hung9da14732019-09-02 16:16:58 +08006579/**
6580 * If we had defined port associations, but the viewport is not ready, the touch device would be
6581 * expected to be disabled, and it should be enabled after the viewport has found.
6582 */
6583TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
6584 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6585 constexpr uint8_t hdmi2 = 1;
6586 const std::string secondaryUniqueId = "uniqueId2";
6587 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6588
6589 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6590
6591 addConfigurationProperty("touch.deviceType", "touchScreen");
6592 prepareAxes(POSITION);
6593 addMapperAndConfigure(mapper);
6594
6595 ASSERT_EQ(mDevice->isEnabled(), false);
6596
6597 // Add display on hdmi2, the device should be enabled and can receive touch event.
6598 prepareSecondaryDisplay(type, hdmi2);
6599 ASSERT_EQ(mDevice->isEnabled(), true);
6600
6601 // Send a touch event.
6602 processPosition(mapper, 100, 100);
6603 processSync(mapper);
6604
6605 NotifyMotionArgs args;
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6607 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6608}
6609
Arthur Hung6cd19a42019-08-30 19:04:12 +08006610/**
6611 * Test touch should not work if outside of surface.
6612 */
6613TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
6614 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6615 addConfigurationProperty("touch.deviceType", "touchScreen");
6616 prepareDisplay(DISPLAY_ORIENTATION_0);
6617 // Let surface be different from physical display.
6618 std::optional<DisplayViewport> internalViewport =
6619 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6620 internalViewport->logicalLeft = internalViewport->physicalTop + 20;
6621 internalViewport->logicalTop = internalViewport->physicalRight + 20;
6622 internalViewport->logicalRight = internalViewport->physicalRight - 20;
6623 internalViewport->logicalBottom = internalViewport->physicalBottom - 20;
6624 mFakePolicy->updateViewport(internalViewport.value());
6625
6626 prepareAxes(POSITION);
6627 addMapperAndConfigure(mapper);
6628
6629 int32_t rawX = 10;
6630 int32_t rawY = 10;
6631 processPosition(mapper, rawX, rawY);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6634}
6635
Michael Wrightd02c5b62014-02-10 15:10:22 -08006636} // namespace android