blob: 1da1829d5ca368d9064374105ad41d9ed7df734b [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>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080021#include <InputReaderBase.h>
22#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070023#include <KeyboardInputMapper.h>
24#include <MultiTouchInputMapper.h>
25#include <SingleTouchInputMapper.h>
26#include <SwitchInputMapper.h>
27#include <TestInputListener.h>
28#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080029#include <UinputDevice.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070031#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080033#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <math.h>
35
36namespace android {
37
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038using std::chrono_literals::operator""ms;
39
40// Timeout for waiting for an expected event
41static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
42
Michael Wrightd02c5b62014-02-10 15:10:22 -080043// An arbitrary time value.
44static const nsecs_t ARBITRARY_TIME = 1234;
45
46// Arbitrary display properties.
47static const int32_t DISPLAY_ID = 0;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070048static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -080049static const int32_t DISPLAY_WIDTH = 480;
50static const int32_t DISPLAY_HEIGHT = 800;
Santos Cordonfa5cf462017-04-05 10:37:00 -070051static const int32_t VIRTUAL_DISPLAY_ID = 1;
52static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
53static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070054static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070055static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Error tolerance for floating point assertions.
58static const float EPSILON = 0.001f;
59
60template<typename T>
61static inline T min(T a, T b) {
62 return a < b ? a : b;
63}
64
65static inline float avg(float x, float y) {
66 return (x + y) / 2;
67}
68
69
70// --- FakePointerController ---
71
72class FakePointerController : public PointerControllerInterface {
73 bool mHaveBounds;
74 float mMinX, mMinY, mMaxX, mMaxY;
75 float mX, mY;
76 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080077 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
79protected:
80 virtual ~FakePointerController() { }
81
82public:
83 FakePointerController() :
84 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080085 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080086 }
87
88 void setBounds(float minX, float minY, float maxX, float maxY) {
89 mHaveBounds = true;
90 mMinX = minX;
91 mMinY = minY;
92 mMaxX = maxX;
93 mMaxY = maxY;
94 }
95
96 virtual void setPosition(float x, float y) {
97 mX = x;
98 mY = y;
99 }
100
101 virtual void setButtonState(int32_t buttonState) {
102 mButtonState = buttonState;
103 }
104
105 virtual int32_t getButtonState() const {
106 return mButtonState;
107 }
108
109 virtual void getPosition(float* outX, float* outY) const {
110 *outX = mX;
111 *outY = mY;
112 }
113
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800114 virtual int32_t getDisplayId() const {
115 return mDisplayId;
116 }
117
Garfield Tan888a6a42020-01-09 11:39:16 -0800118 virtual void setDisplayViewport(const DisplayViewport& viewport) {
119 mDisplayId = viewport.displayId;
120 }
121
Arthur Hung7c645402019-01-25 17:45:42 +0800122 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
123 return mSpotsByDisplay;
124 }
125
Michael Wrightd02c5b62014-02-10 15:10:22 -0800126private:
127 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
128 *outMinX = mMinX;
129 *outMinY = mMinY;
130 *outMaxX = mMaxX;
131 *outMaxY = mMaxY;
132 return mHaveBounds;
133 }
134
135 virtual void move(float deltaX, float deltaY) {
136 mX += deltaX;
137 if (mX < mMinX) mX = mMinX;
138 if (mX > mMaxX) mX = mMaxX;
139 mY += deltaY;
140 if (mY < mMinY) mY = mMinY;
141 if (mY > mMaxY) mY = mMaxY;
142 }
143
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100144 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 }
146
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100147 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148 }
149
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100150 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151 }
152
Arthur Hung7c645402019-01-25 17:45:42 +0800153 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
154 int32_t displayId) {
155 std::vector<int32_t> newSpots;
156 // Add spots for fingers that are down.
157 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
158 uint32_t id = idBits.clearFirstMarkedBit();
159 newSpots.push_back(id);
160 }
161
162 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 }
164
165 virtual void clearSpots() {
166 }
Arthur Hung7c645402019-01-25 17:45:42 +0800167
168 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169};
170
171
172// --- FakeInputReaderPolicy ---
173
174class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700175 std::mutex mLock;
176 std::condition_variable mDevicesChangedCondition;
177
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 InputReaderConfiguration mConfig;
179 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700180 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
181 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100182 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700183 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
185protected:
186 virtual ~FakeInputReaderPolicy() { }
187
188public:
189 FakeInputReaderPolicy() {
190 }
191
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700192 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800193 waitForInputDevices([](bool devicesChanged) {
194 if (!devicesChanged) {
195 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
196 }
197 });
198 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700199
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800200 void assertInputDevicesNotChanged() {
201 waitForInputDevices([](bool devicesChanged) {
202 if (devicesChanged) {
203 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
204 }
205 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700206 }
207
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700208 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100209 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100210 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700211 }
212
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700213 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
214 return mConfig.getDisplayViewportByUniqueId(uniqueId);
215 }
216 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
217 return mConfig.getDisplayViewportByType(type);
218 }
219
220 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
221 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700222 }
223
224 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700225 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
226 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700227 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700228 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700229 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100230 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 }
232
Arthur Hung6cd19a42019-08-30 19:04:12 +0800233 bool updateViewport(const DisplayViewport& viewport) {
234 size_t count = mViewports.size();
235 for (size_t i = 0; i < count; i++) {
236 const DisplayViewport& currentViewport = mViewports[i];
237 if (currentViewport.displayId == viewport.displayId) {
238 mViewports[i] = viewport;
239 mConfig.setDisplayViewports(mViewports);
240 return true;
241 }
242 }
243 // no viewport found.
244 return false;
245 }
246
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100247 void addExcludedDeviceName(const std::string& deviceName) {
248 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 }
250
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700251 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
252 mConfig.portAssociations.insert({inputPort, displayPort});
253 }
254
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000255 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700256
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000257 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
260 mPointerControllers.add(deviceId, controller);
261 }
262
263 const InputReaderConfiguration* getReaderConfiguration() const {
264 return &mConfig;
265 }
266
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800267 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 return mInputDevices;
269 }
270
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100271 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700272 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700273 return transform;
274 }
275
276 void setTouchAffineTransformation(const TouchAffineTransformation t) {
277 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800278 }
279
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800280 void setPointerCapture(bool enabled) {
281 mConfig.pointerCapture = enabled;
282 }
283
Arthur Hung7c645402019-01-25 17:45:42 +0800284 void setShowTouches(bool enabled) {
285 mConfig.showTouches = enabled;
286 }
287
Garfield Tan888a6a42020-01-09 11:39:16 -0800288 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
289 mConfig.defaultPointerDisplayId = pointerDisplayId;
290 }
291
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700293 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700294 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
295 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700296 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
297 || orientation == DISPLAY_ORIENTATION_270);
298 DisplayViewport v;
299 v.displayId = displayId;
300 v.orientation = orientation;
301 v.logicalLeft = 0;
302 v.logicalTop = 0;
303 v.logicalRight = isRotated ? height : width;
304 v.logicalBottom = isRotated ? width : height;
305 v.physicalLeft = 0;
306 v.physicalTop = 0;
307 v.physicalRight = isRotated ? height : width;
308 v.physicalBottom = isRotated ? width : height;
309 v.deviceWidth = isRotated ? height : width;
310 v.deviceHeight = isRotated ? width : height;
311 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700312 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100313 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700314 return v;
315 }
316
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
318 *outConfig = mConfig;
319 }
320
321 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
322 return mPointerControllers.valueFor(deviceId);
323 }
324
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800325 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700326 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700328 mInputDevicesChanged = true;
329 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 }
331
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100332 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700333 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100336 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
337 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800339
340 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
341 std::unique_lock<std::mutex> lock(mLock);
342 base::ScopedLockAssertion assumeLocked(mLock);
343
344 const bool devicesChanged =
345 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
346 return mInputDevicesChanged;
347 });
348 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
349 mInputDevicesChanged = false;
350 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351};
352
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353// --- FakeEventHub ---
354
355class FakeEventHub : public EventHubInterface {
356 struct KeyInfo {
357 int32_t keyCode;
358 uint32_t flags;
359 };
360
361 struct Device {
362 InputDeviceIdentifier identifier;
363 uint32_t classes;
364 PropertyMap configuration;
365 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
366 KeyedVector<int, bool> relativeAxes;
367 KeyedVector<int32_t, int32_t> keyCodeStates;
368 KeyedVector<int32_t, int32_t> scanCodeStates;
369 KeyedVector<int32_t, int32_t> switchStates;
370 KeyedVector<int32_t, int32_t> absoluteAxisValue;
371 KeyedVector<int32_t, KeyInfo> keysByScanCode;
372 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
373 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800374 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700375 bool enabled;
376
377 status_t enable() {
378 enabled = true;
379 return OK;
380 }
381
382 status_t disable() {
383 enabled = false;
384 return OK;
385 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700387 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700388 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 }
390 };
391
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700392 std::mutex mLock;
393 std::condition_variable mEventsCondition;
394
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100396 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700397 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600398 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700400public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 virtual ~FakeEventHub() {
402 for (size_t i = 0; i < mDevices.size(); i++) {
403 delete mDevices.valueAt(i);
404 }
405 }
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 FakeEventHub() { }
408
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100409 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 Device* device = new Device(classes);
411 device->identifier.name = name;
412 mDevices.add(deviceId, device);
413
414 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
415 }
416
417 void removeDevice(int32_t deviceId) {
418 delete mDevices.valueFor(deviceId);
419 mDevices.removeItem(deviceId);
420
421 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
422 }
423
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700424 bool isDeviceEnabled(int32_t deviceId) {
425 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700426 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700427 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
428 return false;
429 }
430 return device->enabled;
431 }
432
433 status_t enableDevice(int32_t deviceId) {
434 status_t result;
435 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700436 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700437 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
438 return BAD_VALUE;
439 }
440 if (device->enabled) {
441 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
442 return OK;
443 }
444 result = device->enable();
445 return result;
446 }
447
448 status_t disableDevice(int32_t deviceId) {
449 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700450 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700451 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
452 return BAD_VALUE;
453 }
454 if (!device->enabled) {
455 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
456 return OK;
457 }
458 return device->disable();
459 }
460
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 void finishDeviceScan() {
462 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
463 }
464
465 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
466 Device* device = getDevice(deviceId);
467 device->configuration.addProperty(key, value);
468 }
469
470 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
471 Device* device = getDevice(deviceId);
472 device->configuration.addAll(configuration);
473 }
474
475 void addAbsoluteAxis(int32_t deviceId, int axis,
476 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
477 Device* device = getDevice(deviceId);
478
479 RawAbsoluteAxisInfo info;
480 info.valid = true;
481 info.minValue = minValue;
482 info.maxValue = maxValue;
483 info.flat = flat;
484 info.fuzz = fuzz;
485 info.resolution = resolution;
486 device->absoluteAxes.add(axis, info);
487 }
488
489 void addRelativeAxis(int32_t deviceId, int32_t axis) {
490 Device* device = getDevice(deviceId);
491 device->relativeAxes.add(axis, true);
492 }
493
494 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
495 Device* device = getDevice(deviceId);
496 device->keyCodeStates.replaceValueFor(keyCode, state);
497 }
498
499 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
500 Device* device = getDevice(deviceId);
501 device->scanCodeStates.replaceValueFor(scanCode, state);
502 }
503
504 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
505 Device* device = getDevice(deviceId);
506 device->switchStates.replaceValueFor(switchCode, state);
507 }
508
509 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
510 Device* device = getDevice(deviceId);
511 device->absoluteAxisValue.replaceValueFor(axis, value);
512 }
513
514 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
515 int32_t keyCode, uint32_t flags) {
516 Device* device = getDevice(deviceId);
517 KeyInfo info;
518 info.keyCode = keyCode;
519 info.flags = flags;
520 if (scanCode) {
521 device->keysByScanCode.add(scanCode, info);
522 }
523 if (usageCode) {
524 device->keysByUsageCode.add(usageCode, info);
525 }
526 }
527
528 void addLed(int32_t deviceId, int32_t led, bool initialState) {
529 Device* device = getDevice(deviceId);
530 device->leds.add(led, initialState);
531 }
532
533 bool getLedState(int32_t deviceId, int32_t led) {
534 Device* device = getDevice(deviceId);
535 return device->leds.valueFor(led);
536 }
537
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100538 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 return mExcludedDevices;
540 }
541
542 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
543 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800544 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 }
546
547 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
548 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700549 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 RawEvent event;
551 event.when = when;
552 event.deviceId = deviceId;
553 event.type = type;
554 event.code = code;
555 event.value = value;
556 mEvents.push_back(event);
557
558 if (type == EV_ABS) {
559 setAbsoluteAxisValue(deviceId, code, value);
560 }
561 }
562
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600563 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
564 std::vector<TouchVideoFrame>> videoFrames) {
565 mVideoFrames = std::move(videoFrames);
566 }
567
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700569 std::unique_lock<std::mutex> lock(mLock);
570 base::ScopedLockAssertion assumeLocked(mLock);
571 const bool queueIsEmpty =
572 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
573 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
574 if (!queueIsEmpty) {
575 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
576 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
579private:
580 Device* getDevice(int32_t deviceId) const {
581 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100582 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583 }
584
585 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
586 Device* device = getDevice(deviceId);
587 return device ? device->classes : 0;
588 }
589
590 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
591 Device* device = getDevice(deviceId);
592 return device ? device->identifier : InputDeviceIdentifier();
593 }
594
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100595 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 return 0;
597 }
598
599 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
600 Device* device = getDevice(deviceId);
601 if (device) {
602 *outConfiguration = device->configuration;
603 }
604 }
605
606 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
607 RawAbsoluteAxisInfo* outAxisInfo) const {
608 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800609 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 ssize_t index = device->absoluteAxes.indexOfKey(axis);
611 if (index >= 0) {
612 *outAxisInfo = device->absoluteAxes.valueAt(index);
613 return OK;
614 }
615 }
616 outAxisInfo->clear();
617 return -1;
618 }
619
620 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
621 Device* device = getDevice(deviceId);
622 if (device) {
623 return device->relativeAxes.indexOfKey(axis) >= 0;
624 }
625 return false;
626 }
627
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100628 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 return false;
630 }
631
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700632 virtual status_t mapKey(int32_t deviceId,
633 int32_t scanCode, int32_t usageCode, int32_t metaState,
634 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 Device* device = getDevice(deviceId);
636 if (device) {
637 const KeyInfo* key = getKey(device, scanCode, usageCode);
638 if (key) {
639 if (outKeycode) {
640 *outKeycode = key->keyCode;
641 }
642 if (outFlags) {
643 *outFlags = key->flags;
644 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700645 if (outMetaState) {
646 *outMetaState = metaState;
647 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 return OK;
649 }
650 }
651 return NAME_NOT_FOUND;
652 }
653
654 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
655 if (usageCode) {
656 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
657 if (index >= 0) {
658 return &device->keysByUsageCode.valueAt(index);
659 }
660 }
661 if (scanCode) {
662 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
663 if (index >= 0) {
664 return &device->keysByScanCode.valueAt(index);
665 }
666 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700667 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 }
669
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100670 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 return NAME_NOT_FOUND;
672 }
673
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100674 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 mExcludedDevices = devices;
676 }
677
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100678 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700679 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 if (mEvents.empty()) {
681 return 0;
682 }
683
684 *buffer = *mEvents.begin();
685 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700686 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 return 1;
688 }
689
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800690 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600691 auto it = mVideoFrames.find(deviceId);
692 if (it != mVideoFrames.end()) {
693 std::vector<TouchVideoFrame> frames = std::move(it->second);
694 mVideoFrames.erase(deviceId);
695 return frames;
696 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800697 return {};
698 }
699
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
701 Device* device = getDevice(deviceId);
702 if (device) {
703 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
704 if (index >= 0) {
705 return device->scanCodeStates.valueAt(index);
706 }
707 }
708 return AKEY_STATE_UNKNOWN;
709 }
710
711 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
712 Device* device = getDevice(deviceId);
713 if (device) {
714 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
715 if (index >= 0) {
716 return device->keyCodeStates.valueAt(index);
717 }
718 }
719 return AKEY_STATE_UNKNOWN;
720 }
721
722 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
723 Device* device = getDevice(deviceId);
724 if (device) {
725 ssize_t index = device->switchStates.indexOfKey(sw);
726 if (index >= 0) {
727 return device->switchStates.valueAt(index);
728 }
729 }
730 return AKEY_STATE_UNKNOWN;
731 }
732
733 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
734 int32_t* outValue) const {
735 Device* device = getDevice(deviceId);
736 if (device) {
737 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
738 if (index >= 0) {
739 *outValue = device->absoluteAxisValue.valueAt(index);
740 return OK;
741 }
742 }
743 *outValue = 0;
744 return -1;
745 }
746
747 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
748 uint8_t* outFlags) const {
749 bool result = false;
750 Device* device = getDevice(deviceId);
751 if (device) {
752 for (size_t i = 0; i < numCodes; i++) {
753 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
754 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
755 outFlags[i] = 1;
756 result = true;
757 }
758 }
759 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
760 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
761 outFlags[i] = 1;
762 result = true;
763 }
764 }
765 }
766 }
767 return result;
768 }
769
770 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
771 Device* device = getDevice(deviceId);
772 if (device) {
773 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
774 return index >= 0;
775 }
776 return false;
777 }
778
779 virtual bool hasLed(int32_t deviceId, int32_t led) const {
780 Device* device = getDevice(deviceId);
781 return device && device->leds.indexOfKey(led) >= 0;
782 }
783
784 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
785 Device* device = getDevice(deviceId);
786 if (device) {
787 ssize_t index = device->leds.indexOfKey(led);
788 if (index >= 0) {
789 device->leds.replaceValueAt(led, on);
790 } else {
791 ADD_FAILURE()
792 << "Attempted to set the state of an LED that the EventHub declared "
793 "was not present. led=" << led;
794 }
795 }
796 }
797
798 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800799 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 outVirtualKeys.clear();
801
802 Device* device = getDevice(deviceId);
803 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800804 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 }
806 }
807
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100808 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700809 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 }
811
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100812 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 return false;
814 }
815
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100816 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 }
818
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100819 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 }
821
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100822 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 return false;
824 }
825
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800826 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 }
828
829 virtual void monitor() {
830 }
831
832 virtual void requestReopenDevices() {
833 }
834
835 virtual void wake() {
836 }
837};
838
839
840// --- FakeInputReaderContext ---
841
842class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700843 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 sp<InputReaderPolicyInterface> mPolicy;
845 sp<InputListenerInterface> mListener;
846 int32_t mGlobalMetaState;
847 bool mUpdateGlobalMetaStateWasCalled;
848 int32_t mGeneration;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800849 uint32_t mNextSequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850
851public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700852 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
853 const sp<InputReaderPolicyInterface>& policy,
854 const sp<InputListenerInterface>& listener)
855 : mEventHub(eventHub),
856 mPolicy(policy),
857 mListener(listener),
858 mGlobalMetaState(0),
859 mNextSequenceNum(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
861 virtual ~FakeInputReaderContext() { }
862
863 void assertUpdateGlobalMetaStateWasCalled() {
864 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
865 << "Expected updateGlobalMetaState() to have been called.";
866 mUpdateGlobalMetaStateWasCalled = false;
867 }
868
869 void setGlobalMetaState(int32_t state) {
870 mGlobalMetaState = state;
871 }
872
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800873 uint32_t getGeneration() {
874 return mGeneration;
875 }
876
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877private:
878 virtual void updateGlobalMetaState() {
879 mUpdateGlobalMetaStateWasCalled = true;
880 }
881
882 virtual int32_t getGlobalMetaState() {
883 return mGlobalMetaState;
884 }
885
886 virtual EventHubInterface* getEventHub() {
887 return mEventHub.get();
888 }
889
890 virtual InputReaderPolicyInterface* getPolicy() {
891 return mPolicy.get();
892 }
893
894 virtual InputListenerInterface* getListener() {
895 return mListener.get();
896 }
897
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100898 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899 }
900
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100901 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902 return false;
903 }
904
905 virtual void fadePointer() {
906 }
907
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100908 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 }
910
911 virtual int32_t bumpGeneration() {
912 return ++mGeneration;
913 }
Michael Wright842500e2015-03-13 17:32:02 -0700914
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800915 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700916
917 }
918
919 virtual void dispatchExternalStylusState(const StylusState&) {
920
921 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800922
923 virtual uint32_t getNextSequenceNum() {
924 return mNextSequenceNum++;
925 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926};
927
928
929// --- FakeInputMapper ---
930
931class FakeInputMapper : public InputMapper {
932 uint32_t mSources;
933 int32_t mKeyboardType;
934 int32_t mMetaState;
935 KeyedVector<int32_t, int32_t> mKeyCodeStates;
936 KeyedVector<int32_t, int32_t> mScanCodeStates;
937 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800938 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700940 std::mutex mLock;
941 std::condition_variable mStateChangedCondition;
942 bool mConfigureWasCalled GUARDED_BY(mLock);
943 bool mResetWasCalled GUARDED_BY(mLock);
944 bool mProcessWasCalled GUARDED_BY(mLock);
945 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946
Arthur Hungc23540e2018-11-29 20:42:11 +0800947 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800948public:
949 FakeInputMapper(InputDevice* device, uint32_t sources) :
950 InputMapper(device),
951 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
952 mMetaState(0),
953 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
954 }
955
956 virtual ~FakeInputMapper() { }
957
958 void setKeyboardType(int32_t keyboardType) {
959 mKeyboardType = keyboardType;
960 }
961
962 void setMetaState(int32_t metaState) {
963 mMetaState = metaState;
964 }
965
966 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700967 std::unique_lock<std::mutex> lock(mLock);
968 base::ScopedLockAssertion assumeLocked(mLock);
969 const bool configureCalled =
970 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
971 return mConfigureWasCalled;
972 });
973 if (!configureCalled) {
974 FAIL() << "Expected configure() to have been called.";
975 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976 mConfigureWasCalled = false;
977 }
978
979 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700980 std::unique_lock<std::mutex> lock(mLock);
981 base::ScopedLockAssertion assumeLocked(mLock);
982 const bool resetCalled =
983 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
984 return mResetWasCalled;
985 });
986 if (!resetCalled) {
987 FAIL() << "Expected reset() to have been called.";
988 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989 mResetWasCalled = false;
990 }
991
Yi Kong9b14ac62018-07-17 13:48:38 -0700992 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700993 std::unique_lock<std::mutex> lock(mLock);
994 base::ScopedLockAssertion assumeLocked(mLock);
995 const bool processCalled =
996 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
997 return mProcessWasCalled;
998 });
999 if (!processCalled) {
1000 FAIL() << "Expected process() to have been called.";
1001 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002 if (outLastEvent) {
1003 *outLastEvent = mLastEvent;
1004 }
1005 mProcessWasCalled = false;
1006 }
1007
1008 void setKeyCodeState(int32_t keyCode, int32_t state) {
1009 mKeyCodeStates.replaceValueFor(keyCode, state);
1010 }
1011
1012 void setScanCodeState(int32_t scanCode, int32_t state) {
1013 mScanCodeStates.replaceValueFor(scanCode, state);
1014 }
1015
1016 void setSwitchState(int32_t switchCode, int32_t state) {
1017 mSwitchStates.replaceValueFor(switchCode, state);
1018 }
1019
1020 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001021 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022 }
1023
1024private:
1025 virtual uint32_t getSources() {
1026 return mSources;
1027 }
1028
1029 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1030 InputMapper::populateDeviceInfo(deviceInfo);
1031
1032 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1033 deviceInfo->setKeyboardType(mKeyboardType);
1034 }
1035 }
1036
Arthur Hungc23540e2018-11-29 20:42:11 +08001037 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001038 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001040
1041 // Find the associated viewport if exist.
1042 const std::optional<uint8_t> displayPort = mDevice->getAssociatedDisplayPort();
1043 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1044 mViewport = config->getDisplayViewportByPort(*displayPort);
1045 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001046
1047 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048 }
1049
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001050 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001051 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001053 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001054 }
1055
1056 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001057 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058 mLastEvent = *rawEvent;
1059 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001060 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061 }
1062
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001063 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001064 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1065 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1066 }
1067
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001068 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1070 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1071 }
1072
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001073 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1075 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1076 }
1077
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001078 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079 const int32_t* keyCodes, uint8_t* outFlags) {
1080 bool result = false;
1081 for (size_t i = 0; i < numCodes; i++) {
1082 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1083 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1084 outFlags[i] = 1;
1085 result = true;
1086 }
1087 }
1088 }
1089 return result;
1090 }
1091
1092 virtual int32_t getMetaState() {
1093 return mMetaState;
1094 }
1095
1096 virtual void fadePointer() {
1097 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001098
1099 virtual std::optional<int32_t> getAssociatedDisplay() {
1100 if (mViewport) {
1101 return std::make_optional(mViewport->displayId);
1102 }
1103 return std::nullopt;
1104 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105};
1106
1107
1108// --- InstrumentedInputReader ---
1109
1110class InstrumentedInputReader : public InputReader {
1111 InputDevice* mNextDevice;
1112
1113public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001114 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1115 const sp<InputReaderPolicyInterface>& policy,
1116 const sp<InputListenerInterface>& listener)
1117 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118
1119 virtual ~InstrumentedInputReader() {
1120 if (mNextDevice) {
1121 delete mNextDevice;
1122 }
1123 }
1124
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001125 void setNextDevice(InputDevice* device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001127 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const std::string& name,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001128 uint32_t classes, const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 InputDeviceIdentifier identifier;
1130 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001131 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 int32_t generation = deviceId + 1;
1133 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001134 classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135 }
1136
Prabir Pradhan28efc192019-11-05 01:10:04 +00001137 // Make the protected loopOnce method accessible to tests.
1138 using InputReader::loopOnce;
1139
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140protected:
1141 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001142 const InputDeviceIdentifier& identifier,
1143 uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 if (mNextDevice) {
1145 InputDevice* device = mNextDevice;
Yi Kong9b14ac62018-07-17 13:48:38 -07001146 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147 return device;
1148 }
1149 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1150 }
1151
1152 friend class InputReaderTest;
1153};
1154
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001155// --- InputReaderPolicyTest ---
1156class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001157protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001158 sp<FakeInputReaderPolicy> mFakePolicy;
1159
Prabir Pradhan28efc192019-11-05 01:10:04 +00001160 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1161 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001162};
1163
1164/**
1165 * Check that empty set of viewports is an acceptable configuration.
1166 * Also try to get internal viewport two different ways - by type and by uniqueId.
1167 *
1168 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1169 * Such configuration is not currently allowed.
1170 */
1171TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001172 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001173
1174 // We didn't add any viewports yet, so there shouldn't be any.
1175 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001176 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001177 ASSERT_FALSE(internalViewport);
1178
1179 // Add an internal viewport, then clear it
1180 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001181 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001182
1183 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001184 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001186 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001187
1188 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001189 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001190 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001191 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001192
1193 mFakePolicy->clearViewports();
1194 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001195 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001196 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001197 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001198 ASSERT_FALSE(internalViewport);
1199}
1200
1201TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1202 const std::string internalUniqueId = "local:0";
1203 const std::string externalUniqueId = "local:1";
1204 const std::string virtualUniqueId1 = "virtual:2";
1205 const std::string virtualUniqueId2 = "virtual:3";
1206 constexpr int32_t virtualDisplayId1 = 2;
1207 constexpr int32_t virtualDisplayId2 = 3;
1208
1209 // Add an internal viewport
1210 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001211 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001212 // Add an external viewport
1213 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001214 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001215 // Add an virtual viewport
1216 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001217 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001218 // Add another virtual viewport
1219 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001220 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001221
1222 // Check matching by type for internal
1223 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001224 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001225 ASSERT_TRUE(internalViewport);
1226 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1227
1228 // Check matching by type for external
1229 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001230 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001231 ASSERT_TRUE(externalViewport);
1232 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1233
1234 // Check matching by uniqueId for virtual viewport #1
1235 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001236 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001237 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001238 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001239 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1240 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1241
1242 // Check matching by uniqueId for virtual viewport #2
1243 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001244 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001245 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001246 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001247 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1248 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1249}
1250
1251
1252/**
1253 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1254 * that lookup works by checking display id.
1255 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1256 */
1257TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1258 const std::string uniqueId1 = "uniqueId1";
1259 const std::string uniqueId2 = "uniqueId2";
1260 constexpr int32_t displayId1 = 2;
1261 constexpr int32_t displayId2 = 3;
1262
1263 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1264 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1265 for (const ViewportType& type : types) {
1266 mFakePolicy->clearViewports();
1267 // Add a viewport
1268 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001269 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001270 // Add another viewport
1271 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001272 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001273
1274 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001275 std::optional<DisplayViewport> viewport1 =
1276 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001277 ASSERT_TRUE(viewport1);
1278 ASSERT_EQ(displayId1, viewport1->displayId);
1279 ASSERT_EQ(type, viewport1->type);
1280
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001281 std::optional<DisplayViewport> viewport2 =
1282 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001283 ASSERT_TRUE(viewport2);
1284 ASSERT_EQ(displayId2, viewport2->displayId);
1285 ASSERT_EQ(type, viewport2->type);
1286
1287 // When there are multiple viewports of the same kind, and uniqueId is not specified
1288 // in the call to getDisplayViewport, then that situation is not supported.
1289 // The viewports can be stored in any order, so we cannot rely on the order, since that
1290 // is just implementation detail.
1291 // However, we can check that it still returns *a* viewport, we just cannot assert
1292 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001293 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001294 ASSERT_TRUE(someViewport);
1295 }
1296}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001297
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001298/**
1299 * Check getDisplayViewportByPort
1300 */
1301TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1302 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1303 const std::string uniqueId1 = "uniqueId1";
1304 const std::string uniqueId2 = "uniqueId2";
1305 constexpr int32_t displayId1 = 1;
1306 constexpr int32_t displayId2 = 2;
1307 const uint8_t hdmi1 = 0;
1308 const uint8_t hdmi2 = 1;
1309 const uint8_t hdmi3 = 2;
1310
1311 mFakePolicy->clearViewports();
1312 // Add a viewport that's associated with some display port that's not of interest.
1313 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1314 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1315 // Add another viewport, connected to HDMI1 port
1316 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1317 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1318
1319 // Check that correct display viewport was returned by comparing the display ports.
1320 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1321 ASSERT_TRUE(hdmi1Viewport);
1322 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1323 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1324
1325 // Check that we can still get the same viewport using the uniqueId
1326 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1327 ASSERT_TRUE(hdmi1Viewport);
1328 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1329 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1330 ASSERT_EQ(type, hdmi1Viewport->type);
1331
1332 // Check that we cannot find a port with "HDMI2", because we never added one
1333 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1334 ASSERT_FALSE(hdmi2Viewport);
1335}
1336
Michael Wrightd02c5b62014-02-10 15:10:22 -08001337// --- InputReaderTest ---
1338
1339class InputReaderTest : public testing::Test {
1340protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001341 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001342 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001343 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001344 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001345
Prabir Pradhan28efc192019-11-05 01:10:04 +00001346 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001347 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001348 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001349 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001350
Prabir Pradhan28efc192019-11-05 01:10:04 +00001351 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1352 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001353 }
1354
Prabir Pradhan28efc192019-11-05 01:10:04 +00001355 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001356 mFakeListener.clear();
1357 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001358 }
1359
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001360 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001361 const PropertyMap* configuration) {
1362 mFakeEventHub->addDevice(deviceId, name, classes);
1363
1364 if (configuration) {
1365 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1366 }
1367 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001368 mReader->loopOnce();
1369 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001370 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1371 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001372 }
1373
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001374 void disableDevice(int32_t deviceId, InputDevice* device) {
1375 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001376 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001377 }
1378
1379 void enableDevice(int32_t deviceId, InputDevice* device) {
1380 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001381 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001382 }
1383
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001385 const std::string& name, uint32_t classes, uint32_t sources,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 const PropertyMap* configuration) {
1387 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1388 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1389 device->addMapper(mapper);
1390 mReader->setNextDevice(device);
1391 addDevice(deviceId, name, classes, configuration);
1392 return mapper;
1393 }
1394};
1395
1396TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001397 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001398 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001399 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001400 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001402 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001403 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001404 ASSERT_EQ(1U, inputDevices.size());
1405 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001406 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001407 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1409 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1410
1411 // Should also have received a notification describing the new input devices.
1412 inputDevices = mFakePolicy->getInputDevices();
1413 ASSERT_EQ(1U, inputDevices.size());
1414 ASSERT_EQ(1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001415 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1417 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1418 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1419}
1420
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001421TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1422 constexpr int32_t deviceId = 1;
1423 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001424 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001425 // Must add at least one mapper or the device will be ignored!
1426 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1427 device->addMapper(mapper);
1428 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001429 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001430
Yi Kong9b14ac62018-07-17 13:48:38 -07001431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001432
1433 NotifyDeviceResetArgs resetArgs;
1434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001435 ASSERT_EQ(deviceId, resetArgs.deviceId);
1436
1437 ASSERT_EQ(device->isEnabled(), true);
1438 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001439 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001440
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001442 ASSERT_EQ(deviceId, resetArgs.deviceId);
1443 ASSERT_EQ(device->isEnabled(), false);
1444
1445 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001446 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001449 ASSERT_EQ(device->isEnabled(), false);
1450
1451 enableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001452 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001454 ASSERT_EQ(deviceId, resetArgs.deviceId);
1455 ASSERT_EQ(device->isEnabled(), true);
1456}
1457
Michael Wrightd02c5b62014-02-10 15:10:22 -08001458TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001459 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001460 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001461 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001462 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1463
1464 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1465 AINPUT_SOURCE_ANY, AKEYCODE_A))
1466 << "Should return unknown when the device id is >= 0 but unknown.";
1467
1468 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1469 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1470 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1471
1472 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1473 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1474 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1475
1476 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1477 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1478 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1479
1480 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1481 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1482 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1483}
1484
1485TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001486 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001487 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001488 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001489 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1490
1491 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1492 AINPUT_SOURCE_ANY, KEY_A))
1493 << "Should return unknown when the device id is >= 0 but unknown.";
1494
1495 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1496 AINPUT_SOURCE_TRACKBALL, KEY_A))
1497 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1498
1499 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1500 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1501 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1502
1503 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1504 AINPUT_SOURCE_TRACKBALL, KEY_A))
1505 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1506
1507 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1508 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1509 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1510}
1511
1512TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001513 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001514 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001515 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1517
1518 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1519 AINPUT_SOURCE_ANY, SW_LID))
1520 << "Should return unknown when the device id is >= 0 but unknown.";
1521
1522 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1523 AINPUT_SOURCE_TRACKBALL, SW_LID))
1524 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1525
1526 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1527 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1528 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1529
1530 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1531 AINPUT_SOURCE_TRACKBALL, SW_LID))
1532 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1533
1534 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1535 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1536 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1537}
1538
1539TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001540 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001541 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001542 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001543
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544 mapper->addSupportedKeyCode(AKEYCODE_A);
1545 mapper->addSupportedKeyCode(AKEYCODE_B);
1546
1547 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1548 uint8_t flags[4] = { 0, 0, 0, 1 };
1549
1550 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1551 << "Should return false when device id is >= 0 but unknown.";
1552 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1553
1554 flags[3] = 1;
1555 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1556 << "Should return false when device id is valid but the sources are not supported by the device.";
1557 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1558
1559 flags[3] = 1;
1560 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1561 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1562 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1563
1564 flags[3] = 1;
1565 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1566 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1567 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1568
1569 flags[3] = 1;
1570 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1571 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1572 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1573}
1574
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001575TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001576 addDevice(1, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577
1578 NotifyConfigurationChangedArgs args;
1579
1580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1581 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1582}
1583
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001584TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001585 FakeInputMapper* mapper = nullptr;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001586 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, "fake",
Yi Kong9b14ac62018-07-17 13:48:38 -07001587 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, nullptr));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588
1589 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001590 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1592
1593 RawEvent event;
1594 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1595 ASSERT_EQ(0, event.when);
1596 ASSERT_EQ(1, event.deviceId);
1597 ASSERT_EQ(EV_KEY, event.type);
1598 ASSERT_EQ(KEY_A, event.code);
1599 ASSERT_EQ(1, event.value);
1600}
1601
Prabir Pradhan42611e02018-11-27 14:04:02 -08001602TEST_F(InputReaderTest, DeviceReset_IncrementsSequenceNumber) {
1603 constexpr int32_t deviceId = 1;
1604 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Arthur Hungc23540e2018-11-29 20:42:11 +08001605 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001606 // Must add at least one mapper or the device will be ignored!
1607 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1608 device->addMapper(mapper);
1609 mReader->setNextDevice(device);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001610 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001611
1612 NotifyDeviceResetArgs resetArgs;
1613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1614 uint32_t prevSequenceNum = resetArgs.sequenceNum;
1615
1616 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001617 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001619 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1620 prevSequenceNum = resetArgs.sequenceNum;
1621
1622 enableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001623 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001625 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1626 prevSequenceNum = resetArgs.sequenceNum;
1627
1628 disableDevice(deviceId, device);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001629 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001631 ASSERT_TRUE(prevSequenceNum < resetArgs.sequenceNum);
1632 prevSequenceNum = resetArgs.sequenceNum;
1633}
1634
Arthur Hungc23540e2018-11-29 20:42:11 +08001635TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1636 constexpr int32_t deviceId = 1;
1637 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1638 const char* DEVICE_LOCATION = "USB1";
1639 InputDevice* device = mReader->newDevice(deviceId, 0 /*controllerNumber*/, "fake", deviceClass,
1640 DEVICE_LOCATION);
1641 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_TOUCHSCREEN);
1642 device->addMapper(mapper);
1643 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001644
1645 const uint8_t hdmi1 = 1;
1646
1647 // Associated touch screen with second display.
1648 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1649
1650 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001651 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001652 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1653 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1654 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1655 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1656 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001657 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001658
1659 // Add the device, and make sure all of the callbacks are triggered.
1660 // The device is added after the input port associations are processed since
1661 // we do not yet support dynamic device-to-display associations.
1662 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
1665 ASSERT_NO_FATAL_FAILURE(mapper->assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001666
Arthur Hung2c9a3342019-07-23 14:18:59 +08001667 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001668 ASSERT_EQ(deviceId, device->getId());
1669 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1670 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001671
1672 // Can't dispatch event from a disabled device.
1673 disableDevice(deviceId, device);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001674 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001675 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001676}
1677
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001678// --- InputReaderIntegrationTest ---
1679
1680// These tests create and interact with the InputReader only through its interface.
1681// The InputReader is started during SetUp(), which starts its processing in its own
1682// thread. The tests use linux uinput to emulate input devices.
1683// NOTE: Interacting with the physical device while these tests are running may cause
1684// the tests to fail.
1685class InputReaderIntegrationTest : public testing::Test {
1686protected:
1687 sp<TestInputListener> mTestListener;
1688 sp<FakeInputReaderPolicy> mFakePolicy;
1689 sp<InputReaderInterface> mReader;
1690
1691 virtual void SetUp() override {
1692 mFakePolicy = new FakeInputReaderPolicy();
1693 mTestListener = new TestInputListener();
1694
1695 mReader = createInputReader(mFakePolicy, mTestListener);
1696 ASSERT_EQ(mReader->start(), OK);
1697
1698 // Since this test is run on a real device, all the input devices connected
1699 // to the test device will show up in mReader. We wait for those input devices to
1700 // show up before beginning the tests.
1701 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1702 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1703 }
1704
1705 virtual void TearDown() override {
1706 ASSERT_EQ(mReader->stop(), OK);
1707 mTestListener.clear();
1708 mFakePolicy.clear();
1709 }
1710};
1711
1712TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1713 // An invalid input device that is only used for this test.
1714 class InvalidUinputDevice : public UinputDevice {
1715 public:
1716 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1717
1718 private:
1719 void configureDevice(int fd, uinput_user_dev* device) override {}
1720 };
1721
1722 const size_t numDevices = mFakePolicy->getInputDevices().size();
1723
1724 // UinputDevice does not set any event or key bits, so InputReader should not
1725 // consider it as a valid device.
1726 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1727 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1728 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1729 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1730
1731 invalidDevice.reset();
1732 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1733 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1734 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1735}
1736
1737TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1738 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1739
1740 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1741 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1742 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1743 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1744
1745 // Find the test device by its name.
1746 std::vector<InputDeviceInfo> inputDevices;
1747 mReader->getInputDevices(inputDevices);
1748 InputDeviceInfo* keyboardInfo = nullptr;
1749 const char* keyboardName = keyboard->getName();
1750 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1751 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1752 keyboardInfo = &inputDevices[i];
1753 break;
1754 }
1755 }
1756 ASSERT_NE(keyboardInfo, nullptr);
1757 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1758 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1759 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1760
1761 keyboard.reset();
1762 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1763 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1764 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1765}
1766
1767TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1768 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1769 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1770
1771 NotifyConfigurationChangedArgs configChangedArgs;
1772 ASSERT_NO_FATAL_FAILURE(
1773 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
1774 uint32_t prevSequenceNum = configChangedArgs.sequenceNum;
1775 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1776
1777 NotifyKeyArgs keyArgs;
1778 keyboard->pressAndReleaseHomeKey();
1779 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1780 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
1781 ASSERT_LT(prevSequenceNum, keyArgs.sequenceNum);
1782 prevSequenceNum = keyArgs.sequenceNum;
1783 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1784 prevTimestamp = keyArgs.eventTime;
1785
1786 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1787 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
1788 ASSERT_LT(prevSequenceNum, keyArgs.sequenceNum);
1789 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1790}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001791
1792// --- InputDeviceTest ---
1793
1794class InputDeviceTest : public testing::Test {
1795protected:
1796 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001797 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001798 static const int32_t DEVICE_ID;
1799 static const int32_t DEVICE_GENERATION;
1800 static const int32_t DEVICE_CONTROLLER_NUMBER;
1801 static const uint32_t DEVICE_CLASSES;
1802
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001803 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001804 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001805 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001806 FakeInputReaderContext* mFakeContext;
1807
1808 InputDevice* mDevice;
1809
Prabir Pradhan28efc192019-11-05 01:10:04 +00001810 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001811 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001812 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001813 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001814 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1815
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001816 mFakeEventHub->addDevice(DEVICE_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001817 InputDeviceIdentifier identifier;
1818 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001819 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1821 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1822 }
1823
Prabir Pradhan28efc192019-11-05 01:10:04 +00001824 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001825 delete mDevice;
1826
1827 delete mFakeContext;
1828 mFakeListener.clear();
1829 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001830 }
1831};
1832
1833const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08001834const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001835const int32_t InputDeviceTest::DEVICE_ID = 1;
1836const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1837const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1838const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1839 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1840
1841TEST_F(InputDeviceTest, ImmutableProperties) {
1842 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001843 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001844 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1845}
1846
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001847TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1848 ASSERT_EQ(mDevice->isEnabled(), true);
1849}
1850
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1852 // Configuration.
1853 InputReaderConfiguration config;
1854 mDevice->configure(ARBITRARY_TIME, &config, 0);
1855
1856 // Reset.
1857 mDevice->reset(ARBITRARY_TIME);
1858
1859 NotifyDeviceResetArgs resetArgs;
1860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1861 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1862 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1863
1864 // Metadata.
1865 ASSERT_TRUE(mDevice->isIgnored());
1866 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1867
1868 InputDeviceInfo info;
1869 mDevice->getDeviceInfo(&info);
1870 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001871 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001872 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1873 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1874
1875 // State queries.
1876 ASSERT_EQ(0, mDevice->getMetaState());
1877
1878 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1879 << "Ignored device should return unknown key code state.";
1880 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1881 << "Ignored device should return unknown scan code state.";
1882 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1883 << "Ignored device should return unknown switch state.";
1884
1885 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1886 uint8_t flags[2] = { 0, 1 };
1887 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1888 << "Ignored device should never mark any key codes.";
1889 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1890 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1891}
1892
1893TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1894 // Configuration.
1895 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1896
1897 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1898 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1899 mapper1->setMetaState(AMETA_ALT_ON);
1900 mapper1->addSupportedKeyCode(AKEYCODE_A);
1901 mapper1->addSupportedKeyCode(AKEYCODE_B);
1902 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1903 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1904 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1905 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1906 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1907 mDevice->addMapper(mapper1);
1908
1909 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1910 mapper2->setMetaState(AMETA_SHIFT_ON);
1911 mDevice->addMapper(mapper2);
1912
1913 InputReaderConfiguration config;
1914 mDevice->configure(ARBITRARY_TIME, &config, 0);
1915
1916 String8 propertyValue;
1917 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1918 << "Device should have read configuration during configuration phase.";
1919 ASSERT_STREQ("value", propertyValue.string());
1920
1921 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1922 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1923
1924 // Reset
1925 mDevice->reset(ARBITRARY_TIME);
1926 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1927 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1928
1929 NotifyDeviceResetArgs resetArgs;
1930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1931 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1932 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1933
1934 // Metadata.
1935 ASSERT_FALSE(mDevice->isIgnored());
1936 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1937
1938 InputDeviceInfo info;
1939 mDevice->getDeviceInfo(&info);
1940 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001941 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001942 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1943 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1944
1945 // State queries.
1946 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1947 << "Should query mappers and combine meta states.";
1948
1949 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1950 << "Should return unknown key code state when source not supported.";
1951 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1952 << "Should return unknown scan code state when source not supported.";
1953 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1954 << "Should return unknown switch state when source not supported.";
1955
1956 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1957 << "Should query mapper when source is supported.";
1958 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1959 << "Should query mapper when source is supported.";
1960 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1961 << "Should query mapper when source is supported.";
1962
1963 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1964 uint8_t flags[4] = { 0, 0, 0, 1 };
1965 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1966 << "Should do nothing when source is unsupported.";
1967 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1968 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1969 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1970 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1971
1972 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1973 << "Should query mapper when source is supported.";
1974 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1975 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1976 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1977 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1978
1979 // Event handling.
1980 RawEvent event;
1981 mDevice->process(&event, 1);
1982
1983 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1984 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1985}
1986
Arthur Hung2c9a3342019-07-23 14:18:59 +08001987// A single input device is associated with a specific display. Check that:
1988// 1. Device is disabled if the viewport corresponding to the associated display is not found
1989// 2. Device is disabled when setEnabled API is called
1990TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
1991 FakeInputMapper* mapper = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1992 mDevice->addMapper(mapper);
1993
1994 // First Configuration.
1995 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1996
1997 // Device should be enabled by default.
1998 ASSERT_TRUE(mDevice->isEnabled());
1999
2000 // Prepare associated info.
2001 constexpr uint8_t hdmi = 1;
2002 const std::string UNIQUE_ID = "local:1";
2003
2004 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2005 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2006 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2007 // Device should be disabled because it is associated with a specific display via
2008 // input port <-> display port association, but the corresponding display is not found
2009 ASSERT_FALSE(mDevice->isEnabled());
2010
2011 // Prepare displays.
2012 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2013 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2014 ViewportType::VIEWPORT_INTERNAL);
2015 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2016 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2017 ASSERT_TRUE(mDevice->isEnabled());
2018
2019 // Device should be disabled after set disable.
2020 mFakePolicy->addDisabledDevice(mDevice->getId());
2021 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2022 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2023 ASSERT_FALSE(mDevice->isEnabled());
2024
2025 // Device should still be disabled even found the associated display.
2026 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2027 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2028 ASSERT_FALSE(mDevice->isEnabled());
2029}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002030
2031// --- InputMapperTest ---
2032
2033class InputMapperTest : public testing::Test {
2034protected:
2035 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002036 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002037 static const int32_t DEVICE_ID;
2038 static const int32_t DEVICE_GENERATION;
2039 static const int32_t DEVICE_CONTROLLER_NUMBER;
2040 static const uint32_t DEVICE_CLASSES;
2041
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002042 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002043 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002044 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002045 FakeInputReaderContext* mFakeContext;
2046 InputDevice* mDevice;
2047
Prabir Pradhan28efc192019-11-05 01:10:04 +00002048 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002049 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002050 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002051 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002052 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2053 InputDeviceIdentifier identifier;
2054 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002055 identifier.location = DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002056 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2057 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2058
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002059 mFakeEventHub->addDevice(mDevice->getId(), DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002060 }
2061
Prabir Pradhan28efc192019-11-05 01:10:04 +00002062 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002063 delete mDevice;
2064 delete mFakeContext;
2065 mFakeListener.clear();
2066 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002067 }
2068
2069 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002070 mFakeEventHub->addConfigurationProperty(mDevice->getId(), String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002071 }
2072
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002073 void configureDevice(uint32_t changes) {
2074 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2075 }
2076
Michael Wrightd02c5b62014-02-10 15:10:22 -08002077 void addMapperAndConfigure(InputMapper* mapper) {
2078 mDevice->addMapper(mapper);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002079 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002080 mDevice->reset(ARBITRARY_TIME);
2081 }
2082
2083 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002084 int32_t orientation, const std::string& uniqueId,
2085 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002086 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002087 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002088 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2089 }
2090
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002091 void clearViewports() {
2092 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002093 }
2094
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002095 static void process(InputMapper* mapper, nsecs_t when, int32_t type,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002096 int32_t code, int32_t value) {
2097 RawEvent event;
2098 event.when = when;
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002099 event.deviceId = mapper->getDeviceId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100 event.type = type;
2101 event.code = code;
2102 event.value = value;
2103 mapper->process(&event);
2104 }
2105
2106 static void assertMotionRange(const InputDeviceInfo& info,
2107 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2108 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002109 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002110 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2111 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2112 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2113 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2114 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2115 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2116 }
2117
2118 static void assertPointerCoords(const PointerCoords& coords,
2119 float x, float y, float pressure, float size,
2120 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2121 float orientation, float distance) {
2122 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2123 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2124 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2125 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2126 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2127 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2128 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2129 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2130 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2131 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2132 }
2133
2134 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2135 float actualX, actualY;
2136 controller->getPosition(&actualX, &actualY);
2137 ASSERT_NEAR(x, actualX, 1);
2138 ASSERT_NEAR(y, actualY, 1);
2139 }
2140};
2141
2142const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002143const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002144const int32_t InputMapperTest::DEVICE_ID = 1;
2145const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2146const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2147const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
2148
2149
2150// --- SwitchInputMapperTest ---
2151
2152class SwitchInputMapperTest : public InputMapperTest {
2153protected:
2154};
2155
2156TEST_F(SwitchInputMapperTest, GetSources) {
2157 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2158 addMapperAndConfigure(mapper);
2159
2160 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
2161}
2162
2163TEST_F(SwitchInputMapperTest, GetSwitchState) {
2164 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2165 addMapperAndConfigure(mapper);
2166
2167 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
2168 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2169
2170 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
2171 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2172}
2173
2174TEST_F(SwitchInputMapperTest, Process) {
2175 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
2176 addMapperAndConfigure(mapper);
2177
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002178 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2179 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2180 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2181 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002182
2183 NotifySwitchArgs args;
2184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2185 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002186 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2187 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002188 args.switchMask);
2189 ASSERT_EQ(uint32_t(0), args.policyFlags);
2190}
2191
2192
2193// --- KeyboardInputMapperTest ---
2194
2195class KeyboardInputMapperTest : public InputMapperTest {
2196protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002197 const std::string UNIQUE_ID = "local:0";
2198
2199 void prepareDisplay(int32_t orientation);
2200
Arthur Hung2c9a3342019-07-23 14:18:59 +08002201 void testDPadKeyRotation(KeyboardInputMapper* mapper, int32_t originalScanCode,
2202 int32_t originalKeyCode, int32_t rotatedKeyCode,
2203 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002204};
2205
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002206/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2207 * orientation.
2208 */
2209void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2210 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002211 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002212}
2213
Michael Wrightd02c5b62014-02-10 15:10:22 -08002214void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002215 int32_t originalScanCode, int32_t originalKeyCode,
2216 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002217 NotifyKeyArgs args;
2218
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002219 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2221 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2222 ASSERT_EQ(originalScanCode, args.scanCode);
2223 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002224 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002225
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002226 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2228 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2229 ASSERT_EQ(originalScanCode, args.scanCode);
2230 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002231 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002232}
2233
Michael Wrightd02c5b62014-02-10 15:10:22 -08002234TEST_F(KeyboardInputMapperTest, GetSources) {
2235 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2236 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2237 addMapperAndConfigure(mapper);
2238
2239 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
2240}
2241
2242TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2243 const int32_t USAGE_A = 0x070004;
2244 const int32_t USAGE_UNKNOWN = 0x07ffff;
2245 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2246 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2247
2248 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2249 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2250 addMapperAndConfigure(mapper);
2251
2252 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002253 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002254 NotifyKeyArgs args;
2255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2256 ASSERT_EQ(DEVICE_ID, args.deviceId);
2257 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2258 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2259 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2260 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2261 ASSERT_EQ(KEY_HOME, args.scanCode);
2262 ASSERT_EQ(AMETA_NONE, args.metaState);
2263 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2264 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2265 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2266
2267 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002268 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2270 ASSERT_EQ(DEVICE_ID, args.deviceId);
2271 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2272 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2273 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2274 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2275 ASSERT_EQ(KEY_HOME, args.scanCode);
2276 ASSERT_EQ(AMETA_NONE, args.metaState);
2277 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2278 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2279 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2280
2281 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002282 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2283 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2285 ASSERT_EQ(DEVICE_ID, args.deviceId);
2286 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2287 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2288 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2289 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2290 ASSERT_EQ(0, args.scanCode);
2291 ASSERT_EQ(AMETA_NONE, args.metaState);
2292 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2293 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2294 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2295
2296 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002297 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2298 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2300 ASSERT_EQ(DEVICE_ID, args.deviceId);
2301 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2302 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2303 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2304 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2305 ASSERT_EQ(0, args.scanCode);
2306 ASSERT_EQ(AMETA_NONE, args.metaState);
2307 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2308 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2309 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2310
2311 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002312 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2313 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2315 ASSERT_EQ(DEVICE_ID, args.deviceId);
2316 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2317 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2318 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2319 ASSERT_EQ(0, args.keyCode);
2320 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2321 ASSERT_EQ(AMETA_NONE, args.metaState);
2322 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2323 ASSERT_EQ(0U, args.policyFlags);
2324 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2325
2326 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002327 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2328 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2330 ASSERT_EQ(DEVICE_ID, args.deviceId);
2331 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2332 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2333 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2334 ASSERT_EQ(0, args.keyCode);
2335 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2336 ASSERT_EQ(AMETA_NONE, args.metaState);
2337 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2338 ASSERT_EQ(0U, args.policyFlags);
2339 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2340}
2341
2342TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2343 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2344 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2345
2346 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2347 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2348 addMapperAndConfigure(mapper);
2349
2350 // Initial metastate.
2351 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2352
2353 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002354 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002355 NotifyKeyArgs args;
2356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2357 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2359 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2360
2361 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002362 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2364 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2365 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2366
2367 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002368 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2370 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2371 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
2372
2373 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002374 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2376 ASSERT_EQ(AMETA_NONE, args.metaState);
2377 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2378 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2379}
2380
2381TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2382 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2383 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2384 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2385 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2386
2387 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2388 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2389 addMapperAndConfigure(mapper);
2390
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002391 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2393 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2394 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2395 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2396 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2397 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2398 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2399 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2400}
2401
2402TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2403 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2404 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2405 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2406 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2407
2408 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2409 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2410 addConfigurationProperty("keyboard.orientationAware", "1");
2411 addMapperAndConfigure(mapper);
2412
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002413 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002414 ASSERT_NO_FATAL_FAILURE(
2415 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2416 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2417 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2418 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2419 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2420 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2421 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002422
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002423 clearViewports();
2424 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002425 ASSERT_NO_FATAL_FAILURE(
2426 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2427 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2428 AKEYCODE_DPAD_UP, DISPLAY_ID));
2429 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2430 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2431 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2432 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002433
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002434 clearViewports();
2435 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002436 ASSERT_NO_FATAL_FAILURE(
2437 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2438 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2439 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2440 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2441 AKEYCODE_DPAD_UP, DISPLAY_ID));
2442 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2443 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002444
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002445 clearViewports();
2446 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002447 ASSERT_NO_FATAL_FAILURE(
2448 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2449 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2450 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2451 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2452 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2453 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2454 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455
2456 // Special case: if orientation changes while key is down, we still emit the same keycode
2457 // in the key up as we did in the key down.
2458 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002459 clearViewports();
2460 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002461 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2463 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2464 ASSERT_EQ(KEY_UP, args.scanCode);
2465 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2466
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002467 clearViewports();
2468 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002469 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2471 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2472 ASSERT_EQ(KEY_UP, args.scanCode);
2473 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2474}
2475
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002476TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2477 // If the keyboard is not orientation aware,
2478 // key events should not be associated with a specific display id
2479 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2480
2481 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2482 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2483 addMapperAndConfigure(mapper);
2484 NotifyKeyArgs args;
2485
2486 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002487 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002489 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2491 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2492
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002493 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002494 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002496 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2498 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2499}
2500
2501TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2502 // If the keyboard is orientation aware,
2503 // key events should be associated with the internal viewport
2504 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2505
2506 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2507 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2508 addConfigurationProperty("keyboard.orientationAware", "1");
2509 addMapperAndConfigure(mapper);
2510 NotifyKeyArgs args;
2511
2512 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2513 // ^--- already checked by the previous test
2514
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002515 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002516 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002517 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002519 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2521 ASSERT_EQ(DISPLAY_ID, args.displayId);
2522
2523 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002524 clearViewports();
2525 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002526 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002527 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002529 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2531 ASSERT_EQ(newDisplayId, args.displayId);
2532}
2533
Michael Wrightd02c5b62014-02-10 15:10:22 -08002534TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2535 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2536 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2537 addMapperAndConfigure(mapper);
2538
2539 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2540 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2541
2542 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2543 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2544}
2545
2546TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2547 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2548 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2549 addMapperAndConfigure(mapper);
2550
2551 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2552 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2553
2554 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2555 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2556}
2557
2558TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2559 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2560 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2561 addMapperAndConfigure(mapper);
2562
2563 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2564
2565 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2566 uint8_t flags[2] = { 0, 0 };
2567 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2568 ASSERT_TRUE(flags[0]);
2569 ASSERT_FALSE(flags[1]);
2570}
2571
2572TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2573 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2574 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2575 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2576 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2577 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2578 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2579
2580 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2581 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2582 addMapperAndConfigure(mapper);
2583
2584 // Initialization should have turned all of the lights off.
2585 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2586 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2587 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2588
2589 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002590 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2591 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002592 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2593 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2594 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2595 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2596
2597 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002598 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2599 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2601 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2602 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2603 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2604
2605 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002606 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2607 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002608 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2609 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2610 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2611 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2612
2613 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002614 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2615 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2617 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2618 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2619 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2620
2621 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002622 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2623 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2625 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2626 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2627 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2628
2629 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002630 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2631 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2633 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2634 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2635 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2636}
2637
Arthur Hung2c9a3342019-07-23 14:18:59 +08002638TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2639 // keyboard 1.
2640 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2641 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2642 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2643 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2644
2645 // keyboard 2.
2646 const std::string USB2 = "USB2";
2647 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2648 InputDeviceIdentifier identifier;
2649 identifier.name = "KEYBOARD2";
2650 identifier.location = USB2;
2651 std::unique_ptr<InputDevice> device2 =
2652 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2653 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
2654 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
2655 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2656 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2657 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2658 mFakeEventHub->addKey(SECOND_DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2659
2660 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2661 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2662 addMapperAndConfigure(mapper);
2663
2664 KeyboardInputMapper* mapper2 = new KeyboardInputMapper(device2.get(), AINPUT_SOURCE_KEYBOARD,
2665 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2666 device2->addMapper(mapper2);
2667 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2668 device2->reset(ARBITRARY_TIME);
2669
2670 // Prepared displays and associated info.
2671 constexpr uint8_t hdmi1 = 0;
2672 constexpr uint8_t hdmi2 = 1;
2673 const std::string SECONDARY_UNIQUE_ID = "local:1";
2674
2675 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2676 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2677
2678 // No associated display viewport found, should disable the device.
2679 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2680 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2681 ASSERT_FALSE(device2->isEnabled());
2682
2683 // Prepare second display.
2684 constexpr int32_t newDisplayId = 2;
2685 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2686 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2687 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2688 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2689 // Default device will reconfigure above, need additional reconfiguration for another device.
2690 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2691 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2692
2693 // Device should be enabled after the associated display is found.
2694 ASSERT_TRUE(mDevice->isEnabled());
2695 ASSERT_TRUE(device2->isEnabled());
2696
2697 // Test pad key events
2698 ASSERT_NO_FATAL_FAILURE(
2699 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2700 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2701 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2702 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2703 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2704 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2705 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2706
2707 ASSERT_NO_FATAL_FAILURE(
2708 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2709 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2710 AKEYCODE_DPAD_RIGHT, newDisplayId));
2711 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2712 AKEYCODE_DPAD_DOWN, newDisplayId));
2713 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2714 AKEYCODE_DPAD_LEFT, newDisplayId));
2715}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002716
Powei Fengd041c5d2019-05-03 17:11:33 -07002717TEST_F(KeyboardInputMapperTest, ExternalDevice_WakeBehavior) {
2718 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2719 // marked as WAKE in the keylayout file to trigger wake.
2720 mDevice->setExternal(true);
2721
2722 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2723 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2724 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE, POLICY_FLAG_WAKE);
2725
2726 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2727 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2728 addMapperAndConfigure(mapper);
2729
2730 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2731 NotifyKeyArgs args;
2732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2733 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2734
2735 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2737 ASSERT_EQ(uint32_t(0), args.policyFlags);
2738
2739 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2741 ASSERT_EQ(uint32_t(0), args.policyFlags);
2742
2743 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2745 ASSERT_EQ(uint32_t(0), args.policyFlags);
2746
2747 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2749 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2750
2751 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2753 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2754}
2755
2756TEST_F(KeyboardInputMapperTest, ExternalDevice_DoNotWakeByDefaultBehavior) {
2757 // Tv Remote key's wake behavior is prescribed by the keylayout file.
2758 mDevice->setExternal(true);
2759
2760 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2761 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2762 mFakeEventHub->addKey(DEVICE_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
2763
2764 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD,
2765 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2766 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
2767 addMapperAndConfigure(mapper);
2768
2769 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2770 NotifyKeyArgs args;
2771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2772 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2773
2774 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2776 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2777
2778 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
2779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2780 ASSERT_EQ(uint32_t(0), args.policyFlags);
2781
2782 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
2783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2784 ASSERT_EQ(uint32_t(0), args.policyFlags);
2785
2786 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2788 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2789
2790 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2792 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2793}
2794
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795// --- CursorInputMapperTest ---
2796
2797class CursorInputMapperTest : public InputMapperTest {
2798protected:
2799 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2800
2801 sp<FakePointerController> mFakePointerController;
2802
Prabir Pradhan28efc192019-11-05 01:10:04 +00002803 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804 InputMapperTest::SetUp();
2805
2806 mFakePointerController = new FakePointerController();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002807 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 }
2809
2810 void testMotionRotation(CursorInputMapper* mapper,
2811 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002812
2813 void prepareDisplay(int32_t orientation) {
2814 const std::string uniqueId = "local:0";
2815 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
2816 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2817 orientation, uniqueId, NO_PORT, viewportType);
2818 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819};
2820
2821const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2822
2823void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2824 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2825 NotifyMotionArgs args;
2826
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002827 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
2828 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
2829 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2833 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2834 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2835 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2836}
2837
2838TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2839 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2840 addConfigurationProperty("cursor.mode", "pointer");
2841 addMapperAndConfigure(mapper);
2842
2843 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2844}
2845
2846TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2847 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2848 addConfigurationProperty("cursor.mode", "navigation");
2849 addMapperAndConfigure(mapper);
2850
2851 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2852}
2853
2854TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2855 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2856 addConfigurationProperty("cursor.mode", "pointer");
2857 addMapperAndConfigure(mapper);
2858
2859 InputDeviceInfo info;
2860 mapper->populateDeviceInfo(&info);
2861
2862 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07002863 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2864 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2866 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2867
2868 // When the bounds are set, then there should be a valid motion range.
2869 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2870
2871 InputDeviceInfo info2;
2872 mapper->populateDeviceInfo(&info2);
2873
2874 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2875 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2876 1, 800 - 1, 0.0f, 0.0f));
2877 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2878 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2879 2, 480 - 1, 0.0f, 0.0f));
2880 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2881 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2882 0.0f, 1.0f, 0.0f, 0.0f));
2883}
2884
2885TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2886 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2887 addConfigurationProperty("cursor.mode", "navigation");
2888 addMapperAndConfigure(mapper);
2889
2890 InputDeviceInfo info;
2891 mapper->populateDeviceInfo(&info);
2892
2893 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2894 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2895 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2896 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2897 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2898 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2899 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2900 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2901 0.0f, 1.0f, 0.0f, 0.0f));
2902}
2903
2904TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2905 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2906 addConfigurationProperty("cursor.mode", "navigation");
2907 addMapperAndConfigure(mapper);
2908
2909 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2910
2911 NotifyMotionArgs args;
2912
2913 // Button press.
2914 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002915 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
2916 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2918 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2919 ASSERT_EQ(DEVICE_ID, args.deviceId);
2920 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2921 ASSERT_EQ(uint32_t(0), args.policyFlags);
2922 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2923 ASSERT_EQ(0, args.flags);
2924 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2925 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2926 ASSERT_EQ(0, args.edgeFlags);
2927 ASSERT_EQ(uint32_t(1), args.pointerCount);
2928 ASSERT_EQ(0, args.pointerProperties[0].id);
2929 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2931 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2932 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2933 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2934 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2935
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2937 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2938 ASSERT_EQ(DEVICE_ID, args.deviceId);
2939 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2940 ASSERT_EQ(uint32_t(0), args.policyFlags);
2941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2942 ASSERT_EQ(0, args.flags);
2943 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2944 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2945 ASSERT_EQ(0, args.edgeFlags);
2946 ASSERT_EQ(uint32_t(1), args.pointerCount);
2947 ASSERT_EQ(0, args.pointerProperties[0].id);
2948 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2950 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2951 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2952 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2953 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2954
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002956 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
2957 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2959 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2960 ASSERT_EQ(DEVICE_ID, args.deviceId);
2961 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2962 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08002963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2964 ASSERT_EQ(0, args.flags);
2965 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2966 ASSERT_EQ(0, args.buttonState);
2967 ASSERT_EQ(0, args.edgeFlags);
2968 ASSERT_EQ(uint32_t(1), args.pointerCount);
2969 ASSERT_EQ(0, args.pointerProperties[0].id);
2970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2972 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2973 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2974 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2975 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2976
2977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2978 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2979 ASSERT_EQ(DEVICE_ID, args.deviceId);
2980 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2981 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002982 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2983 ASSERT_EQ(0, args.flags);
2984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2985 ASSERT_EQ(0, args.buttonState);
2986 ASSERT_EQ(0, args.edgeFlags);
2987 ASSERT_EQ(uint32_t(1), args.pointerCount);
2988 ASSERT_EQ(0, args.pointerProperties[0].id);
2989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2991 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2992 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2993 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2994 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2995}
2996
2997TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2998 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2999 addConfigurationProperty("cursor.mode", "navigation");
3000 addMapperAndConfigure(mapper);
3001
3002 NotifyMotionArgs args;
3003
3004 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003005 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3006 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3010 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3011
3012 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003013 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3014 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3018 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3019}
3020
3021TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
3022 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3023 addConfigurationProperty("cursor.mode", "navigation");
3024 addMapperAndConfigure(mapper);
3025
3026 NotifyMotionArgs args;
3027
3028 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003029 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3030 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3034 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3035
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3039 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3040
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003042 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3043 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3047 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3048
3049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3052 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3053}
3054
3055TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
3056 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3057 addConfigurationProperty("cursor.mode", "navigation");
3058 addMapperAndConfigure(mapper);
3059
3060 NotifyMotionArgs args;
3061
3062 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003063 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3064 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3065 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3066 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3068 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3070 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3071 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3072
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3074 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3076 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3077 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3078
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003080 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3081 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3082 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3086 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3087 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3088
3089 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003090 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3091 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3095 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3096
3097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3100 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3101}
3102
3103TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
3104 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3105 addConfigurationProperty("cursor.mode", "navigation");
3106 addMapperAndConfigure(mapper);
3107
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003108 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3117}
3118
3119TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
3120 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3121 addConfigurationProperty("cursor.mode", "navigation");
3122 addConfigurationProperty("cursor.orientationAware", "1");
3123 addMapperAndConfigure(mapper);
3124
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003125 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3134
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003135 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3144
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003145 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3152 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3153 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3154
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003155 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3164}
3165
3166TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
3167 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3168 addConfigurationProperty("cursor.mode", "pointer");
3169 addMapperAndConfigure(mapper);
3170
3171 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3172 mFakePointerController->setPosition(100, 200);
3173 mFakePointerController->setButtonState(0);
3174
3175 NotifyMotionArgs motionArgs;
3176 NotifyKeyArgs keyArgs;
3177
3178 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003179 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3180 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3182 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3183 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3184 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3186 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3187
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3190 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3191 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3193 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3194
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003195 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3196 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199 ASSERT_EQ(0, motionArgs.buttonState);
3200 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3202 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3203
3204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003205 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206 ASSERT_EQ(0, motionArgs.buttonState);
3207 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3209 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3210
3211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003213 ASSERT_EQ(0, motionArgs.buttonState);
3214 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3216 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3217
3218 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003219 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3220 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3221 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3223 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3224 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3225 motionArgs.buttonState);
3226 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3227 mFakePointerController->getButtonState());
3228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3229 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3230
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3233 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3234 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3235 mFakePointerController->getButtonState());
3236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3237 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3238
3239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3241 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3242 motionArgs.buttonState);
3243 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3244 mFakePointerController->getButtonState());
3245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3246 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3247
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003248 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3249 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3253 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3255 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3256
3257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003259 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3260 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3262 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3263
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003264 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3265 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3268 ASSERT_EQ(0, motionArgs.buttonState);
3269 ASSERT_EQ(0, mFakePointerController->getButtonState());
3270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3271 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 -08003272 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3273 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003274
3275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276 ASSERT_EQ(0, motionArgs.buttonState);
3277 ASSERT_EQ(0, mFakePointerController->getButtonState());
3278 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3280 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 -08003281
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3283 ASSERT_EQ(0, motionArgs.buttonState);
3284 ASSERT_EQ(0, mFakePointerController->getButtonState());
3285 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3287 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3288
3289 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003290 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3291 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3293 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3294 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003295
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003297 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3299 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3301 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3302
3303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3305 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3306 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3308 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3309
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003310 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3311 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 ASSERT_EQ(0, motionArgs.buttonState);
3315 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3317 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3318
3319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003321 ASSERT_EQ(0, motionArgs.buttonState);
3322 ASSERT_EQ(0, mFakePointerController->getButtonState());
3323
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3325 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3327 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3328 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3329
3330 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003331 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3332 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3334 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3335 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003336
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3340 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3342 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3343
3344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3345 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3346 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3347 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3349 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3350
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003351 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3352 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_EQ(0, motionArgs.buttonState);
3356 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3358 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 -08003359
3360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3362 ASSERT_EQ(0, motionArgs.buttonState);
3363 ASSERT_EQ(0, mFakePointerController->getButtonState());
3364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3365 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3366
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3368 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3369 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3370
3371 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003372 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3373 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3375 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3376 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003377
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003379 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3381 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3383 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3384
3385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3386 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3387 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3388 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3390 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3391
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003392 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3393 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396 ASSERT_EQ(0, motionArgs.buttonState);
3397 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3399 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 -08003400
3401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3403 ASSERT_EQ(0, motionArgs.buttonState);
3404 ASSERT_EQ(0, mFakePointerController->getButtonState());
3405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3406 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3407
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3409 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3410 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3411
3412 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003413 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3414 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3416 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3417 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003418
Michael Wrightd02c5b62014-02-10 15:10:22 -08003419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003420 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3422 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3424 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3425
3426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3428 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3429 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3431 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3432
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003433 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3434 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003436 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437 ASSERT_EQ(0, motionArgs.buttonState);
3438 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3440 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 -08003441
3442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3443 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3444 ASSERT_EQ(0, motionArgs.buttonState);
3445 ASSERT_EQ(0, mFakePointerController->getButtonState());
3446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3447 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3448
Michael Wrightd02c5b62014-02-10 15:10:22 -08003449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3450 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3451 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3452}
3453
3454TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3455 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3456 addConfigurationProperty("cursor.mode", "pointer");
3457 addMapperAndConfigure(mapper);
3458
3459 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3460 mFakePointerController->setPosition(100, 200);
3461 mFakePointerController->setButtonState(0);
3462
3463 NotifyMotionArgs args;
3464
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003465 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3466 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3467 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003469 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3470 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3472 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3473 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3474}
3475
3476TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3477 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3478 addConfigurationProperty("cursor.mode", "pointer");
3479 mFakePolicy->setPointerCapture(true);
3480 addMapperAndConfigure(mapper);
3481
3482 NotifyDeviceResetArgs resetArgs;
3483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3484 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3485 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3486
3487 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3488 mFakePointerController->setPosition(100, 200);
3489 mFakePointerController->setButtonState(0);
3490
3491 NotifyMotionArgs args;
3492
3493 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003494 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3495 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3496 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3498 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3501 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3502 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3503
3504 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003505 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3506 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3508 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3511 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3513 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3516 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3517
3518 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003519 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3520 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3522 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3523 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3525 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3527 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3528 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3530 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3531
3532 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003533 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3534 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3535 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3537 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3540 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3541 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3542
3543 // Disable pointer capture and check that the device generation got bumped
3544 // and events are generated the usual way.
3545 const uint32_t generation = mFakeContext->getGeneration();
3546 mFakePolicy->setPointerCapture(false);
3547 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3548 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3549
3550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3551 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3552 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3553
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003554 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3555 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3556 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3558 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3561 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3562 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3563}
3564
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003565TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3566 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
3567 addMapperAndConfigure(mapper);
3568
Garfield Tan888a6a42020-01-09 11:39:16 -08003569 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003570 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003571 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3572 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3573 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3574 ViewportType::VIEWPORT_EXTERNAL);
3575 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3576 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3577
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003578 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3579 mFakePointerController->setPosition(100, 200);
3580 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003581
3582 NotifyMotionArgs args;
3583 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3584 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3585 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3587 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3588 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3590 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3591 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3592 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3593}
3594
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595
3596// --- TouchInputMapperTest ---
3597
3598class TouchInputMapperTest : public InputMapperTest {
3599protected:
3600 static const int32_t RAW_X_MIN;
3601 static const int32_t RAW_X_MAX;
3602 static const int32_t RAW_Y_MIN;
3603 static const int32_t RAW_Y_MAX;
3604 static const int32_t RAW_TOUCH_MIN;
3605 static const int32_t RAW_TOUCH_MAX;
3606 static const int32_t RAW_TOOL_MIN;
3607 static const int32_t RAW_TOOL_MAX;
3608 static const int32_t RAW_PRESSURE_MIN;
3609 static const int32_t RAW_PRESSURE_MAX;
3610 static const int32_t RAW_ORIENTATION_MIN;
3611 static const int32_t RAW_ORIENTATION_MAX;
3612 static const int32_t RAW_DISTANCE_MIN;
3613 static const int32_t RAW_DISTANCE_MAX;
3614 static const int32_t RAW_TILT_MIN;
3615 static const int32_t RAW_TILT_MAX;
3616 static const int32_t RAW_ID_MIN;
3617 static const int32_t RAW_ID_MAX;
3618 static const int32_t RAW_SLOT_MIN;
3619 static const int32_t RAW_SLOT_MAX;
3620 static const float X_PRECISION;
3621 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003622 static const float X_PRECISION_VIRTUAL;
3623 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624
3625 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003626 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627
3628 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3629
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003630 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003631 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003632
Michael Wrightd02c5b62014-02-10 15:10:22 -08003633 enum Axes {
3634 POSITION = 1 << 0,
3635 TOUCH = 1 << 1,
3636 TOOL = 1 << 2,
3637 PRESSURE = 1 << 3,
3638 ORIENTATION = 1 << 4,
3639 MINOR = 1 << 5,
3640 ID = 1 << 6,
3641 DISTANCE = 1 << 7,
3642 TILT = 1 << 8,
3643 SLOT = 1 << 9,
3644 TOOL_TYPE = 1 << 10,
3645 };
3646
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003647 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3648 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003649 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003651 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652 int32_t toRawX(float displayX);
3653 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003654 float toCookedX(float rawX, float rawY);
3655 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003657 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003659 float toDisplayY(int32_t rawY, int32_t displayHeight);
3660
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661};
3662
3663const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3664const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3665const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3666const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3667const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3668const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3669const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3670const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003671const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3672const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3674const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3675const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3676const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3677const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3678const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3679const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3680const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3681const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3682const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3683const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3684const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003685const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3686 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3687const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3688 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003689const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3690 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691
3692const float TouchInputMapperTest::GEOMETRIC_SCALE =
3693 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3694 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3695
3696const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3697 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3698 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3699};
3700
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003701void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003702 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003703 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3704}
3705
3706void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3707 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3708 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709}
3710
Santos Cordonfa5cf462017-04-05 10:37:00 -07003711void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003712 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3713 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003714 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003715}
3716
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717void TouchInputMapperTest::prepareVirtualKeys() {
3718 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
3719 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
3720 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3721 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3722}
3723
Jason Gerecke489fda82012-09-07 17:19:40 -07003724void TouchInputMapperTest::prepareLocationCalibration() {
3725 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3726}
3727
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728int32_t TouchInputMapperTest::toRawX(float displayX) {
3729 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3730}
3731
3732int32_t TouchInputMapperTest::toRawY(float displayY) {
3733 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3734}
3735
Jason Gerecke489fda82012-09-07 17:19:40 -07003736float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3737 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3738 return rawX;
3739}
3740
3741float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3742 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3743 return rawY;
3744}
3745
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003747 return toDisplayX(rawX, DISPLAY_WIDTH);
3748}
3749
3750float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3751 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752}
3753
3754float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003755 return toDisplayY(rawY, DISPLAY_HEIGHT);
3756}
3757
3758float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3759 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760}
3761
3762
3763// --- SingleTouchInputMapperTest ---
3764
3765class SingleTouchInputMapperTest : public TouchInputMapperTest {
3766protected:
3767 void prepareButtons();
3768 void prepareAxes(int axes);
3769
3770 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3771 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3772 void processUp(SingleTouchInputMapper* mappery);
3773 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3774 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3775 void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3776 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3777 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3778 void processSync(SingleTouchInputMapper* mapper);
3779};
3780
3781void SingleTouchInputMapperTest::prepareButtons() {
3782 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3783}
3784
3785void SingleTouchInputMapperTest::prepareAxes(int axes) {
3786 if (axes & POSITION) {
3787 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3788 RAW_X_MIN, RAW_X_MAX, 0, 0);
3789 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3790 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3791 }
3792 if (axes & PRESSURE) {
3793 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3794 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3795 }
3796 if (axes & TOOL) {
3797 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3798 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3799 }
3800 if (axes & DISTANCE) {
3801 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3802 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3803 }
3804 if (axes & TILT) {
3805 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3806 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3807 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3808 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3809 }
3810}
3811
3812void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003813 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
3814 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3815 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816}
3817
3818void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003819 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
3820 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821}
3822
3823void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003824 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003825}
3826
3827void SingleTouchInputMapperTest::processPressure(
3828 SingleTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003829 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830}
3831
3832void SingleTouchInputMapperTest::processToolMajor(
3833 SingleTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003834 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835}
3836
3837void SingleTouchInputMapperTest::processDistance(
3838 SingleTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003839 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003840}
3841
3842void SingleTouchInputMapperTest::processTilt(
3843 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003844 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
3845 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846}
3847
3848void SingleTouchInputMapperTest::processKey(
3849 SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003850 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851}
3852
3853void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003854 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855}
3856
3857
3858TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3859 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3860 prepareButtons();
3861 prepareAxes(POSITION);
3862 addMapperAndConfigure(mapper);
3863
3864 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3865}
3866
3867TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3868 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3869 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3870 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3871 prepareButtons();
3872 prepareAxes(POSITION);
3873 addMapperAndConfigure(mapper);
3874
3875 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3876}
3877
3878TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3879 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3880 prepareButtons();
3881 prepareAxes(POSITION);
3882 addConfigurationProperty("touch.deviceType", "touchPad");
3883 addMapperAndConfigure(mapper);
3884
3885 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3886}
3887
3888TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3889 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3890 prepareButtons();
3891 prepareAxes(POSITION);
3892 addConfigurationProperty("touch.deviceType", "touchScreen");
3893 addMapperAndConfigure(mapper);
3894
3895 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3896}
3897
3898TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3899 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3900 addConfigurationProperty("touch.deviceType", "touchScreen");
3901 prepareDisplay(DISPLAY_ORIENTATION_0);
3902 prepareButtons();
3903 prepareAxes(POSITION);
3904 prepareVirtualKeys();
3905 addMapperAndConfigure(mapper);
3906
3907 // Unknown key.
3908 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3909
3910 // Virtual key is down.
3911 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3912 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3913 processDown(mapper, x, y);
3914 processSync(mapper);
3915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3916
3917 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3918
3919 // Virtual key is up.
3920 processUp(mapper);
3921 processSync(mapper);
3922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3923
3924 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3925}
3926
3927TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3928 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3929 addConfigurationProperty("touch.deviceType", "touchScreen");
3930 prepareDisplay(DISPLAY_ORIENTATION_0);
3931 prepareButtons();
3932 prepareAxes(POSITION);
3933 prepareVirtualKeys();
3934 addMapperAndConfigure(mapper);
3935
3936 // Unknown key.
3937 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3938
3939 // Virtual key is down.
3940 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3941 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3942 processDown(mapper, x, y);
3943 processSync(mapper);
3944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3945
3946 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3947
3948 // Virtual key is up.
3949 processUp(mapper);
3950 processSync(mapper);
3951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3952
3953 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3954}
3955
3956TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3957 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3958 addConfigurationProperty("touch.deviceType", "touchScreen");
3959 prepareDisplay(DISPLAY_ORIENTATION_0);
3960 prepareButtons();
3961 prepareAxes(POSITION);
3962 prepareVirtualKeys();
3963 addMapperAndConfigure(mapper);
3964
3965 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3966 uint8_t flags[2] = { 0, 0 };
3967 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3968 ASSERT_TRUE(flags[0]);
3969 ASSERT_FALSE(flags[1]);
3970}
3971
3972TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3973 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3974 addConfigurationProperty("touch.deviceType", "touchScreen");
3975 prepareDisplay(DISPLAY_ORIENTATION_0);
3976 prepareButtons();
3977 prepareAxes(POSITION);
3978 prepareVirtualKeys();
3979 addMapperAndConfigure(mapper);
3980
3981 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3982
3983 NotifyKeyArgs args;
3984
3985 // Press virtual key.
3986 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3987 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3988 processDown(mapper, x, y);
3989 processSync(mapper);
3990
3991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3992 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3993 ASSERT_EQ(DEVICE_ID, args.deviceId);
3994 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3995 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3996 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3997 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3998 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3999 ASSERT_EQ(KEY_HOME, args.scanCode);
4000 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4001 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4002
4003 // Release virtual key.
4004 processUp(mapper);
4005 processSync(mapper);
4006
4007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4008 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4009 ASSERT_EQ(DEVICE_ID, args.deviceId);
4010 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4011 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4012 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4013 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4014 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4015 ASSERT_EQ(KEY_HOME, args.scanCode);
4016 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4017 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4018
4019 // Should not have sent any motions.
4020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4021}
4022
4023TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
4024 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4025 addConfigurationProperty("touch.deviceType", "touchScreen");
4026 prepareDisplay(DISPLAY_ORIENTATION_0);
4027 prepareButtons();
4028 prepareAxes(POSITION);
4029 prepareVirtualKeys();
4030 addMapperAndConfigure(mapper);
4031
4032 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4033
4034 NotifyKeyArgs keyArgs;
4035
4036 // Press virtual key.
4037 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4038 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4039 processDown(mapper, x, y);
4040 processSync(mapper);
4041
4042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4043 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4044 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4045 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4046 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4047 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4048 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4049 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4050 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4051 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4052 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4053
4054 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4055 // into the display area.
4056 y -= 100;
4057 processMove(mapper, x, y);
4058 processSync(mapper);
4059
4060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4061 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4062 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4063 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4064 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4065 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4066 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4067 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4068 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4069 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4070 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4071 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4072
4073 NotifyMotionArgs motionArgs;
4074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4075 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4076 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4077 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4078 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4079 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4080 ASSERT_EQ(0, motionArgs.flags);
4081 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4082 ASSERT_EQ(0, motionArgs.buttonState);
4083 ASSERT_EQ(0, motionArgs.edgeFlags);
4084 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4085 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4088 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4089 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4090 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4091 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4092
4093 // Keep moving out of bounds. Should generate a pointer move.
4094 y -= 50;
4095 processMove(mapper, x, y);
4096 processSync(mapper);
4097
4098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4099 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4100 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4101 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4102 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4104 ASSERT_EQ(0, motionArgs.flags);
4105 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4106 ASSERT_EQ(0, motionArgs.buttonState);
4107 ASSERT_EQ(0, motionArgs.edgeFlags);
4108 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4109 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4112 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4113 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4114 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4115 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4116
4117 // Release out of bounds. Should generate a pointer up.
4118 processUp(mapper);
4119 processSync(mapper);
4120
4121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4122 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4123 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4124 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4125 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4126 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4127 ASSERT_EQ(0, motionArgs.flags);
4128 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4129 ASSERT_EQ(0, motionArgs.buttonState);
4130 ASSERT_EQ(0, motionArgs.edgeFlags);
4131 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4132 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4135 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4136 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4137 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4138 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4139
4140 // Should not have sent any more keys or motions.
4141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4143}
4144
4145TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
4146 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4147 addConfigurationProperty("touch.deviceType", "touchScreen");
4148 prepareDisplay(DISPLAY_ORIENTATION_0);
4149 prepareButtons();
4150 prepareAxes(POSITION);
4151 prepareVirtualKeys();
4152 addMapperAndConfigure(mapper);
4153
4154 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4155
4156 NotifyMotionArgs motionArgs;
4157
4158 // Initially go down out of bounds.
4159 int32_t x = -10;
4160 int32_t y = -10;
4161 processDown(mapper, x, y);
4162 processSync(mapper);
4163
4164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4165
4166 // Move into the display area. Should generate a pointer down.
4167 x = 50;
4168 y = 75;
4169 processMove(mapper, x, y);
4170 processSync(mapper);
4171
4172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4173 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4174 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4175 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4176 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4177 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4178 ASSERT_EQ(0, motionArgs.flags);
4179 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4180 ASSERT_EQ(0, motionArgs.buttonState);
4181 ASSERT_EQ(0, motionArgs.edgeFlags);
4182 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4183 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4186 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4187 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4188 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4189 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4190
4191 // Release. Should generate a pointer up.
4192 processUp(mapper);
4193 processSync(mapper);
4194
4195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4196 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4197 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4198 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4199 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4200 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4201 ASSERT_EQ(0, motionArgs.flags);
4202 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4203 ASSERT_EQ(0, motionArgs.buttonState);
4204 ASSERT_EQ(0, motionArgs.edgeFlags);
4205 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4206 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4209 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4210 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4211 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4213
4214 // Should not have sent any more keys or motions.
4215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4217}
4218
Santos Cordonfa5cf462017-04-05 10:37:00 -07004219TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
4220 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4221 addConfigurationProperty("touch.deviceType", "touchScreen");
4222 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4223
4224 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4225 prepareButtons();
4226 prepareAxes(POSITION);
4227 prepareVirtualKeys();
4228 addMapperAndConfigure(mapper);
4229
4230 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4231
4232 NotifyMotionArgs motionArgs;
4233
4234 // Down.
4235 int32_t x = 100;
4236 int32_t y = 125;
4237 processDown(mapper, x, y);
4238 processSync(mapper);
4239
4240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4241 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4242 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4243 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4244 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4245 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4247 ASSERT_EQ(0, motionArgs.flags);
4248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4249 ASSERT_EQ(0, motionArgs.buttonState);
4250 ASSERT_EQ(0, motionArgs.edgeFlags);
4251 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4252 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4255 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4256 1, 0, 0, 0, 0, 0, 0, 0));
4257 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4258 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4260
4261 // Move.
4262 x += 50;
4263 y += 75;
4264 processMove(mapper, x, y);
4265 processSync(mapper);
4266
4267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4269 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4270 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4271 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4272 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4274 ASSERT_EQ(0, motionArgs.flags);
4275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4276 ASSERT_EQ(0, motionArgs.buttonState);
4277 ASSERT_EQ(0, motionArgs.edgeFlags);
4278 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4279 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4282 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4283 1, 0, 0, 0, 0, 0, 0, 0));
4284 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4285 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4287
4288 // Up.
4289 processUp(mapper);
4290 processSync(mapper);
4291
4292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4295 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4296 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4297 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4298 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4299 ASSERT_EQ(0, motionArgs.flags);
4300 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4301 ASSERT_EQ(0, motionArgs.buttonState);
4302 ASSERT_EQ(0, motionArgs.edgeFlags);
4303 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4304 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4307 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4308 1, 0, 0, 0, 0, 0, 0, 0));
4309 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4310 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4312
4313 // Should not have sent any more keys or motions.
4314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4316}
4317
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
4319 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4320 addConfigurationProperty("touch.deviceType", "touchScreen");
4321 prepareDisplay(DISPLAY_ORIENTATION_0);
4322 prepareButtons();
4323 prepareAxes(POSITION);
4324 prepareVirtualKeys();
4325 addMapperAndConfigure(mapper);
4326
4327 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4328
4329 NotifyMotionArgs motionArgs;
4330
4331 // Down.
4332 int32_t x = 100;
4333 int32_t y = 125;
4334 processDown(mapper, x, y);
4335 processSync(mapper);
4336
4337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4339 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4340 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4341 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4342 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4343 ASSERT_EQ(0, motionArgs.flags);
4344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4345 ASSERT_EQ(0, motionArgs.buttonState);
4346 ASSERT_EQ(0, motionArgs.edgeFlags);
4347 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4348 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4351 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4352 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4353 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4354 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4355
4356 // Move.
4357 x += 50;
4358 y += 75;
4359 processMove(mapper, x, y);
4360 processSync(mapper);
4361
4362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4363 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4364 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4365 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4366 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4368 ASSERT_EQ(0, motionArgs.flags);
4369 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4370 ASSERT_EQ(0, motionArgs.buttonState);
4371 ASSERT_EQ(0, motionArgs.edgeFlags);
4372 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4376 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4377 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4378 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4379 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4380
4381 // Up.
4382 processUp(mapper);
4383 processSync(mapper);
4384
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4386 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4387 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4388 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4389 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4390 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4391 ASSERT_EQ(0, motionArgs.flags);
4392 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4393 ASSERT_EQ(0, motionArgs.buttonState);
4394 ASSERT_EQ(0, motionArgs.edgeFlags);
4395 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4396 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4399 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4400 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4401 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4402 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4403
4404 // Should not have sent any more keys or motions.
4405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4407}
4408
4409TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
4410 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4411 addConfigurationProperty("touch.deviceType", "touchScreen");
4412 prepareButtons();
4413 prepareAxes(POSITION);
4414 addConfigurationProperty("touch.orientationAware", "0");
4415 addMapperAndConfigure(mapper);
4416
4417 NotifyMotionArgs args;
4418
4419 // Rotation 90.
4420 prepareDisplay(DISPLAY_ORIENTATION_90);
4421 processDown(mapper, toRawX(50), toRawY(75));
4422 processSync(mapper);
4423
4424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4425 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4426 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4427
4428 processUp(mapper);
4429 processSync(mapper);
4430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4431}
4432
4433TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4434 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4435 addConfigurationProperty("touch.deviceType", "touchScreen");
4436 prepareButtons();
4437 prepareAxes(POSITION);
4438 addMapperAndConfigure(mapper);
4439
4440 NotifyMotionArgs args;
4441
4442 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004443 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 prepareDisplay(DISPLAY_ORIENTATION_0);
4445 processDown(mapper, toRawX(50), toRawY(75));
4446 processSync(mapper);
4447
4448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4449 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4450 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4451
4452 processUp(mapper);
4453 processSync(mapper);
4454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4455
4456 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004457 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 prepareDisplay(DISPLAY_ORIENTATION_90);
4459 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4460 processSync(mapper);
4461
4462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4463 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4464 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4465
4466 processUp(mapper);
4467 processSync(mapper);
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4469
4470 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004471 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472 prepareDisplay(DISPLAY_ORIENTATION_180);
4473 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4474 processSync(mapper);
4475
4476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4477 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4478 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4479
4480 processUp(mapper);
4481 processSync(mapper);
4482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4483
4484 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004485 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486 prepareDisplay(DISPLAY_ORIENTATION_270);
4487 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4488 processSync(mapper);
4489
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4491 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4492 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4493
4494 processUp(mapper);
4495 processSync(mapper);
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4497}
4498
4499TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4500 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4501 addConfigurationProperty("touch.deviceType", "touchScreen");
4502 prepareDisplay(DISPLAY_ORIENTATION_0);
4503 prepareButtons();
4504 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4505 addMapperAndConfigure(mapper);
4506
4507 // These calculations are based on the input device calibration documentation.
4508 int32_t rawX = 100;
4509 int32_t rawY = 200;
4510 int32_t rawPressure = 10;
4511 int32_t rawToolMajor = 12;
4512 int32_t rawDistance = 2;
4513 int32_t rawTiltX = 30;
4514 int32_t rawTiltY = 110;
4515
4516 float x = toDisplayX(rawX);
4517 float y = toDisplayY(rawY);
4518 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4519 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4520 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4521 float distance = float(rawDistance);
4522
4523 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4524 float tiltScale = M_PI / 180;
4525 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4526 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4527 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4528 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4529
4530 processDown(mapper, rawX, rawY);
4531 processPressure(mapper, rawPressure);
4532 processToolMajor(mapper, rawToolMajor);
4533 processDistance(mapper, rawDistance);
4534 processTilt(mapper, rawTiltX, rawTiltY);
4535 processSync(mapper);
4536
4537 NotifyMotionArgs args;
4538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4540 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4541 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4542}
4543
Jason Gerecke489fda82012-09-07 17:19:40 -07004544TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4545 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4546 addConfigurationProperty("touch.deviceType", "touchScreen");
4547 prepareDisplay(DISPLAY_ORIENTATION_0);
4548 prepareLocationCalibration();
4549 prepareButtons();
4550 prepareAxes(POSITION);
4551 addMapperAndConfigure(mapper);
4552
4553 int32_t rawX = 100;
4554 int32_t rawY = 200;
4555
4556 float x = toDisplayX(toCookedX(rawX, rawY));
4557 float y = toDisplayY(toCookedY(rawX, rawY));
4558
4559 processDown(mapper, rawX, rawY);
4560 processSync(mapper);
4561
4562 NotifyMotionArgs args;
4563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4565 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4566}
4567
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4569 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4570 addConfigurationProperty("touch.deviceType", "touchScreen");
4571 prepareDisplay(DISPLAY_ORIENTATION_0);
4572 prepareButtons();
4573 prepareAxes(POSITION);
4574 addMapperAndConfigure(mapper);
4575
4576 NotifyMotionArgs motionArgs;
4577 NotifyKeyArgs keyArgs;
4578
4579 processDown(mapper, 100, 200);
4580 processSync(mapper);
4581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4582 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4583 ASSERT_EQ(0, motionArgs.buttonState);
4584
4585 // press BTN_LEFT, release BTN_LEFT
4586 processKey(mapper, BTN_LEFT, 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_BUTTON_PRIMARY, motionArgs.buttonState);
4591
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4594 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4595
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596 processKey(mapper, BTN_LEFT, 0);
4597 processSync(mapper);
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004599 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004601
4602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605
4606 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4607 processKey(mapper, BTN_RIGHT, 1);
4608 processKey(mapper, BTN_MIDDLE, 1);
4609 processSync(mapper);
4610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4612 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4613 motionArgs.buttonState);
4614
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4616 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4617 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4618
4619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4620 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4621 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4622 motionArgs.buttonState);
4623
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 processKey(mapper, BTN_RIGHT, 0);
4625 processSync(mapper);
4626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004629
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004632 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633
4634 processKey(mapper, BTN_MIDDLE, 0);
4635 processSync(mapper);
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004637 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004639
4640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004642 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643
4644 // press BTN_BACK, release BTN_BACK
4645 processKey(mapper, BTN_BACK, 1);
4646 processSync(mapper);
4647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4648 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4649 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004650
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004653 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4654
4655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4657 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658
4659 processKey(mapper, BTN_BACK, 0);
4660 processSync(mapper);
4661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004662 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004664
4665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004667 ASSERT_EQ(0, motionArgs.buttonState);
4668
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4670 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4671 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4672
4673 // press BTN_SIDE, release BTN_SIDE
4674 processKey(mapper, BTN_SIDE, 1);
4675 processSync(mapper);
4676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4677 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4678 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004682 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4683
4684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4686 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687
4688 processKey(mapper, BTN_SIDE, 0);
4689 processSync(mapper);
4690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004691 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004693
4694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004696 ASSERT_EQ(0, motionArgs.buttonState);
4697
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4699 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4700 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4701
4702 // press BTN_FORWARD, release BTN_FORWARD
4703 processKey(mapper, BTN_FORWARD, 1);
4704 processSync(mapper);
4705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4706 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4707 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004708
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004711 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4712
4713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4715 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716
4717 processKey(mapper, BTN_FORWARD, 0);
4718 processSync(mapper);
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004720 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004722
4723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004725 ASSERT_EQ(0, motionArgs.buttonState);
4726
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4728 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4729 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4730
4731 // press BTN_EXTRA, release BTN_EXTRA
4732 processKey(mapper, BTN_EXTRA, 1);
4733 processSync(mapper);
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4735 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4736 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004737
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4741
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4743 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4744 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745
4746 processKey(mapper, BTN_EXTRA, 0);
4747 processSync(mapper);
4748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004749 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004750 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004751
4752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004754 ASSERT_EQ(0, motionArgs.buttonState);
4755
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4757 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4758 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4759
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4761
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 // press BTN_STYLUS, release BTN_STYLUS
4763 processKey(mapper, BTN_STYLUS, 1);
4764 processSync(mapper);
4765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004767 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4768
4769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4771 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772
4773 processKey(mapper, BTN_STYLUS, 0);
4774 processSync(mapper);
4775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004776 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004778
4779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004781 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782
4783 // press BTN_STYLUS2, release BTN_STYLUS2
4784 processKey(mapper, BTN_STYLUS2, 1);
4785 processSync(mapper);
4786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004788 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4789
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4791 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4792 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793
4794 processKey(mapper, BTN_STYLUS2, 0);
4795 processSync(mapper);
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004797 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004799
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803
4804 // release touch
4805 processUp(mapper);
4806 processSync(mapper);
4807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4808 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4809 ASSERT_EQ(0, motionArgs.buttonState);
4810}
4811
4812TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4813 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4814 addConfigurationProperty("touch.deviceType", "touchScreen");
4815 prepareDisplay(DISPLAY_ORIENTATION_0);
4816 prepareButtons();
4817 prepareAxes(POSITION);
4818 addMapperAndConfigure(mapper);
4819
4820 NotifyMotionArgs motionArgs;
4821
4822 // default tool type is finger
4823 processDown(mapper, 100, 200);
4824 processSync(mapper);
4825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4826 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4828
4829 // eraser
4830 processKey(mapper, BTN_TOOL_RUBBER, 1);
4831 processSync(mapper);
4832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4835
4836 // stylus
4837 processKey(mapper, BTN_TOOL_RUBBER, 0);
4838 processKey(mapper, BTN_TOOL_PEN, 1);
4839 processSync(mapper);
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4843
4844 // brush
4845 processKey(mapper, BTN_TOOL_PEN, 0);
4846 processKey(mapper, BTN_TOOL_BRUSH, 1);
4847 processSync(mapper);
4848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4851
4852 // pencil
4853 processKey(mapper, BTN_TOOL_BRUSH, 0);
4854 processKey(mapper, BTN_TOOL_PENCIL, 1);
4855 processSync(mapper);
4856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4859
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08004860 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 processKey(mapper, BTN_TOOL_PENCIL, 0);
4862 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4863 processSync(mapper);
4864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4867
4868 // mouse
4869 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4870 processKey(mapper, BTN_TOOL_MOUSE, 1);
4871 processSync(mapper);
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4875
4876 // lens
4877 processKey(mapper, BTN_TOOL_MOUSE, 0);
4878 processKey(mapper, BTN_TOOL_LENS, 1);
4879 processSync(mapper);
4880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4883
4884 // double-tap
4885 processKey(mapper, BTN_TOOL_LENS, 0);
4886 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4887 processSync(mapper);
4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4891
4892 // triple-tap
4893 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4894 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4895 processSync(mapper);
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4899
4900 // quad-tap
4901 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4902 processKey(mapper, BTN_TOOL_QUADTAP, 1);
4903 processSync(mapper);
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4907
4908 // finger
4909 processKey(mapper, BTN_TOOL_QUADTAP, 0);
4910 processKey(mapper, BTN_TOOL_FINGER, 1);
4911 processSync(mapper);
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4915
4916 // stylus trumps finger
4917 processKey(mapper, BTN_TOOL_PEN, 1);
4918 processSync(mapper);
4919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4921 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4922
4923 // eraser trumps stylus
4924 processKey(mapper, BTN_TOOL_RUBBER, 1);
4925 processSync(mapper);
4926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4929
4930 // mouse trumps eraser
4931 processKey(mapper, BTN_TOOL_MOUSE, 1);
4932 processSync(mapper);
4933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4936
4937 // back to default tool type
4938 processKey(mapper, BTN_TOOL_MOUSE, 0);
4939 processKey(mapper, BTN_TOOL_RUBBER, 0);
4940 processKey(mapper, BTN_TOOL_PEN, 0);
4941 processKey(mapper, BTN_TOOL_FINGER, 0);
4942 processSync(mapper);
4943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4946}
4947
4948TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4949 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4950 addConfigurationProperty("touch.deviceType", "touchScreen");
4951 prepareDisplay(DISPLAY_ORIENTATION_0);
4952 prepareButtons();
4953 prepareAxes(POSITION);
4954 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4955 addMapperAndConfigure(mapper);
4956
4957 NotifyMotionArgs motionArgs;
4958
4959 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4960 processKey(mapper, BTN_TOOL_FINGER, 1);
4961 processMove(mapper, 100, 200);
4962 processSync(mapper);
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4964 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4966 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4967
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4969 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4971 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4972
4973 // move a little
4974 processMove(mapper, 150, 250);
4975 processSync(mapper);
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4977 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4979 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4980
4981 // down when BTN_TOUCH is pressed, pressure defaults to 1
4982 processKey(mapper, BTN_TOUCH, 1);
4983 processSync(mapper);
4984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4985 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4987 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4988
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4990 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4992 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4993
4994 // up when BTN_TOUCH is released, hover restored
4995 processKey(mapper, BTN_TOUCH, 0);
4996 processSync(mapper);
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4998 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5000 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5001
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5003 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5005 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5006
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5008 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5010 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5011
5012 // exit hover when pointer goes away
5013 processKey(mapper, BTN_TOOL_FINGER, 0);
5014 processSync(mapper);
5015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5016 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5018 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5019}
5020
5021TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
5022 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
5023 addConfigurationProperty("touch.deviceType", "touchScreen");
5024 prepareDisplay(DISPLAY_ORIENTATION_0);
5025 prepareButtons();
5026 prepareAxes(POSITION | PRESSURE);
5027 addMapperAndConfigure(mapper);
5028
5029 NotifyMotionArgs motionArgs;
5030
5031 // initially hovering because pressure is 0
5032 processDown(mapper, 100, 200);
5033 processPressure(mapper, 0);
5034 processSync(mapper);
5035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5036 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5038 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5039
5040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5041 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5043 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5044
5045 // move a little
5046 processMove(mapper, 150, 250);
5047 processSync(mapper);
5048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5049 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5051 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5052
5053 // down when pressure is non-zero
5054 processPressure(mapper, RAW_PRESSURE_MAX);
5055 processSync(mapper);
5056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5057 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5059 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5060
5061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5062 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5064 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5065
5066 // up when pressure becomes 0, hover restored
5067 processPressure(mapper, 0);
5068 processSync(mapper);
5069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5070 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5072 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5073
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5075 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5077 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5078
5079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5080 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5082 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5083
5084 // exit hover when pointer goes away
5085 processUp(mapper);
5086 processSync(mapper);
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5088 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5090 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5091}
5092
Dan Harmsaca28402018-12-17 13:55:20 -08005093
Michael Wrightd02c5b62014-02-10 15:10:22 -08005094// --- MultiTouchInputMapperTest ---
5095
5096class MultiTouchInputMapperTest : public TouchInputMapperTest {
5097protected:
5098 void prepareAxes(int axes);
5099
5100 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
5101 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
5102 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
5103 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
5104 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
5105 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
5106 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
5107 void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
5108 void processId(MultiTouchInputMapper* mapper, int32_t id);
5109 void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
5110 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
5111 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
5112 void processMTSync(MultiTouchInputMapper* mapper);
5113 void processSync(MultiTouchInputMapper* mapper);
5114};
5115
5116void MultiTouchInputMapperTest::prepareAxes(int axes) {
5117 if (axes & POSITION) {
5118 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
5119 RAW_X_MIN, RAW_X_MAX, 0, 0);
5120 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
5121 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
5122 }
5123 if (axes & TOUCH) {
5124 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
5125 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
5126 if (axes & MINOR) {
5127 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
5128 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
5129 }
5130 }
5131 if (axes & TOOL) {
5132 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
5133 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
5134 if (axes & MINOR) {
5135 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
5136 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
5137 }
5138 }
5139 if (axes & ORIENTATION) {
5140 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
5141 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
5142 }
5143 if (axes & PRESSURE) {
5144 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
5145 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
5146 }
5147 if (axes & DISTANCE) {
5148 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
5149 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
5150 }
5151 if (axes & ID) {
5152 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
5153 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
5154 }
5155 if (axes & SLOT) {
5156 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
5157 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5158 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
5159 }
5160 if (axes & TOOL_TYPE) {
5161 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
5162 0, MT_TOOL_MAX, 0, 0);
5163 }
5164}
5165
5166void MultiTouchInputMapperTest::processPosition(
5167 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005168 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5169 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170}
5171
5172void MultiTouchInputMapperTest::processTouchMajor(
5173 MultiTouchInputMapper* mapper, int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005174 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175}
5176
5177void MultiTouchInputMapperTest::processTouchMinor(
5178 MultiTouchInputMapper* mapper, int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005179 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180}
5181
5182void MultiTouchInputMapperTest::processToolMajor(
5183 MultiTouchInputMapper* mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005184 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185}
5186
5187void MultiTouchInputMapperTest::processToolMinor(
5188 MultiTouchInputMapper* mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005189 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005190}
5191
5192void MultiTouchInputMapperTest::processOrientation(
5193 MultiTouchInputMapper* mapper, int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005194 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195}
5196
5197void MultiTouchInputMapperTest::processPressure(
5198 MultiTouchInputMapper* mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005199 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200}
5201
5202void MultiTouchInputMapperTest::processDistance(
5203 MultiTouchInputMapper* mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005204 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205}
5206
5207void MultiTouchInputMapperTest::processId(
5208 MultiTouchInputMapper* mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005209 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005210}
5211
5212void MultiTouchInputMapperTest::processSlot(
5213 MultiTouchInputMapper* mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005214 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215}
5216
5217void MultiTouchInputMapperTest::processToolType(
5218 MultiTouchInputMapper* mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005219 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220}
5221
5222void MultiTouchInputMapperTest::processKey(
5223 MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005224 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005225}
5226
5227void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005228 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229}
5230
5231void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005232 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005233}
5234
5235
5236TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
5237 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5238 addConfigurationProperty("touch.deviceType", "touchScreen");
5239 prepareDisplay(DISPLAY_ORIENTATION_0);
5240 prepareAxes(POSITION);
5241 prepareVirtualKeys();
5242 addMapperAndConfigure(mapper);
5243
5244 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5245
5246 NotifyMotionArgs motionArgs;
5247
5248 // Two fingers down at once.
5249 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5250 processPosition(mapper, x1, y1);
5251 processMTSync(mapper);
5252 processPosition(mapper, x2, y2);
5253 processMTSync(mapper);
5254 processSync(mapper);
5255
5256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5257 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5258 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5259 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5260 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5262 ASSERT_EQ(0, motionArgs.flags);
5263 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5264 ASSERT_EQ(0, motionArgs.buttonState);
5265 ASSERT_EQ(0, motionArgs.edgeFlags);
5266 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5267 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5270 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5271 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5272 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5274
5275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5276 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5277 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5278 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5279 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5280 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5281 motionArgs.action);
5282 ASSERT_EQ(0, motionArgs.flags);
5283 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5284 ASSERT_EQ(0, motionArgs.buttonState);
5285 ASSERT_EQ(0, motionArgs.edgeFlags);
5286 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5287 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5288 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5289 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5292 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5294 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5295 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5296 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5297 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5298
5299 // Move.
5300 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5301 processPosition(mapper, x1, y1);
5302 processMTSync(mapper);
5303 processPosition(mapper, x2, y2);
5304 processMTSync(mapper);
5305 processSync(mapper);
5306
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5308 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5309 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5310 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5311 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5313 ASSERT_EQ(0, motionArgs.flags);
5314 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5315 ASSERT_EQ(0, motionArgs.buttonState);
5316 ASSERT_EQ(0, motionArgs.edgeFlags);
5317 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5318 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5320 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5323 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5325 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5326 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5327 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5328 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5329
5330 // First finger up.
5331 x2 += 15; y2 -= 20;
5332 processPosition(mapper, x2, y2);
5333 processMTSync(mapper);
5334 processSync(mapper);
5335
5336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5338 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5339 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5340 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5341 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5342 motionArgs.action);
5343 ASSERT_EQ(0, motionArgs.flags);
5344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5345 ASSERT_EQ(0, motionArgs.buttonState);
5346 ASSERT_EQ(0, motionArgs.edgeFlags);
5347 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5348 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5350 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5353 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5355 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5356 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5357 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5358 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5359
5360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5361 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5362 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5363 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5364 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5366 ASSERT_EQ(0, motionArgs.flags);
5367 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5368 ASSERT_EQ(0, motionArgs.buttonState);
5369 ASSERT_EQ(0, motionArgs.edgeFlags);
5370 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5371 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5374 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5375 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5376 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5378
5379 // Move.
5380 x2 += 20; y2 -= 25;
5381 processPosition(mapper, x2, y2);
5382 processMTSync(mapper);
5383 processSync(mapper);
5384
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5386 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5387 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5388 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5389 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5391 ASSERT_EQ(0, motionArgs.flags);
5392 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5393 ASSERT_EQ(0, motionArgs.buttonState);
5394 ASSERT_EQ(0, motionArgs.edgeFlags);
5395 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5396 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5399 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5400 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5401 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5402 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5403
5404 // New finger down.
5405 int32_t x3 = 700, y3 = 300;
5406 processPosition(mapper, x2, y2);
5407 processMTSync(mapper);
5408 processPosition(mapper, x3, y3);
5409 processMTSync(mapper);
5410 processSync(mapper);
5411
5412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5413 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5414 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5415 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5416 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5417 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5418 motionArgs.action);
5419 ASSERT_EQ(0, motionArgs.flags);
5420 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5421 ASSERT_EQ(0, motionArgs.buttonState);
5422 ASSERT_EQ(0, motionArgs.edgeFlags);
5423 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5424 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5425 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5426 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5429 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5431 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5432 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5433 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5434 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5435
5436 // Second finger up.
5437 x3 += 30; y3 -= 20;
5438 processPosition(mapper, x3, y3);
5439 processMTSync(mapper);
5440 processSync(mapper);
5441
5442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5443 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5444 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5445 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5446 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5447 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5448 motionArgs.action);
5449 ASSERT_EQ(0, motionArgs.flags);
5450 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5451 ASSERT_EQ(0, motionArgs.buttonState);
5452 ASSERT_EQ(0, motionArgs.edgeFlags);
5453 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5454 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5456 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5459 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5461 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5462 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5463 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5464 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5465
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5468 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5469 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5470 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5472 ASSERT_EQ(0, motionArgs.flags);
5473 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5474 ASSERT_EQ(0, motionArgs.buttonState);
5475 ASSERT_EQ(0, motionArgs.edgeFlags);
5476 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5477 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5480 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5481 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5482 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5484
5485 // Last finger up.
5486 processMTSync(mapper);
5487 processSync(mapper);
5488
5489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5490 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5491 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5492 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5493 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5494 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5495 ASSERT_EQ(0, motionArgs.flags);
5496 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5497 ASSERT_EQ(0, motionArgs.buttonState);
5498 ASSERT_EQ(0, motionArgs.edgeFlags);
5499 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5500 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5501 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5503 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5504 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5505 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5506 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5507
5508 // Should not have sent any more keys or motions.
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5511}
5512
5513TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5514 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5515 addConfigurationProperty("touch.deviceType", "touchScreen");
5516 prepareDisplay(DISPLAY_ORIENTATION_0);
5517 prepareAxes(POSITION | ID);
5518 prepareVirtualKeys();
5519 addMapperAndConfigure(mapper);
5520
5521 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5522
5523 NotifyMotionArgs motionArgs;
5524
5525 // Two fingers down at once.
5526 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5527 processPosition(mapper, x1, y1);
5528 processId(mapper, 1);
5529 processMTSync(mapper);
5530 processPosition(mapper, x2, y2);
5531 processId(mapper, 2);
5532 processMTSync(mapper);
5533 processSync(mapper);
5534
5535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5536 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5537 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5538 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5541 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5542
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5544 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5545 motionArgs.action);
5546 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5547 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5549 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5552 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5554 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5555
5556 // Move.
5557 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5558 processPosition(mapper, x1, y1);
5559 processId(mapper, 1);
5560 processMTSync(mapper);
5561 processPosition(mapper, x2, y2);
5562 processId(mapper, 2);
5563 processMTSync(mapper);
5564 processSync(mapper);
5565
5566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5568 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5571 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5574 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5576 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5577
5578 // First finger up.
5579 x2 += 15; y2 -= 20;
5580 processPosition(mapper, x2, y2);
5581 processId(mapper, 2);
5582 processMTSync(mapper);
5583 processSync(mapper);
5584
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5586 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5587 motionArgs.action);
5588 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5589 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5591 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5594 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5596 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5597
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5600 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5601 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5604 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5605
5606 // Move.
5607 x2 += 20; y2 -= 25;
5608 processPosition(mapper, x2, y2);
5609 processId(mapper, 2);
5610 processMTSync(mapper);
5611 processSync(mapper);
5612
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5615 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5616 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5619 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5620
5621 // New finger down.
5622 int32_t x3 = 700, y3 = 300;
5623 processPosition(mapper, x2, y2);
5624 processId(mapper, 2);
5625 processMTSync(mapper);
5626 processPosition(mapper, x3, y3);
5627 processId(mapper, 3);
5628 processMTSync(mapper);
5629 processSync(mapper);
5630
5631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5632 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5633 motionArgs.action);
5634 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5637 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5640 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5642 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5643
5644 // Second finger up.
5645 x3 += 30; y3 -= 20;
5646 processPosition(mapper, x3, y3);
5647 processId(mapper, 3);
5648 processMTSync(mapper);
5649 processSync(mapper);
5650
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5652 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5653 motionArgs.action);
5654 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5655 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5656 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5657 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5660 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5662 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5663
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5666 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5667 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5670 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5671
5672 // Last finger up.
5673 processMTSync(mapper);
5674 processSync(mapper);
5675
5676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5677 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5678 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5679 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5682 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5683
5684 // Should not have sent any more keys or motions.
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5687}
5688
5689TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5690 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5691 addConfigurationProperty("touch.deviceType", "touchScreen");
5692 prepareDisplay(DISPLAY_ORIENTATION_0);
5693 prepareAxes(POSITION | ID | SLOT);
5694 prepareVirtualKeys();
5695 addMapperAndConfigure(mapper);
5696
5697 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5698
5699 NotifyMotionArgs motionArgs;
5700
5701 // Two fingers down at once.
5702 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5703 processPosition(mapper, x1, y1);
5704 processId(mapper, 1);
5705 processSlot(mapper, 1);
5706 processPosition(mapper, x2, y2);
5707 processId(mapper, 2);
5708 processSync(mapper);
5709
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5712 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5713 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5716 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5717
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5719 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5720 motionArgs.action);
5721 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5722 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5724 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5727 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5729 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5730
5731 // Move.
5732 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5733 processSlot(mapper, 0);
5734 processPosition(mapper, x1, y1);
5735 processSlot(mapper, 1);
5736 processPosition(mapper, x2, y2);
5737 processSync(mapper);
5738
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5741 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5742 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5744 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5747 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5749 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5750
5751 // First finger up.
5752 x2 += 15; y2 -= 20;
5753 processSlot(mapper, 0);
5754 processId(mapper, -1);
5755 processSlot(mapper, 1);
5756 processPosition(mapper, x2, y2);
5757 processSync(mapper);
5758
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5760 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5761 motionArgs.action);
5762 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5763 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5765 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5768 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5770 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5771
5772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5774 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5775 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5778 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5779
5780 // Move.
5781 x2 += 20; y2 -= 25;
5782 processPosition(mapper, x2, y2);
5783 processSync(mapper);
5784
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5787 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5788 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5791 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5792
5793 // New finger down.
5794 int32_t x3 = 700, y3 = 300;
5795 processPosition(mapper, x2, y2);
5796 processSlot(mapper, 0);
5797 processId(mapper, 3);
5798 processPosition(mapper, x3, y3);
5799 processSync(mapper);
5800
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5802 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5803 motionArgs.action);
5804 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5805 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5806 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5807 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5810 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5812 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5813
5814 // Second finger up.
5815 x3 += 30; y3 -= 20;
5816 processSlot(mapper, 1);
5817 processId(mapper, -1);
5818 processSlot(mapper, 0);
5819 processPosition(mapper, x3, y3);
5820 processSync(mapper);
5821
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5823 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5824 motionArgs.action);
5825 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5826 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5828 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5831 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5833 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5834
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5837 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5838 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5839 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5841 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5842
5843 // Last finger up.
5844 processId(mapper, -1);
5845 processSync(mapper);
5846
5847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5848 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5849 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5850 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5853 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5854
5855 // Should not have sent any more keys or motions.
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5858}
5859
5860TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5861 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5862 addConfigurationProperty("touch.deviceType", "touchScreen");
5863 prepareDisplay(DISPLAY_ORIENTATION_0);
5864 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5865 addMapperAndConfigure(mapper);
5866
5867 // These calculations are based on the input device calibration documentation.
5868 int32_t rawX = 100;
5869 int32_t rawY = 200;
5870 int32_t rawTouchMajor = 7;
5871 int32_t rawTouchMinor = 6;
5872 int32_t rawToolMajor = 9;
5873 int32_t rawToolMinor = 8;
5874 int32_t rawPressure = 11;
5875 int32_t rawDistance = 0;
5876 int32_t rawOrientation = 3;
5877 int32_t id = 5;
5878
5879 float x = toDisplayX(rawX);
5880 float y = toDisplayY(rawY);
5881 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5882 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5883 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5884 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5885 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5886 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5887 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5888 float distance = float(rawDistance);
5889
5890 processPosition(mapper, rawX, rawY);
5891 processTouchMajor(mapper, rawTouchMajor);
5892 processTouchMinor(mapper, rawTouchMinor);
5893 processToolMajor(mapper, rawToolMajor);
5894 processToolMinor(mapper, rawToolMinor);
5895 processPressure(mapper, rawPressure);
5896 processOrientation(mapper, rawOrientation);
5897 processDistance(mapper, rawDistance);
5898 processId(mapper, id);
5899 processMTSync(mapper);
5900 processSync(mapper);
5901
5902 NotifyMotionArgs args;
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5904 ASSERT_EQ(0, args.pointerProperties[0].id);
5905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5906 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5907 orientation, distance));
5908}
5909
5910TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5911 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5912 addConfigurationProperty("touch.deviceType", "touchScreen");
5913 prepareDisplay(DISPLAY_ORIENTATION_0);
5914 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5915 addConfigurationProperty("touch.size.calibration", "geometric");
5916 addMapperAndConfigure(mapper);
5917
5918 // These calculations are based on the input device calibration documentation.
5919 int32_t rawX = 100;
5920 int32_t rawY = 200;
5921 int32_t rawTouchMajor = 140;
5922 int32_t rawTouchMinor = 120;
5923 int32_t rawToolMajor = 180;
5924 int32_t rawToolMinor = 160;
5925
5926 float x = toDisplayX(rawX);
5927 float y = toDisplayY(rawY);
5928 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5929 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5930 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5931 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5932 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5933
5934 processPosition(mapper, rawX, rawY);
5935 processTouchMajor(mapper, rawTouchMajor);
5936 processTouchMinor(mapper, rawTouchMinor);
5937 processToolMajor(mapper, rawToolMajor);
5938 processToolMinor(mapper, rawToolMinor);
5939 processMTSync(mapper);
5940 processSync(mapper);
5941
5942 NotifyMotionArgs args;
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5945 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5946}
5947
5948TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5949 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5950 addConfigurationProperty("touch.deviceType", "touchScreen");
5951 prepareDisplay(DISPLAY_ORIENTATION_0);
5952 prepareAxes(POSITION | TOUCH | TOOL);
5953 addConfigurationProperty("touch.size.calibration", "diameter");
5954 addConfigurationProperty("touch.size.scale", "10");
5955 addConfigurationProperty("touch.size.bias", "160");
5956 addConfigurationProperty("touch.size.isSummed", "1");
5957 addMapperAndConfigure(mapper);
5958
5959 // These calculations are based on the input device calibration documentation.
5960 // Note: We only provide a single common touch/tool value because the device is assumed
5961 // not to emit separate values for each pointer (isSummed = 1).
5962 int32_t rawX = 100;
5963 int32_t rawY = 200;
5964 int32_t rawX2 = 150;
5965 int32_t rawY2 = 250;
5966 int32_t rawTouchMajor = 5;
5967 int32_t rawToolMajor = 8;
5968
5969 float x = toDisplayX(rawX);
5970 float y = toDisplayY(rawY);
5971 float x2 = toDisplayX(rawX2);
5972 float y2 = toDisplayY(rawY2);
5973 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5974 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5975 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5976
5977 processPosition(mapper, rawX, rawY);
5978 processTouchMajor(mapper, rawTouchMajor);
5979 processToolMajor(mapper, rawToolMajor);
5980 processMTSync(mapper);
5981 processPosition(mapper, rawX2, rawY2);
5982 processTouchMajor(mapper, rawTouchMajor);
5983 processToolMajor(mapper, rawToolMajor);
5984 processMTSync(mapper);
5985 processSync(mapper);
5986
5987 NotifyMotionArgs args;
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5990
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5992 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5993 args.action);
5994 ASSERT_EQ(size_t(2), args.pointerCount);
5995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5996 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5998 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5999}
6000
6001TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
6002 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6003 addConfigurationProperty("touch.deviceType", "touchScreen");
6004 prepareDisplay(DISPLAY_ORIENTATION_0);
6005 prepareAxes(POSITION | TOUCH | TOOL);
6006 addConfigurationProperty("touch.size.calibration", "area");
6007 addConfigurationProperty("touch.size.scale", "43");
6008 addConfigurationProperty("touch.size.bias", "3");
6009 addMapperAndConfigure(mapper);
6010
6011 // These calculations are based on the input device calibration documentation.
6012 int32_t rawX = 100;
6013 int32_t rawY = 200;
6014 int32_t rawTouchMajor = 5;
6015 int32_t rawToolMajor = 8;
6016
6017 float x = toDisplayX(rawX);
6018 float y = toDisplayY(rawY);
6019 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6020 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6021 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6022
6023 processPosition(mapper, rawX, rawY);
6024 processTouchMajor(mapper, rawTouchMajor);
6025 processToolMajor(mapper, rawToolMajor);
6026 processMTSync(mapper);
6027 processSync(mapper);
6028
6029 NotifyMotionArgs args;
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6032 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6033}
6034
6035TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
6036 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6037 addConfigurationProperty("touch.deviceType", "touchScreen");
6038 prepareDisplay(DISPLAY_ORIENTATION_0);
6039 prepareAxes(POSITION | PRESSURE);
6040 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6041 addConfigurationProperty("touch.pressure.scale", "0.01");
6042 addMapperAndConfigure(mapper);
6043
Michael Wrightaa449c92017-12-13 21:21:43 +00006044 InputDeviceInfo info;
6045 mapper->populateDeviceInfo(&info);
6046 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6047 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6048 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6049
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050 // These calculations are based on the input device calibration documentation.
6051 int32_t rawX = 100;
6052 int32_t rawY = 200;
6053 int32_t rawPressure = 60;
6054
6055 float x = toDisplayX(rawX);
6056 float y = toDisplayY(rawY);
6057 float pressure = float(rawPressure) * 0.01f;
6058
6059 processPosition(mapper, rawX, rawY);
6060 processPressure(mapper, rawPressure);
6061 processMTSync(mapper);
6062 processSync(mapper);
6063
6064 NotifyMotionArgs args;
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6067 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6068}
6069
6070TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
6071 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6072 addConfigurationProperty("touch.deviceType", "touchScreen");
6073 prepareDisplay(DISPLAY_ORIENTATION_0);
6074 prepareAxes(POSITION | ID | SLOT);
6075 addMapperAndConfigure(mapper);
6076
6077 NotifyMotionArgs motionArgs;
6078 NotifyKeyArgs keyArgs;
6079
6080 processId(mapper, 1);
6081 processPosition(mapper, 100, 200);
6082 processSync(mapper);
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6085 ASSERT_EQ(0, motionArgs.buttonState);
6086
6087 // press BTN_LEFT, release BTN_LEFT
6088 processKey(mapper, BTN_LEFT, 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_BUTTON_PRIMARY, motionArgs.buttonState);
6093
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6096 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6097
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098 processKey(mapper, BTN_LEFT, 0);
6099 processSync(mapper);
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006103
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006106 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107
6108 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6109 processKey(mapper, BTN_RIGHT, 1);
6110 processKey(mapper, BTN_MIDDLE, 1);
6111 processSync(mapper);
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6114 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6115 motionArgs.buttonState);
6116
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6118 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6119 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6120
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6123 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6124 motionArgs.buttonState);
6125
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 processKey(mapper, BTN_RIGHT, 0);
6127 processSync(mapper);
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006129 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006131
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006134 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006135
6136 processKey(mapper, BTN_MIDDLE, 0);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006139 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006141
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006144 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145
6146 // press BTN_BACK, release BTN_BACK
6147 processKey(mapper, BTN_BACK, 1);
6148 processSync(mapper);
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6150 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6151 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006152
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006155 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6156
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6158 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6159 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160
6161 processKey(mapper, BTN_BACK, 0);
6162 processSync(mapper);
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006166
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006169 ASSERT_EQ(0, motionArgs.buttonState);
6170
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6172 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6173 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6174
6175 // press BTN_SIDE, release BTN_SIDE
6176 processKey(mapper, BTN_SIDE, 1);
6177 processSync(mapper);
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6179 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6180 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006181
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006184 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6185
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6187 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6188 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189
6190 processKey(mapper, BTN_SIDE, 0);
6191 processSync(mapper);
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006193 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006195
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006198 ASSERT_EQ(0, motionArgs.buttonState);
6199
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6201 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6202 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6203
6204 // press BTN_FORWARD, release BTN_FORWARD
6205 processKey(mapper, BTN_FORWARD, 1);
6206 processSync(mapper);
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6208 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6209 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006213 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6214
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6217 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218
6219 processKey(mapper, BTN_FORWARD, 0);
6220 processSync(mapper);
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006222 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006223 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006224
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006227 ASSERT_EQ(0, motionArgs.buttonState);
6228
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6230 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6231 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6232
6233 // press BTN_EXTRA, release BTN_EXTRA
6234 processKey(mapper, BTN_EXTRA, 1);
6235 processSync(mapper);
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6237 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6238 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006239
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006242 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6243
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6246 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247
6248 processKey(mapper, BTN_EXTRA, 0);
6249 processSync(mapper);
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006253
6254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006256 ASSERT_EQ(0, motionArgs.buttonState);
6257
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6259 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6260 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6261
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6263
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264 // press BTN_STYLUS, release BTN_STYLUS
6265 processKey(mapper, BTN_STYLUS, 1);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006269 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6270
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6273 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274
6275 processKey(mapper, BTN_STYLUS, 0);
6276 processSync(mapper);
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006279 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006280
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006283 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284
6285 // press BTN_STYLUS2, release BTN_STYLUS2
6286 processKey(mapper, BTN_STYLUS2, 1);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006290 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6291
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6294 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295
6296 processKey(mapper, BTN_STYLUS2, 0);
6297 processSync(mapper);
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006301
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305
6306 // release touch
6307 processId(mapper, -1);
6308 processSync(mapper);
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6310 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6311 ASSERT_EQ(0, motionArgs.buttonState);
6312}
6313
6314TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
6315 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6316 addConfigurationProperty("touch.deviceType", "touchScreen");
6317 prepareDisplay(DISPLAY_ORIENTATION_0);
6318 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6319 addMapperAndConfigure(mapper);
6320
6321 NotifyMotionArgs motionArgs;
6322
6323 // default tool type is finger
6324 processId(mapper, 1);
6325 processPosition(mapper, 100, 200);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6328 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6330
6331 // eraser
6332 processKey(mapper, BTN_TOOL_RUBBER, 1);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6337
6338 // stylus
6339 processKey(mapper, BTN_TOOL_RUBBER, 0);
6340 processKey(mapper, BTN_TOOL_PEN, 1);
6341 processSync(mapper);
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6344 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6345
6346 // brush
6347 processKey(mapper, BTN_TOOL_PEN, 0);
6348 processKey(mapper, BTN_TOOL_BRUSH, 1);
6349 processSync(mapper);
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6353
6354 // pencil
6355 processKey(mapper, BTN_TOOL_BRUSH, 0);
6356 processKey(mapper, BTN_TOOL_PENCIL, 1);
6357 processSync(mapper);
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6361
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006362 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006363 processKey(mapper, BTN_TOOL_PENCIL, 0);
6364 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6365 processSync(mapper);
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6369
6370 // mouse
6371 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6372 processKey(mapper, BTN_TOOL_MOUSE, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6377
6378 // lens
6379 processKey(mapper, BTN_TOOL_MOUSE, 0);
6380 processKey(mapper, BTN_TOOL_LENS, 1);
6381 processSync(mapper);
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6385
6386 // double-tap
6387 processKey(mapper, BTN_TOOL_LENS, 0);
6388 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6393
6394 // triple-tap
6395 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6396 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6397 processSync(mapper);
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6401
6402 // quad-tap
6403 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6404 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6405 processSync(mapper);
6406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6409
6410 // finger
6411 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6412 processKey(mapper, BTN_TOOL_FINGER, 1);
6413 processSync(mapper);
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6417
6418 // stylus trumps finger
6419 processKey(mapper, BTN_TOOL_PEN, 1);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6424
6425 // eraser trumps stylus
6426 processKey(mapper, BTN_TOOL_RUBBER, 1);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6431
6432 // mouse trumps eraser
6433 processKey(mapper, BTN_TOOL_MOUSE, 1);
6434 processSync(mapper);
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6438
6439 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6440 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6445
6446 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6447 processToolType(mapper, MT_TOOL_PEN);
6448 processSync(mapper);
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6452
6453 // back to default tool type
6454 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6455 processKey(mapper, BTN_TOOL_MOUSE, 0);
6456 processKey(mapper, BTN_TOOL_RUBBER, 0);
6457 processKey(mapper, BTN_TOOL_PEN, 0);
6458 processKey(mapper, BTN_TOOL_FINGER, 0);
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6463}
6464
6465TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6466 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6467 addConfigurationProperty("touch.deviceType", "touchScreen");
6468 prepareDisplay(DISPLAY_ORIENTATION_0);
6469 prepareAxes(POSITION | ID | SLOT);
6470 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6471 addMapperAndConfigure(mapper);
6472
6473 NotifyMotionArgs motionArgs;
6474
6475 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6476 processId(mapper, 1);
6477 processPosition(mapper, 100, 200);
6478 processSync(mapper);
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6482 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6483
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6487 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6488
6489 // move a little
6490 processPosition(mapper, 150, 250);
6491 processSync(mapper);
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6493 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6495 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6496
6497 // down when BTN_TOUCH is pressed, pressure defaults to 1
6498 processKey(mapper, BTN_TOUCH, 1);
6499 processSync(mapper);
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6501 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6503 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6504
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6508 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6509
6510 // up when BTN_TOUCH is released, hover restored
6511 processKey(mapper, BTN_TOUCH, 0);
6512 processSync(mapper);
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6514 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6516 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6517
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6521 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6522
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6526 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6527
6528 // exit hover when pointer goes away
6529 processId(mapper, -1);
6530 processSync(mapper);
6531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6532 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6534 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6535}
6536
6537TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6538 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6539 addConfigurationProperty("touch.deviceType", "touchScreen");
6540 prepareDisplay(DISPLAY_ORIENTATION_0);
6541 prepareAxes(POSITION | ID | SLOT | PRESSURE);
6542 addMapperAndConfigure(mapper);
6543
6544 NotifyMotionArgs motionArgs;
6545
6546 // initially hovering because pressure is 0
6547 processId(mapper, 1);
6548 processPosition(mapper, 100, 200);
6549 processPressure(mapper, 0);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6554 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6555
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6559 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6560
6561 // move a little
6562 processPosition(mapper, 150, 250);
6563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6567 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6568
6569 // down when pressure becomes non-zero
6570 processPressure(mapper, RAW_PRESSURE_MAX);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6575 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6576
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6578 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6580 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6581
6582 // up when pressure becomes 0, hover restored
6583 processPressure(mapper, 0);
6584 processSync(mapper);
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6588 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6589
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6593 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6594
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6598 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6599
6600 // exit hover when pointer goes away
6601 processId(mapper, -1);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6606 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6607}
6608
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006609/**
6610 * Set the input device port <--> display port associations, and check that the
6611 * events are routed to the display that matches the display port.
6612 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6613 */
6614TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6615 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6616 const std::string usb2 = "USB2";
6617 const uint8_t hdmi1 = 0;
6618 const uint8_t hdmi2 = 1;
6619 const std::string secondaryUniqueId = "uniqueId2";
6620 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6621
6622 addConfigurationProperty("touch.deviceType", "touchScreen");
6623 prepareAxes(POSITION);
6624 addMapperAndConfigure(mapper);
6625
6626 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6627 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6628
6629 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6630 // for this input device is specified, and the matching viewport is not present,
6631 // the input device should be disabled (at the mapper level).
6632
6633 // Add viewport for display 2 on hdmi2
6634 prepareSecondaryDisplay(type, hdmi2);
6635 // Send a touch event
6636 processPosition(mapper, 100, 100);
6637 processSync(mapper);
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6639
6640 // Add viewport for display 1 on hdmi1
6641 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6642 // Send a touch event again
6643 processPosition(mapper, 100, 100);
6644 processSync(mapper);
6645
6646 NotifyMotionArgs args;
6647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6648 ASSERT_EQ(DISPLAY_ID, args.displayId);
6649}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006650
Arthur Hung41a712e2018-11-22 19:41:03 +08006651/**
6652 * Expect fallback to internal viewport if device is external and external viewport is not present.
6653 */
6654TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) {
6655 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6656 prepareAxes(POSITION);
6657 addConfigurationProperty("touch.deviceType", "touchScreen");
6658 prepareDisplay(DISPLAY_ORIENTATION_0);
6659 mDevice->setExternal(true);
6660 addMapperAndConfigure(mapper);
6661
6662 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
6663
6664 NotifyMotionArgs motionArgs;
6665
6666 // Expect the event to be sent to the internal viewport,
6667 // because an external viewport is not present.
6668 processPosition(mapper, 100, 100);
6669 processSync(mapper);
6670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6671 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
6672
6673 // Expect the event to be sent to the external viewport if it is present.
6674 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6675 processPosition(mapper, 100, 100);
6676 processSync(mapper);
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6678 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6679}
6680
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006681TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006682 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006683 sp<FakePointerController> fakePointerController = new FakePointerController();
Garfield Tan888a6a42020-01-09 11:39:16 -08006684 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006685 fakePointerController->setPosition(100, 200);
6686 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006687 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6688
Garfield Tan888a6a42020-01-09 11:39:16 -08006689 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6690 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6691
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006692 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6693 prepareDisplay(DISPLAY_ORIENTATION_0);
6694 prepareAxes(POSITION);
6695 addMapperAndConfigure(mapper);
6696
6697 // Check source is mouse that would obtain the PointerController.
6698 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
6699
6700 NotifyMotionArgs motionArgs;
6701 processPosition(mapper, 100, 100);
6702 processSync(mapper);
6703
6704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6705 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6706 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6707}
6708
Arthur Hung7c645402019-01-25 17:45:42 +08006709TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6710 // Setup the first touch screen device.
6711 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6712 prepareAxes(POSITION | ID | SLOT);
6713 addConfigurationProperty("touch.deviceType", "touchScreen");
6714 addMapperAndConfigure(mapper);
6715
6716 // Create the second touch screen device, and enable multi fingers.
6717 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006718 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006719 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006720 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006721 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006722 std::unique_ptr<InputDevice> device2 =
6723 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6724 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
Arthur Hung7c645402019-01-25 17:45:42 +08006725 mFakeEventHub->addDevice(SECOND_DEVICE_ID, DEVICE_NAME, 0 /*classes*/);
6726 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6727 0 /*flat*/, 0 /*fuzz*/);
6728 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6729 0 /*flat*/, 0 /*fuzz*/);
6730 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6731 0 /*flat*/, 0 /*fuzz*/);
6732 mFakeEventHub->addAbsoluteAxis(SECOND_DEVICE_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6733 0 /*flat*/, 0 /*fuzz*/);
6734 mFakeEventHub->setAbsoluteAxisValue(SECOND_DEVICE_ID, ABS_MT_SLOT, 0 /*value*/);
6735 mFakeEventHub->addConfigurationProperty(SECOND_DEVICE_ID, String8("touch.deviceType"),
6736 String8("touchScreen"));
6737
6738 // Setup the second touch screen device.
Arthur Hung2c9a3342019-07-23 14:18:59 +08006739 MultiTouchInputMapper* mapper2 = new MultiTouchInputMapper(device2.get());
Arthur Hung7c645402019-01-25 17:45:42 +08006740 device2->addMapper(mapper2);
6741 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6742 device2->reset(ARBITRARY_TIME);
6743
6744 // Setup PointerController.
6745 sp<FakePointerController> fakePointerController = new FakePointerController();
6746 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6747 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6748
6749 // Setup policy for associated displays and show touches.
6750 const uint8_t hdmi1 = 0;
6751 const uint8_t hdmi2 = 1;
6752 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6753 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6754 mFakePolicy->setShowTouches(true);
6755
6756 // Create displays.
6757 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6758 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6759
6760 // Default device will reconfigure above, need additional reconfiguration for another device.
6761 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6762 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6763
6764 // Two fingers down at default display.
6765 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6766 processPosition(mapper, x1, y1);
6767 processId(mapper, 1);
6768 processSlot(mapper, 1);
6769 processPosition(mapper, x2, y2);
6770 processId(mapper, 2);
6771 processSync(mapper);
6772
6773 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6774 fakePointerController->getSpots().find(DISPLAY_ID);
6775 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6776 ASSERT_EQ(size_t(2), iter->second.size());
6777
6778 // Two fingers down at second display.
6779 processPosition(mapper2, x1, y1);
6780 processId(mapper2, 1);
6781 processSlot(mapper2, 1);
6782 processPosition(mapper2, x2, y2);
6783 processId(mapper2, 2);
6784 processSync(mapper2);
6785
6786 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6787 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6788 ASSERT_EQ(size_t(2), iter->second.size());
6789}
6790
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006791TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6792 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6793 prepareAxes(POSITION);
6794 addConfigurationProperty("touch.deviceType", "touchScreen");
6795 prepareDisplay(DISPLAY_ORIENTATION_0);
6796 addMapperAndConfigure(mapper);
6797
6798 NotifyMotionArgs motionArgs;
6799 // Unrotated video frame
6800 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6801 std::vector<TouchVideoFrame> frames{frame};
6802 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6803 processPosition(mapper, 100, 200);
6804 processSync(mapper);
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6806 ASSERT_EQ(frames, motionArgs.videoFrames);
6807
6808 // Subsequent touch events should not have any videoframes
6809 // This is implemented separately in FakeEventHub,
6810 // but that should match the behaviour of TouchVideoDevice.
6811 processPosition(mapper, 200, 200);
6812 processSync(mapper);
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6814 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6815}
6816
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006817TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6818 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6819 prepareAxes(POSITION);
6820 addConfigurationProperty("touch.deviceType", "touchScreen");
6821 addMapperAndConfigure(mapper);
6822 // Unrotated video frame
6823 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6824 NotifyMotionArgs motionArgs;
6825
6826 // Test all 4 orientations
6827 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6828 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6829 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6830 clearViewports();
6831 prepareDisplay(orientation);
6832 std::vector<TouchVideoFrame> frames{frame};
6833 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6834 processPosition(mapper, 100, 200);
6835 processSync(mapper);
6836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6837 frames[0].rotate(orientation);
6838 ASSERT_EQ(frames, motionArgs.videoFrames);
6839 }
6840}
6841
6842TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6843 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6844 prepareAxes(POSITION);
6845 addConfigurationProperty("touch.deviceType", "touchScreen");
6846 addMapperAndConfigure(mapper);
6847 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6848 // so mix these.
6849 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6850 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6851 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6852 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6853 NotifyMotionArgs motionArgs;
6854
6855 prepareDisplay(DISPLAY_ORIENTATION_90);
6856 mFakeEventHub->setVideoFrames({{mDevice->getId(), frames}});
6857 processPosition(mapper, 100, 200);
6858 processSync(mapper);
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6860 std::for_each(frames.begin(), frames.end(),
6861 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6862 ASSERT_EQ(frames, motionArgs.videoFrames);
6863}
6864
Arthur Hung9da14732019-09-02 16:16:58 +08006865/**
6866 * If we had defined port associations, but the viewport is not ready, the touch device would be
6867 * expected to be disabled, and it should be enabled after the viewport has found.
6868 */
6869TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
6870 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6871 constexpr uint8_t hdmi2 = 1;
6872 const std::string secondaryUniqueId = "uniqueId2";
6873 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6874
6875 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6876
6877 addConfigurationProperty("touch.deviceType", "touchScreen");
6878 prepareAxes(POSITION);
6879 addMapperAndConfigure(mapper);
6880
6881 ASSERT_EQ(mDevice->isEnabled(), false);
6882
6883 // Add display on hdmi2, the device should be enabled and can receive touch event.
6884 prepareSecondaryDisplay(type, hdmi2);
6885 ASSERT_EQ(mDevice->isEnabled(), true);
6886
6887 // Send a touch event.
6888 processPosition(mapper, 100, 100);
6889 processSync(mapper);
6890
6891 NotifyMotionArgs args;
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6893 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
6894}
6895
Arthur Hung6cd19a42019-08-30 19:04:12 +08006896/**
6897 * Test touch should not work if outside of surface.
6898 */
6899TEST_F(MultiTouchInputMapperTest, Viewports_SurfaceRange) {
6900 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6901 addConfigurationProperty("touch.deviceType", "touchScreen");
6902 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung6cd19a42019-08-30 19:04:12 +08006903 prepareAxes(POSITION);
6904 addMapperAndConfigure(mapper);
6905
Arthur Hung05de5772019-09-26 18:31:26 +08006906 // Touch on left-top area should work.
6907 int32_t rawX = DISPLAY_WIDTH / 2 - 1;
6908 int32_t rawY = DISPLAY_HEIGHT / 2 - 1;
6909 processPosition(mapper, rawX, rawY);
6910 processSync(mapper);
6911
6912 NotifyMotionArgs args;
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6914
6915 // Reset.
6916 mapper->reset(ARBITRARY_TIME);
6917
6918 // Let logical display be different to physical display and rotate 90-degrees.
6919 std::optional<DisplayViewport> internalViewport =
6920 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
6921 internalViewport->orientation = DISPLAY_ORIENTATION_90;
6922 internalViewport->logicalLeft = 0;
6923 internalViewport->logicalTop = 0;
6924 internalViewport->logicalRight = DISPLAY_HEIGHT;
6925 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
6926
6927 internalViewport->physicalLeft = DISPLAY_HEIGHT;
6928 internalViewport->physicalTop = DISPLAY_WIDTH / 2;
6929 internalViewport->physicalRight = DISPLAY_HEIGHT;
6930 internalViewport->physicalBottom = DISPLAY_WIDTH;
6931
6932 internalViewport->deviceWidth = DISPLAY_HEIGHT;
6933 internalViewport->deviceHeight = DISPLAY_WIDTH;
6934 mFakePolicy->updateViewport(internalViewport.value());
6935 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6936
6937 // Display align to right-top after rotate 90-degrees, touch on left-top area should not work.
Arthur Hung6cd19a42019-08-30 19:04:12 +08006938 processPosition(mapper, rawX, rawY);
6939 processSync(mapper);
6940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6941}
6942
Arthur Hung421eb1c2020-01-16 00:09:42 +08006943TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
6944 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6945 addConfigurationProperty("touch.deviceType", "touchScreen");
6946 prepareDisplay(DISPLAY_ORIENTATION_0);
6947 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6948 addMapperAndConfigure(mapper);
6949
6950 NotifyMotionArgs motionArgs;
6951
6952 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
6953 // finger down
6954 processId(mapper, 1);
6955 processPosition(mapper, x1, y1);
6956 processSync(mapper);
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6958 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6960
6961 // finger move
6962 processId(mapper, 1);
6963 processPosition(mapper, x2, y2);
6964 processSync(mapper);
6965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6968
6969 // finger up.
6970 processId(mapper, -1);
6971 processSync(mapper);
6972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6973 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6975
6976 // new finger down
6977 processId(mapper, 1);
6978 processPosition(mapper, x3, y3);
6979 processSync(mapper);
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6981 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6983}
6984
6985/**
6986 * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
6987 * UP events should be ignored.
6988 */
6989TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
6990 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
6991 addConfigurationProperty("touch.deviceType", "touchScreen");
6992 prepareDisplay(DISPLAY_ORIENTATION_0);
6993 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6994 addMapperAndConfigure(mapper);
6995
6996 NotifyMotionArgs motionArgs;
6997
6998 // default tool type is finger
6999 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7000 processId(mapper, 1);
7001 processPosition(mapper, x1, y1);
7002 processSync(mapper);
7003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7004 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7006
7007 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7008 processToolType(mapper, MT_TOOL_PALM);
7009 processSync(mapper);
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7011 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7012
7013 // Ignore the following MOVE and UP events if had detect a palm event.
7014 processId(mapper, 1);
7015 processPosition(mapper, x2, y2);
7016 processSync(mapper);
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7018
7019 // finger up.
7020 processId(mapper, -1);
7021 processSync(mapper);
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7023
7024 // new finger down
7025 processToolType(mapper, MT_TOOL_FINGER);
7026 processId(mapper, 1);
7027 processPosition(mapper, x3, y3);
7028 processSync(mapper);
7029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7030 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7032}
7033
Michael Wrightd02c5b62014-02-10 15:10:22 -08007034} // namespace android