blob: c3cb29a8655fde48866c8bff5d4a136c33a7bae9 [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>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070030#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080032#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <math.h>
34
Michael Wright7a376672020-06-26 20:51:44 +010035#include <memory>
36
Michael Wrightd02c5b62014-02-10 15:10:22 -080037namespace android {
38
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039using std::chrono_literals::operator""ms;
40
41// Timeout for waiting for an expected event
42static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
43
Michael Wrightd02c5b62014-02-10 15:10:22 -080044// An arbitrary time value.
45static const nsecs_t ARBITRARY_TIME = 1234;
46
47// Arbitrary display properties.
arthurhung65600042020-04-30 17:55:40 +080048static constexpr int32_t DISPLAY_ID = 0;
49static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
50static constexpr int32_t DISPLAY_WIDTH = 480;
51static constexpr int32_t DISPLAY_HEIGHT = 800;
52static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
53static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
54static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070055static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070056static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080057
arthurhung65600042020-04-30 17:55:40 +080058static constexpr int32_t FIRST_SLOT = 0;
59static constexpr int32_t SECOND_SLOT = 1;
60static constexpr int32_t THIRD_SLOT = 2;
61static constexpr int32_t INVALID_TRACKING_ID = -1;
62static constexpr int32_t FIRST_TRACKING_ID = 0;
63static constexpr int32_t SECOND_TRACKING_ID = 1;
64static constexpr int32_t THIRD_TRACKING_ID = 2;
65
Michael Wrightd02c5b62014-02-10 15:10:22 -080066// Error tolerance for floating point assertions.
67static const float EPSILON = 0.001f;
68
69template<typename T>
70static inline T min(T a, T b) {
71 return a < b ? a : b;
72}
73
74static inline float avg(float x, float y) {
75 return (x + y) / 2;
76}
77
78
79// --- FakePointerController ---
80
81class FakePointerController : public PointerControllerInterface {
82 bool mHaveBounds;
83 float mMinX, mMinY, mMaxX, mMaxY;
84 float mX, mY;
85 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080086 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080087
Michael Wrightd02c5b62014-02-10 15:10:22 -080088public:
89 FakePointerController() :
90 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080091 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080092 }
93
Michael Wright7a376672020-06-26 20:51:44 +010094 virtual ~FakePointerController() {}
95
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 void setBounds(float minX, float minY, float maxX, float maxY) {
97 mHaveBounds = true;
98 mMinX = minX;
99 mMinY = minY;
100 mMaxX = maxX;
101 mMaxY = maxY;
102 }
103
104 virtual void setPosition(float x, float y) {
105 mX = x;
106 mY = y;
107 }
108
109 virtual void setButtonState(int32_t buttonState) {
110 mButtonState = buttonState;
111 }
112
113 virtual int32_t getButtonState() const {
114 return mButtonState;
115 }
116
117 virtual void getPosition(float* outX, float* outY) const {
118 *outX = mX;
119 *outY = mY;
120 }
121
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800122 virtual int32_t getDisplayId() const {
123 return mDisplayId;
124 }
125
Garfield Tan888a6a42020-01-09 11:39:16 -0800126 virtual void setDisplayViewport(const DisplayViewport& viewport) {
127 mDisplayId = viewport.displayId;
128 }
129
Arthur Hung7c645402019-01-25 17:45:42 +0800130 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
131 return mSpotsByDisplay;
132 }
133
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134private:
135 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
136 *outMinX = mMinX;
137 *outMinY = mMinY;
138 *outMaxX = mMaxX;
139 *outMaxY = mMaxY;
140 return mHaveBounds;
141 }
142
143 virtual void move(float deltaX, float deltaY) {
144 mX += deltaX;
145 if (mX < mMinX) mX = mMinX;
146 if (mX > mMaxX) mX = mMaxX;
147 mY += deltaY;
148 if (mY < mMinY) mY = mMinY;
149 if (mY > mMaxY) mY = mMaxY;
150 }
151
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100152 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153 }
154
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100155 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156 }
157
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100158 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159 }
160
Arthur Hung7c645402019-01-25 17:45:42 +0800161 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
162 int32_t displayId) {
163 std::vector<int32_t> newSpots;
164 // Add spots for fingers that are down.
165 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
166 uint32_t id = idBits.clearFirstMarkedBit();
167 newSpots.push_back(id);
168 }
169
170 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 }
172
173 virtual void clearSpots() {
174 }
Arthur Hung7c645402019-01-25 17:45:42 +0800175
176 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177};
178
179
180// --- FakeInputReaderPolicy ---
181
182class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700183 std::mutex mLock;
184 std::condition_variable mDevicesChangedCondition;
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 InputReaderConfiguration mConfig;
Michael Wright7a376672020-06-26 20:51:44 +0100187 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700188 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
189 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100190 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700191 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192
193protected:
194 virtual ~FakeInputReaderPolicy() { }
195
196public:
197 FakeInputReaderPolicy() {
198 }
199
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700200 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800201 waitForInputDevices([](bool devicesChanged) {
202 if (!devicesChanged) {
203 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
204 }
205 });
206 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700207
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800208 void assertInputDevicesNotChanged() {
209 waitForInputDevices([](bool devicesChanged) {
210 if (devicesChanged) {
211 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
212 }
213 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700214 }
215
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700216 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100217 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100218 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700219 }
220
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700221 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
222 return mConfig.getDisplayViewportByUniqueId(uniqueId);
223 }
224 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
225 return mConfig.getDisplayViewportByType(type);
226 }
227
228 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
229 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700230 }
231
232 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700233 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
234 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700235 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700236 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700237 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239 }
240
Arthur Hung6cd19a42019-08-30 19:04:12 +0800241 bool updateViewport(const DisplayViewport& viewport) {
242 size_t count = mViewports.size();
243 for (size_t i = 0; i < count; i++) {
244 const DisplayViewport& currentViewport = mViewports[i];
245 if (currentViewport.displayId == viewport.displayId) {
246 mViewports[i] = viewport;
247 mConfig.setDisplayViewports(mViewports);
248 return true;
249 }
250 }
251 // no viewport found.
252 return false;
253 }
254
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100255 void addExcludedDeviceName(const std::string& deviceName) {
256 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257 }
258
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700259 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
260 mConfig.portAssociations.insert({inputPort, displayPort});
261 }
262
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000263 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700264
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000265 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700266
Michael Wright7a376672020-06-26 20:51:44 +0100267 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
268 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 }
270
271 const InputReaderConfiguration* getReaderConfiguration() const {
272 return &mConfig;
273 }
274
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800275 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276 return mInputDevices;
277 }
278
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100279 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700280 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700281 return transform;
282 }
283
284 void setTouchAffineTransformation(const TouchAffineTransformation t) {
285 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800286 }
287
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800288 void setPointerCapture(bool enabled) {
289 mConfig.pointerCapture = enabled;
290 }
291
Arthur Hung7c645402019-01-25 17:45:42 +0800292 void setShowTouches(bool enabled) {
293 mConfig.showTouches = enabled;
294 }
295
Garfield Tan888a6a42020-01-09 11:39:16 -0800296 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
297 mConfig.defaultPointerDisplayId = pointerDisplayId;
298 }
299
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700301 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700302 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
303 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700304 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
305 || orientation == DISPLAY_ORIENTATION_270);
306 DisplayViewport v;
307 v.displayId = displayId;
308 v.orientation = orientation;
309 v.logicalLeft = 0;
310 v.logicalTop = 0;
311 v.logicalRight = isRotated ? height : width;
312 v.logicalBottom = isRotated ? width : height;
313 v.physicalLeft = 0;
314 v.physicalTop = 0;
315 v.physicalRight = isRotated ? height : width;
316 v.physicalBottom = isRotated ? width : height;
317 v.deviceWidth = isRotated ? height : width;
318 v.deviceHeight = isRotated ? width : height;
319 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700320 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100321 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700322 return v;
323 }
324
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
326 *outConfig = mConfig;
327 }
328
Michael Wright7a376672020-06-26 20:51:44 +0100329 virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
330 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 }
332
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800333 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700334 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700336 mInputDevicesChanged = true;
337 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
339
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100340 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700341 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342 }
343
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100344 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
345 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800347
348 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
349 std::unique_lock<std::mutex> lock(mLock);
350 base::ScopedLockAssertion assumeLocked(mLock);
351
352 const bool devicesChanged =
353 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
354 return mInputDevicesChanged;
355 });
356 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
357 mInputDevicesChanged = false;
358 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359};
360
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361// --- FakeEventHub ---
362
363class FakeEventHub : public EventHubInterface {
364 struct KeyInfo {
365 int32_t keyCode;
366 uint32_t flags;
367 };
368
369 struct Device {
370 InputDeviceIdentifier identifier;
371 uint32_t classes;
372 PropertyMap configuration;
373 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
374 KeyedVector<int, bool> relativeAxes;
375 KeyedVector<int32_t, int32_t> keyCodeStates;
376 KeyedVector<int32_t, int32_t> scanCodeStates;
377 KeyedVector<int32_t, int32_t> switchStates;
378 KeyedVector<int32_t, int32_t> absoluteAxisValue;
379 KeyedVector<int32_t, KeyInfo> keysByScanCode;
380 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
381 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800382 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700383 bool enabled;
384
385 status_t enable() {
386 enabled = true;
387 return OK;
388 }
389
390 status_t disable() {
391 enabled = false;
392 return OK;
393 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700395 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700396 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 }
398 };
399
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700400 std::mutex mLock;
401 std::condition_variable mEventsCondition;
402
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100404 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700405 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600406 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700408public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 virtual ~FakeEventHub() {
410 for (size_t i = 0; i < mDevices.size(); i++) {
411 delete mDevices.valueAt(i);
412 }
413 }
414
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415 FakeEventHub() { }
416
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100417 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418 Device* device = new Device(classes);
419 device->identifier.name = name;
420 mDevices.add(deviceId, device);
421
422 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
423 }
424
425 void removeDevice(int32_t deviceId) {
426 delete mDevices.valueFor(deviceId);
427 mDevices.removeItem(deviceId);
428
429 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
430 }
431
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700432 bool isDeviceEnabled(int32_t deviceId) {
433 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700434 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700435 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
436 return false;
437 }
438 return device->enabled;
439 }
440
441 status_t enableDevice(int32_t deviceId) {
442 status_t result;
443 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700444 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700445 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
446 return BAD_VALUE;
447 }
448 if (device->enabled) {
449 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
450 return OK;
451 }
452 result = device->enable();
453 return result;
454 }
455
456 status_t disableDevice(int32_t deviceId) {
457 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700458 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700459 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
460 return BAD_VALUE;
461 }
462 if (!device->enabled) {
463 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
464 return OK;
465 }
466 return device->disable();
467 }
468
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469 void finishDeviceScan() {
470 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
471 }
472
473 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
474 Device* device = getDevice(deviceId);
475 device->configuration.addProperty(key, value);
476 }
477
478 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
479 Device* device = getDevice(deviceId);
480 device->configuration.addAll(configuration);
481 }
482
483 void addAbsoluteAxis(int32_t deviceId, int axis,
484 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
485 Device* device = getDevice(deviceId);
486
487 RawAbsoluteAxisInfo info;
488 info.valid = true;
489 info.minValue = minValue;
490 info.maxValue = maxValue;
491 info.flat = flat;
492 info.fuzz = fuzz;
493 info.resolution = resolution;
494 device->absoluteAxes.add(axis, info);
495 }
496
497 void addRelativeAxis(int32_t deviceId, int32_t axis) {
498 Device* device = getDevice(deviceId);
499 device->relativeAxes.add(axis, true);
500 }
501
502 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
503 Device* device = getDevice(deviceId);
504 device->keyCodeStates.replaceValueFor(keyCode, state);
505 }
506
507 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
508 Device* device = getDevice(deviceId);
509 device->scanCodeStates.replaceValueFor(scanCode, state);
510 }
511
512 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
513 Device* device = getDevice(deviceId);
514 device->switchStates.replaceValueFor(switchCode, state);
515 }
516
517 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
518 Device* device = getDevice(deviceId);
519 device->absoluteAxisValue.replaceValueFor(axis, value);
520 }
521
522 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
523 int32_t keyCode, uint32_t flags) {
524 Device* device = getDevice(deviceId);
525 KeyInfo info;
526 info.keyCode = keyCode;
527 info.flags = flags;
528 if (scanCode) {
529 device->keysByScanCode.add(scanCode, info);
530 }
531 if (usageCode) {
532 device->keysByUsageCode.add(usageCode, info);
533 }
534 }
535
536 void addLed(int32_t deviceId, int32_t led, bool initialState) {
537 Device* device = getDevice(deviceId);
538 device->leds.add(led, initialState);
539 }
540
541 bool getLedState(int32_t deviceId, int32_t led) {
542 Device* device = getDevice(deviceId);
543 return device->leds.valueFor(led);
544 }
545
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100546 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 return mExcludedDevices;
548 }
549
550 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
551 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800552 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553 }
554
555 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
556 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700557 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558 RawEvent event;
559 event.when = when;
560 event.deviceId = deviceId;
561 event.type = type;
562 event.code = code;
563 event.value = value;
564 mEvents.push_back(event);
565
566 if (type == EV_ABS) {
567 setAbsoluteAxisValue(deviceId, code, value);
568 }
569 }
570
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600571 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
572 std::vector<TouchVideoFrame>> videoFrames) {
573 mVideoFrames = std::move(videoFrames);
574 }
575
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700577 std::unique_lock<std::mutex> lock(mLock);
578 base::ScopedLockAssertion assumeLocked(mLock);
579 const bool queueIsEmpty =
580 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
581 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
582 if (!queueIsEmpty) {
583 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
584 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585 }
586
587private:
588 Device* getDevice(int32_t deviceId) const {
589 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100590 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 }
592
593 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
594 Device* device = getDevice(deviceId);
595 return device ? device->classes : 0;
596 }
597
598 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
599 Device* device = getDevice(deviceId);
600 return device ? device->identifier : InputDeviceIdentifier();
601 }
602
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100603 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604 return 0;
605 }
606
607 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
608 Device* device = getDevice(deviceId);
609 if (device) {
610 *outConfiguration = device->configuration;
611 }
612 }
613
614 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
615 RawAbsoluteAxisInfo* outAxisInfo) const {
616 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800617 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 ssize_t index = device->absoluteAxes.indexOfKey(axis);
619 if (index >= 0) {
620 *outAxisInfo = device->absoluteAxes.valueAt(index);
621 return OK;
622 }
623 }
624 outAxisInfo->clear();
625 return -1;
626 }
627
628 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
629 Device* device = getDevice(deviceId);
630 if (device) {
631 return device->relativeAxes.indexOfKey(axis) >= 0;
632 }
633 return false;
634 }
635
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100636 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 return false;
638 }
639
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700640 virtual status_t mapKey(int32_t deviceId,
641 int32_t scanCode, int32_t usageCode, int32_t metaState,
642 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 Device* device = getDevice(deviceId);
644 if (device) {
645 const KeyInfo* key = getKey(device, scanCode, usageCode);
646 if (key) {
647 if (outKeycode) {
648 *outKeycode = key->keyCode;
649 }
650 if (outFlags) {
651 *outFlags = key->flags;
652 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700653 if (outMetaState) {
654 *outMetaState = metaState;
655 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 return OK;
657 }
658 }
659 return NAME_NOT_FOUND;
660 }
661
662 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
663 if (usageCode) {
664 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
665 if (index >= 0) {
666 return &device->keysByUsageCode.valueAt(index);
667 }
668 }
669 if (scanCode) {
670 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
671 if (index >= 0) {
672 return &device->keysByScanCode.valueAt(index);
673 }
674 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700675 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676 }
677
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100678 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 return NAME_NOT_FOUND;
680 }
681
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100682 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 mExcludedDevices = devices;
684 }
685
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100686 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700687 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 if (mEvents.empty()) {
689 return 0;
690 }
691
692 *buffer = *mEvents.begin();
693 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700694 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800695 return 1;
696 }
697
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800698 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600699 auto it = mVideoFrames.find(deviceId);
700 if (it != mVideoFrames.end()) {
701 std::vector<TouchVideoFrame> frames = std::move(it->second);
702 mVideoFrames.erase(deviceId);
703 return frames;
704 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800705 return {};
706 }
707
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
709 Device* device = getDevice(deviceId);
710 if (device) {
711 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
712 if (index >= 0) {
713 return device->scanCodeStates.valueAt(index);
714 }
715 }
716 return AKEY_STATE_UNKNOWN;
717 }
718
719 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
720 Device* device = getDevice(deviceId);
721 if (device) {
722 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
723 if (index >= 0) {
724 return device->keyCodeStates.valueAt(index);
725 }
726 }
727 return AKEY_STATE_UNKNOWN;
728 }
729
730 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
731 Device* device = getDevice(deviceId);
732 if (device) {
733 ssize_t index = device->switchStates.indexOfKey(sw);
734 if (index >= 0) {
735 return device->switchStates.valueAt(index);
736 }
737 }
738 return AKEY_STATE_UNKNOWN;
739 }
740
741 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
742 int32_t* outValue) const {
743 Device* device = getDevice(deviceId);
744 if (device) {
745 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
746 if (index >= 0) {
747 *outValue = device->absoluteAxisValue.valueAt(index);
748 return OK;
749 }
750 }
751 *outValue = 0;
752 return -1;
753 }
754
755 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
756 uint8_t* outFlags) const {
757 bool result = false;
758 Device* device = getDevice(deviceId);
759 if (device) {
760 for (size_t i = 0; i < numCodes; i++) {
761 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
762 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
763 outFlags[i] = 1;
764 result = true;
765 }
766 }
767 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
768 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
769 outFlags[i] = 1;
770 result = true;
771 }
772 }
773 }
774 }
775 return result;
776 }
777
778 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
779 Device* device = getDevice(deviceId);
780 if (device) {
781 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
782 return index >= 0;
783 }
784 return false;
785 }
786
787 virtual bool hasLed(int32_t deviceId, int32_t led) const {
788 Device* device = getDevice(deviceId);
789 return device && device->leds.indexOfKey(led) >= 0;
790 }
791
792 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
793 Device* device = getDevice(deviceId);
794 if (device) {
795 ssize_t index = device->leds.indexOfKey(led);
796 if (index >= 0) {
797 device->leds.replaceValueAt(led, on);
798 } else {
799 ADD_FAILURE()
800 << "Attempted to set the state of an LED that the EventHub declared "
801 "was not present. led=" << led;
802 }
803 }
804 }
805
806 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800807 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808 outVirtualKeys.clear();
809
810 Device* device = getDevice(deviceId);
811 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800812 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 }
814 }
815
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100816 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700817 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 }
819
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100820 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 return false;
822 }
823
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100824 virtual void vibrate(int32_t, nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 }
826
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100827 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 }
829
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100830 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831 return false;
832 }
833
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800834 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800835 }
836
837 virtual void monitor() {
838 }
839
840 virtual void requestReopenDevices() {
841 }
842
843 virtual void wake() {
844 }
845};
846
847
848// --- FakeInputReaderContext ---
849
850class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700851 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852 sp<InputReaderPolicyInterface> mPolicy;
853 sp<InputListenerInterface> mListener;
854 int32_t mGlobalMetaState;
855 bool mUpdateGlobalMetaStateWasCalled;
856 int32_t mGeneration;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800857 int32_t mNextId;
Michael Wright7a376672020-06-26 20:51:44 +0100858 std::weak_ptr<PointerControllerInterface> mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859
860public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700861 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
862 const sp<InputReaderPolicyInterface>& policy,
863 const sp<InputListenerInterface>& listener)
864 : mEventHub(eventHub),
865 mPolicy(policy),
866 mListener(listener),
867 mGlobalMetaState(0),
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800868 mNextId(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
870 virtual ~FakeInputReaderContext() { }
871
872 void assertUpdateGlobalMetaStateWasCalled() {
873 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
874 << "Expected updateGlobalMetaState() to have been called.";
875 mUpdateGlobalMetaStateWasCalled = false;
876 }
877
878 void setGlobalMetaState(int32_t state) {
879 mGlobalMetaState = state;
880 }
881
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800882 uint32_t getGeneration() {
883 return mGeneration;
884 }
885
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800886 void updatePointerDisplay() {
Michael Wright7a376672020-06-26 20:51:44 +0100887 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800888 if (controller != nullptr) {
889 InputReaderConfiguration config;
890 mPolicy->getReaderConfiguration(&config);
891 auto viewport = config.getDisplayViewportById(config.defaultPointerDisplayId);
892 if (viewport) {
893 controller->setDisplayViewport(*viewport);
894 }
895 }
896 }
897
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898private:
899 virtual void updateGlobalMetaState() {
900 mUpdateGlobalMetaStateWasCalled = true;
901 }
902
903 virtual int32_t getGlobalMetaState() {
904 return mGlobalMetaState;
905 }
906
907 virtual EventHubInterface* getEventHub() {
908 return mEventHub.get();
909 }
910
911 virtual InputReaderPolicyInterface* getPolicy() {
912 return mPolicy.get();
913 }
914
915 virtual InputListenerInterface* getListener() {
916 return mListener.get();
917 }
918
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100919 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920 }
921
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800922 virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923
Michael Wright7a376672020-06-26 20:51:44 +0100924 virtual std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) {
925 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800926 if (controller == nullptr) {
927 controller = mPolicy->obtainPointerController(deviceId);
928 mPointerController = controller;
929 updatePointerDisplay();
930 }
931 return controller;
932 }
933
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 virtual void fadePointer() {
935 }
936
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100937 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 }
939
940 virtual int32_t bumpGeneration() {
941 return ++mGeneration;
942 }
Michael Wright842500e2015-03-13 17:32:02 -0700943
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800944 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700945
946 }
947
948 virtual void dispatchExternalStylusState(const StylusState&) {
949
950 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800951
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800952 virtual int32_t getNextId() { return mNextId++; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953};
954
955
956// --- FakeInputMapper ---
957
958class FakeInputMapper : public InputMapper {
959 uint32_t mSources;
960 int32_t mKeyboardType;
961 int32_t mMetaState;
962 KeyedVector<int32_t, int32_t> mKeyCodeStates;
963 KeyedVector<int32_t, int32_t> mScanCodeStates;
964 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800965 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700967 std::mutex mLock;
968 std::condition_variable mStateChangedCondition;
969 bool mConfigureWasCalled GUARDED_BY(mLock);
970 bool mResetWasCalled GUARDED_BY(mLock);
971 bool mProcessWasCalled GUARDED_BY(mLock);
972 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973
Arthur Hungc23540e2018-11-29 20:42:11 +0800974 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800975public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800976 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
977 : InputMapper(deviceContext),
978 mSources(sources),
979 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800981 mConfigureWasCalled(false),
982 mResetWasCalled(false),
983 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984
985 virtual ~FakeInputMapper() { }
986
987 void setKeyboardType(int32_t keyboardType) {
988 mKeyboardType = keyboardType;
989 }
990
991 void setMetaState(int32_t metaState) {
992 mMetaState = metaState;
993 }
994
995 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 std::unique_lock<std::mutex> lock(mLock);
997 base::ScopedLockAssertion assumeLocked(mLock);
998 const bool configureCalled =
999 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1000 return mConfigureWasCalled;
1001 });
1002 if (!configureCalled) {
1003 FAIL() << "Expected configure() to have been called.";
1004 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005 mConfigureWasCalled = false;
1006 }
1007
1008 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001009 std::unique_lock<std::mutex> lock(mLock);
1010 base::ScopedLockAssertion assumeLocked(mLock);
1011 const bool resetCalled =
1012 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1013 return mResetWasCalled;
1014 });
1015 if (!resetCalled) {
1016 FAIL() << "Expected reset() to have been called.";
1017 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018 mResetWasCalled = false;
1019 }
1020
Yi Kong9b14ac62018-07-17 13:48:38 -07001021 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001022 std::unique_lock<std::mutex> lock(mLock);
1023 base::ScopedLockAssertion assumeLocked(mLock);
1024 const bool processCalled =
1025 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1026 return mProcessWasCalled;
1027 });
1028 if (!processCalled) {
1029 FAIL() << "Expected process() to have been called.";
1030 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 if (outLastEvent) {
1032 *outLastEvent = mLastEvent;
1033 }
1034 mProcessWasCalled = false;
1035 }
1036
1037 void setKeyCodeState(int32_t keyCode, int32_t state) {
1038 mKeyCodeStates.replaceValueFor(keyCode, state);
1039 }
1040
1041 void setScanCodeState(int32_t scanCode, int32_t state) {
1042 mScanCodeStates.replaceValueFor(scanCode, state);
1043 }
1044
1045 void setSwitchState(int32_t switchCode, int32_t state) {
1046 mSwitchStates.replaceValueFor(switchCode, state);
1047 }
1048
1049 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001050 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051 }
1052
1053private:
1054 virtual uint32_t getSources() {
1055 return mSources;
1056 }
1057
1058 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1059 InputMapper::populateDeviceInfo(deviceInfo);
1060
1061 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1062 deviceInfo->setKeyboardType(mKeyboardType);
1063 }
1064 }
1065
Arthur Hungc23540e2018-11-29 20:42:11 +08001066 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001067 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001069
1070 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001071 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001072 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1073 mViewport = config->getDisplayViewportByPort(*displayPort);
1074 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001075
1076 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077 }
1078
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001079 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001080 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001082 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083 }
1084
1085 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001086 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087 mLastEvent = *rawEvent;
1088 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001089 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 }
1091
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001092 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001093 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1094 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1095 }
1096
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001097 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1099 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1100 }
1101
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001102 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1104 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1105 }
1106
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001107 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108 const int32_t* keyCodes, uint8_t* outFlags) {
1109 bool result = false;
1110 for (size_t i = 0; i < numCodes; i++) {
1111 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1112 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1113 outFlags[i] = 1;
1114 result = true;
1115 }
1116 }
1117 }
1118 return result;
1119 }
1120
1121 virtual int32_t getMetaState() {
1122 return mMetaState;
1123 }
1124
1125 virtual void fadePointer() {
1126 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001127
1128 virtual std::optional<int32_t> getAssociatedDisplay() {
1129 if (mViewport) {
1130 return std::make_optional(mViewport->displayId);
1131 }
1132 return std::nullopt;
1133 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001134};
1135
1136
1137// --- InstrumentedInputReader ---
1138
1139class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001140 std::shared_ptr<InputDevice> mNextDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141
1142public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001143 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1144 const sp<InputReaderPolicyInterface>& policy,
1145 const sp<InputListenerInterface>& listener)
1146 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001148 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001150 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001152 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001153 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154 InputDeviceIdentifier identifier;
1155 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001156 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001158 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 }
1160
Prabir Pradhan28efc192019-11-05 01:10:04 +00001161 // Make the protected loopOnce method accessible to tests.
1162 using InputReader::loopOnce;
1163
Michael Wrightd02c5b62014-02-10 15:10:22 -08001164protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001165 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1166 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001168 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001169 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001170 return device;
1171 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001172 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173 }
1174
1175 friend class InputReaderTest;
1176};
1177
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001178// --- InputReaderPolicyTest ---
1179class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001180protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001181 sp<FakeInputReaderPolicy> mFakePolicy;
1182
Prabir Pradhan28efc192019-11-05 01:10:04 +00001183 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1184 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185};
1186
1187/**
1188 * Check that empty set of viewports is an acceptable configuration.
1189 * Also try to get internal viewport two different ways - by type and by uniqueId.
1190 *
1191 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1192 * Such configuration is not currently allowed.
1193 */
1194TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001195 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001196
1197 // We didn't add any viewports yet, so there shouldn't be any.
1198 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001199 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001200 ASSERT_FALSE(internalViewport);
1201
1202 // Add an internal viewport, then clear it
1203 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001204 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001205
1206 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001207 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001208 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001209 ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210
1211 // Check matching by viewport type
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001212 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001213 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001214 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001215
1216 mFakePolicy->clearViewports();
1217 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001218 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001219 ASSERT_FALSE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001220 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001221 ASSERT_FALSE(internalViewport);
1222}
1223
1224TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1225 const std::string internalUniqueId = "local:0";
1226 const std::string externalUniqueId = "local:1";
1227 const std::string virtualUniqueId1 = "virtual:2";
1228 const std::string virtualUniqueId2 = "virtual:3";
1229 constexpr int32_t virtualDisplayId1 = 2;
1230 constexpr int32_t virtualDisplayId2 = 3;
1231
1232 // Add an internal viewport
1233 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001234 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001235 // Add an external viewport
1236 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001237 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001238 // Add an virtual viewport
1239 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001240 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001241 // Add another virtual viewport
1242 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001243 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001244
1245 // Check matching by type for internal
1246 std::optional<DisplayViewport> internalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001247 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001248 ASSERT_TRUE(internalViewport);
1249 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1250
1251 // Check matching by type for external
1252 std::optional<DisplayViewport> externalViewport =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001253 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001254 ASSERT_TRUE(externalViewport);
1255 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1256
1257 // Check matching by uniqueId for virtual viewport #1
1258 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001259 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001260 ASSERT_TRUE(virtualViewport1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001261 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001262 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1263 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1264
1265 // Check matching by uniqueId for virtual viewport #2
1266 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001267 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001268 ASSERT_TRUE(virtualViewport2);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001269 ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001270 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1271 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1272}
1273
1274
1275/**
1276 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1277 * that lookup works by checking display id.
1278 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1279 */
1280TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1281 const std::string uniqueId1 = "uniqueId1";
1282 const std::string uniqueId2 = "uniqueId2";
1283 constexpr int32_t displayId1 = 2;
1284 constexpr int32_t displayId2 = 3;
1285
1286 std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1287 ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1288 for (const ViewportType& type : types) {
1289 mFakePolicy->clearViewports();
1290 // Add a viewport
1291 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001292 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001293 // Add another viewport
1294 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001295 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001296
1297 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001298 std::optional<DisplayViewport> viewport1 =
1299 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001300 ASSERT_TRUE(viewport1);
1301 ASSERT_EQ(displayId1, viewport1->displayId);
1302 ASSERT_EQ(type, viewport1->type);
1303
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001304 std::optional<DisplayViewport> viewport2 =
1305 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001306 ASSERT_TRUE(viewport2);
1307 ASSERT_EQ(displayId2, viewport2->displayId);
1308 ASSERT_EQ(type, viewport2->type);
1309
1310 // When there are multiple viewports of the same kind, and uniqueId is not specified
1311 // in the call to getDisplayViewport, then that situation is not supported.
1312 // The viewports can be stored in any order, so we cannot rely on the order, since that
1313 // is just implementation detail.
1314 // However, we can check that it still returns *a* viewport, we just cannot assert
1315 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001316 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001317 ASSERT_TRUE(someViewport);
1318 }
1319}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001320
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001321/**
1322 * Check getDisplayViewportByPort
1323 */
1324TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1325 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1326 const std::string uniqueId1 = "uniqueId1";
1327 const std::string uniqueId2 = "uniqueId2";
1328 constexpr int32_t displayId1 = 1;
1329 constexpr int32_t displayId2 = 2;
1330 const uint8_t hdmi1 = 0;
1331 const uint8_t hdmi2 = 1;
1332 const uint8_t hdmi3 = 2;
1333
1334 mFakePolicy->clearViewports();
1335 // Add a viewport that's associated with some display port that's not of interest.
1336 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1337 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1338 // Add another viewport, connected to HDMI1 port
1339 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1340 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1341
1342 // Check that correct display viewport was returned by comparing the display ports.
1343 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1344 ASSERT_TRUE(hdmi1Viewport);
1345 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1346 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1347
1348 // Check that we can still get the same viewport using the uniqueId
1349 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1350 ASSERT_TRUE(hdmi1Viewport);
1351 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1352 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1353 ASSERT_EQ(type, hdmi1Viewport->type);
1354
1355 // Check that we cannot find a port with "HDMI2", because we never added one
1356 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1357 ASSERT_FALSE(hdmi2Viewport);
1358}
1359
Michael Wrightd02c5b62014-02-10 15:10:22 -08001360// --- InputReaderTest ---
1361
1362class InputReaderTest : public testing::Test {
1363protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001364 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001366 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001367 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001368
Prabir Pradhan28efc192019-11-05 01:10:04 +00001369 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001370 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001371 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001372 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373
Prabir Pradhan28efc192019-11-05 01:10:04 +00001374 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1375 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001376 }
1377
Prabir Pradhan28efc192019-11-05 01:10:04 +00001378 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001379 mFakeListener.clear();
1380 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381 }
1382
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001383 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1384 const PropertyMap* configuration) {
1385 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386
1387 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001388 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001389 }
1390 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001391 mReader->loopOnce();
1392 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001393 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1394 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001395 }
1396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001397 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001398 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001399 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001400 }
1401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001402 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001403 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001404 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001405 }
1406
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001407 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001408 const std::string& name, uint32_t classes,
1409 uint32_t sources,
1410 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001411 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1412 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001413 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001414 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001415 return mapper;
1416 }
1417};
1418
1419TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001420 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001421 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001422 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001423 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001424
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001425 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001426 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001427 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001428 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001429 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001430 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1431 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1432 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1433
1434 // Should also have received a notification describing the new input devices.
1435 inputDevices = mFakePolicy->getInputDevices();
1436 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001437 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001438 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001439 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1440 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1441 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1442}
1443
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001444TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001445 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001446 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001447 constexpr int32_t eventHubId = 1;
1448 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001449 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001450 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001451 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001452 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001453
Yi Kong9b14ac62018-07-17 13:48:38 -07001454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001455
1456 NotifyDeviceResetArgs resetArgs;
1457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001458 ASSERT_EQ(deviceId, resetArgs.deviceId);
1459
1460 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001461 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001462 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001463
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001465 ASSERT_EQ(deviceId, resetArgs.deviceId);
1466 ASSERT_EQ(device->isEnabled(), false);
1467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001468 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001469 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001472 ASSERT_EQ(device->isEnabled(), false);
1473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001474 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001475 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001477 ASSERT_EQ(deviceId, resetArgs.deviceId);
1478 ASSERT_EQ(device->isEnabled(), true);
1479}
1480
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001482 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1483 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1484 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001485 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001486 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001487 AINPUT_SOURCE_KEYBOARD, nullptr);
1488 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001489
1490 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1491 AINPUT_SOURCE_ANY, AKEYCODE_A))
1492 << "Should return unknown when the device id is >= 0 but unknown.";
1493
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001494 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1495 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1496 << "Should return unknown when the device id is valid but the sources are not "
1497 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001498
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001499 ASSERT_EQ(AKEY_STATE_DOWN,
1500 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1501 AKEYCODE_A))
1502 << "Should return value provided by mapper when device id is valid and the device "
1503 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001504
1505 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1506 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1507 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1508
1509 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1510 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1511 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1512}
1513
1514TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001515 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1516 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1517 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001518 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001520 AINPUT_SOURCE_KEYBOARD, nullptr);
1521 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001522
1523 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1524 AINPUT_SOURCE_ANY, KEY_A))
1525 << "Should return unknown when the device id is >= 0 but unknown.";
1526
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001527 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1528 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1529 << "Should return unknown when the device id is valid but the sources are not "
1530 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001531
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001532 ASSERT_EQ(AKEY_STATE_DOWN,
1533 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1534 KEY_A))
1535 << "Should return value provided by mapper when device id is valid and the device "
1536 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001537
1538 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1539 AINPUT_SOURCE_TRACKBALL, KEY_A))
1540 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1541
1542 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1543 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1544 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1545}
1546
1547TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001548 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1549 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1550 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001551 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001552 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001553 AINPUT_SOURCE_KEYBOARD, nullptr);
1554 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001555
1556 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1557 AINPUT_SOURCE_ANY, SW_LID))
1558 << "Should return unknown when the device id is >= 0 but unknown.";
1559
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001560 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1561 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1562 << "Should return unknown when the device id is valid but the sources are not "
1563 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001565 ASSERT_EQ(AKEY_STATE_DOWN,
1566 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1567 SW_LID))
1568 << "Should return value provided by mapper when device id is valid and the device "
1569 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570
1571 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1572 AINPUT_SOURCE_TRACKBALL, SW_LID))
1573 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1574
1575 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1576 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1577 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1578}
1579
1580TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001581 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1582 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1583 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001584 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001585 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001586 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001587
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001588 mapper.addSupportedKeyCode(AKEYCODE_A);
1589 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590
1591 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1592 uint8_t flags[4] = { 0, 0, 0, 1 };
1593
1594 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1595 << "Should return false when device id is >= 0 but unknown.";
1596 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1597
1598 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001599 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1600 << "Should return false when device id is valid but the sources are not supported by "
1601 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001602 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1603
1604 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1606 keyCodes, flags))
1607 << "Should return value provided by mapper when device id is valid and the device "
1608 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001609 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1610
1611 flags[3] = 1;
1612 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1613 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1614 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1615
1616 flags[3] = 1;
1617 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1618 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1619 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1620}
1621
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001622TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001623 constexpr int32_t eventHubId = 1;
1624 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625
1626 NotifyConfigurationChangedArgs args;
1627
1628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1629 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1630}
1631
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001632TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1634 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1635 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001636 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001637 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001638 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001639
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001640 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001641 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1643
1644 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001645 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001647 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648 ASSERT_EQ(EV_KEY, event.type);
1649 ASSERT_EQ(KEY_A, event.code);
1650 ASSERT_EQ(1, event.value);
1651}
1652
Garfield Tan1c7bc862020-01-28 13:24:04 -08001653TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001654 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001655 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001656 constexpr int32_t eventHubId = 1;
1657 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001658 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001659 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001660 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001661 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001662
1663 NotifyDeviceResetArgs resetArgs;
1664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001665 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001666
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001667 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001668 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001670 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001671 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001673 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001674 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001676 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001677 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001678
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001679 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001680 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001682 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001683 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001684}
1685
Garfield Tan1c7bc862020-01-28 13:24:04 -08001686TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1687 constexpr int32_t deviceId = 1;
1688 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1689 constexpr int32_t eventHubId = 1;
1690 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1691 // Must add at least one mapper or the device will be ignored!
1692 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1693 mReader->setNextDevice(device);
1694 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1695
1696 NotifyDeviceResetArgs resetArgs;
1697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1698 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1699}
1700
Arthur Hungc23540e2018-11-29 20:42:11 +08001701TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001702 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001703 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001704 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001705 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1707 FakeInputMapper& mapper =
1708 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001709 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001710
1711 const uint8_t hdmi1 = 1;
1712
1713 // Associated touch screen with second display.
1714 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1715
1716 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001717 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001718 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1719 DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1720 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1721 DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1722 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001723 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001724
1725 // Add the device, and make sure all of the callbacks are triggered.
1726 // The device is added after the input port associations are processed since
1727 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001728 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001731 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001732
Arthur Hung2c9a3342019-07-23 14:18:59 +08001733 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001734 ASSERT_EQ(deviceId, device->getId());
1735 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1736 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001737
1738 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001739 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001740 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001741 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001742}
1743
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001744// --- InputReaderIntegrationTest ---
1745
1746// These tests create and interact with the InputReader only through its interface.
1747// The InputReader is started during SetUp(), which starts its processing in its own
1748// thread. The tests use linux uinput to emulate input devices.
1749// NOTE: Interacting with the physical device while these tests are running may cause
1750// the tests to fail.
1751class InputReaderIntegrationTest : public testing::Test {
1752protected:
1753 sp<TestInputListener> mTestListener;
1754 sp<FakeInputReaderPolicy> mFakePolicy;
1755 sp<InputReaderInterface> mReader;
1756
1757 virtual void SetUp() override {
1758 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001759 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1760 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001761
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001762 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001763 ASSERT_EQ(mReader->start(), OK);
1764
1765 // Since this test is run on a real device, all the input devices connected
1766 // to the test device will show up in mReader. We wait for those input devices to
1767 // show up before beginning the tests.
1768 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1770 }
1771
1772 virtual void TearDown() override {
1773 ASSERT_EQ(mReader->stop(), OK);
1774 mTestListener.clear();
1775 mFakePolicy.clear();
1776 }
1777};
1778
1779TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1780 // An invalid input device that is only used for this test.
1781 class InvalidUinputDevice : public UinputDevice {
1782 public:
1783 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1784
1785 private:
1786 void configureDevice(int fd, uinput_user_dev* device) override {}
1787 };
1788
1789 const size_t numDevices = mFakePolicy->getInputDevices().size();
1790
1791 // UinputDevice does not set any event or key bits, so InputReader should not
1792 // consider it as a valid device.
1793 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1794 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1795 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1796 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1797
1798 invalidDevice.reset();
1799 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1800 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1801 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1802}
1803
1804TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1805 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1806
1807 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1808 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1809 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1810 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1811
1812 // Find the test device by its name.
1813 std::vector<InputDeviceInfo> inputDevices;
1814 mReader->getInputDevices(inputDevices);
1815 InputDeviceInfo* keyboardInfo = nullptr;
1816 const char* keyboardName = keyboard->getName();
1817 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1818 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1819 keyboardInfo = &inputDevices[i];
1820 break;
1821 }
1822 }
1823 ASSERT_NE(keyboardInfo, nullptr);
1824 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1825 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1826 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1827
1828 keyboard.reset();
1829 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1830 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1831 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1832}
1833
1834TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1835 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1836 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1837
1838 NotifyConfigurationChangedArgs configChangedArgs;
1839 ASSERT_NO_FATAL_FAILURE(
1840 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001841 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001842 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1843
1844 NotifyKeyArgs keyArgs;
1845 keyboard->pressAndReleaseHomeKey();
1846 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1847 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001848 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001849 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001850 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1851 prevTimestamp = keyArgs.eventTime;
1852
1853 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1854 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001855 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001856 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1857}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001858
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001859/**
1860 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1861 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1862 * are passed to the listener.
1863 */
1864static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1865TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1866 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1867 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1868 NotifyKeyArgs keyArgs;
1869
1870 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1871 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1872 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1873 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1874
1875 controller->pressAndReleaseKey(BTN_GEAR_UP);
1876 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1877 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1878 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1879}
1880
Arthur Hungaab25622020-01-16 11:22:11 +08001881// --- TouchProcessTest ---
1882class TouchIntegrationTest : public InputReaderIntegrationTest {
1883protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001884 const std::string UNIQUE_ID = "local:0";
1885
1886 virtual void SetUp() override {
1887 InputReaderIntegrationTest::SetUp();
1888 // At least add an internal display.
1889 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1890 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
1891 ViewportType::VIEWPORT_INTERNAL);
1892
1893 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1894 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1895 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1896 }
1897
1898 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1899 int32_t orientation, const std::string& uniqueId,
1900 std::optional<uint8_t> physicalPort,
1901 ViewportType viewportType) {
1902 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1903 physicalPort, viewportType);
1904 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1905 }
1906
1907 std::unique_ptr<UinputTouchScreen> mDevice;
1908};
1909
1910TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1911 NotifyMotionArgs args;
1912 const Point centerPoint = mDevice->getCenterPoint();
1913
1914 // ACTION_DOWN
1915 mDevice->sendDown(centerPoint);
1916 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1917 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1918
1919 // ACTION_MOVE
1920 mDevice->sendMove(centerPoint + Point(1, 1));
1921 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1923
1924 // ACTION_UP
1925 mDevice->sendUp();
1926 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1927 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1928}
1929
1930TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1931 NotifyMotionArgs args;
1932 const Point centerPoint = mDevice->getCenterPoint();
1933
1934 // ACTION_DOWN
1935 mDevice->sendDown(centerPoint);
1936 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1937 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1938
1939 // ACTION_POINTER_DOWN (Second slot)
1940 const Point secondPoint = centerPoint + Point(100, 100);
1941 mDevice->sendSlot(SECOND_SLOT);
1942 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1943 mDevice->sendDown(secondPoint + Point(1, 1));
1944 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1945 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1946 args.action);
1947
1948 // ACTION_MOVE (Second slot)
1949 mDevice->sendMove(secondPoint);
1950 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1952
1953 // ACTION_POINTER_UP (Second slot)
arthurhung65600042020-04-30 17:55:40 +08001954 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08001955 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhung65600042020-04-30 17:55:40 +08001956 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08001957 args.action);
1958
1959 // ACTION_UP
1960 mDevice->sendSlot(FIRST_SLOT);
1961 mDevice->sendUp();
1962 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1963 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1964}
1965
1966TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1967 NotifyMotionArgs args;
1968 const Point centerPoint = mDevice->getCenterPoint();
1969
1970 // ACTION_DOWN
arthurhung65600042020-04-30 17:55:40 +08001971 mDevice->sendSlot(FIRST_SLOT);
1972 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001973 mDevice->sendDown(centerPoint);
1974 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1975 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1976
arthurhung65600042020-04-30 17:55:40 +08001977 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001978 const Point secondPoint = centerPoint + Point(100, 100);
1979 mDevice->sendSlot(SECOND_SLOT);
1980 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1981 mDevice->sendDown(secondPoint);
1982 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1983 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1984 args.action);
1985
arthurhung65600042020-04-30 17:55:40 +08001986 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001987 mDevice->sendMove(secondPoint + Point(1, 1));
1988 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1990
arthurhung65600042020-04-30 17:55:40 +08001991 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1992 // a palm event.
1993 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001994 mDevice->sendToolType(MT_TOOL_PALM);
1995 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhung65600042020-04-30 17:55:40 +08001996 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1997 args.action);
1998 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001999
arthurhung65600042020-04-30 17:55:40 +08002000 // Send up to second slot, expect first slot send moving.
2001 mDevice->sendPointerUp();
2002 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002004
arthurhung65600042020-04-30 17:55:40 +08002005 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002006 mDevice->sendSlot(FIRST_SLOT);
2007 mDevice->sendUp();
2008
arthurhung65600042020-04-30 17:55:40 +08002009 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2010 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002011}
2012
Michael Wrightd02c5b62014-02-10 15:10:22 -08002013// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002014class InputDeviceTest : public testing::Test {
2015protected:
2016 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002017 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002018 static const int32_t DEVICE_ID;
2019 static const int32_t DEVICE_GENERATION;
2020 static const int32_t DEVICE_CONTROLLER_NUMBER;
2021 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002022 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002023
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002024 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002025 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002026 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002027 FakeInputReaderContext* mFakeContext;
2028
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002029 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002030
Prabir Pradhan28efc192019-11-05 01:10:04 +00002031 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002032 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002033 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002034 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002035 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2036
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002037 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002038 InputDeviceIdentifier identifier;
2039 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002040 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002041 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2042 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002043 }
2044
Prabir Pradhan28efc192019-11-05 01:10:04 +00002045 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002046 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002047 delete mFakeContext;
2048 mFakeListener.clear();
2049 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002050 }
2051};
2052
2053const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002054const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002055const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002056const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2057const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
2058const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
2059 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002060const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002061
2062TEST_F(InputDeviceTest, ImmutableProperties) {
2063 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002064 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002065 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002066}
2067
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002068TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2069 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002070}
2071
Michael Wrightd02c5b62014-02-10 15:10:22 -08002072TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2073 // Configuration.
2074 InputReaderConfiguration config;
2075 mDevice->configure(ARBITRARY_TIME, &config, 0);
2076
2077 // Reset.
2078 mDevice->reset(ARBITRARY_TIME);
2079
2080 NotifyDeviceResetArgs resetArgs;
2081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2082 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2083 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2084
2085 // Metadata.
2086 ASSERT_TRUE(mDevice->isIgnored());
2087 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2088
2089 InputDeviceInfo info;
2090 mDevice->getDeviceInfo(&info);
2091 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002092 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002093 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2094 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2095
2096 // State queries.
2097 ASSERT_EQ(0, mDevice->getMetaState());
2098
2099 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2100 << "Ignored device should return unknown key code state.";
2101 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2102 << "Ignored device should return unknown scan code state.";
2103 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2104 << "Ignored device should return unknown switch state.";
2105
2106 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2107 uint8_t flags[2] = { 0, 1 };
2108 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2109 << "Ignored device should never mark any key codes.";
2110 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2111 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2112}
2113
2114TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2115 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002116 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002117
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002118 FakeInputMapper& mapper1 =
2119 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002120 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2121 mapper1.setMetaState(AMETA_ALT_ON);
2122 mapper1.addSupportedKeyCode(AKEYCODE_A);
2123 mapper1.addSupportedKeyCode(AKEYCODE_B);
2124 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2125 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2126 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2127 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2128 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002129
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002130 FakeInputMapper& mapper2 =
2131 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002132 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002133
2134 InputReaderConfiguration config;
2135 mDevice->configure(ARBITRARY_TIME, &config, 0);
2136
2137 String8 propertyValue;
2138 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2139 << "Device should have read configuration during configuration phase.";
2140 ASSERT_STREQ("value", propertyValue.string());
2141
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002142 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2143 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002144
2145 // Reset
2146 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002147 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2148 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002149
2150 NotifyDeviceResetArgs resetArgs;
2151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2152 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2153 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2154
2155 // Metadata.
2156 ASSERT_FALSE(mDevice->isIgnored());
2157 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2158
2159 InputDeviceInfo info;
2160 mDevice->getDeviceInfo(&info);
2161 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002162 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002163 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2164 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2165
2166 // State queries.
2167 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2168 << "Should query mappers and combine meta states.";
2169
2170 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2171 << "Should return unknown key code state when source not supported.";
2172 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2173 << "Should return unknown scan code state when source not supported.";
2174 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2175 << "Should return unknown switch state when source not supported.";
2176
2177 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2178 << "Should query mapper when source is supported.";
2179 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2180 << "Should query mapper when source is supported.";
2181 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2182 << "Should query mapper when source is supported.";
2183
2184 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2185 uint8_t flags[4] = { 0, 0, 0, 1 };
2186 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2187 << "Should do nothing when source is unsupported.";
2188 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2189 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2190 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2191 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2192
2193 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2194 << "Should query mapper when source is supported.";
2195 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2196 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2197 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2198 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2199
2200 // Event handling.
2201 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002202 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002203 mDevice->process(&event, 1);
2204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002205 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2206 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002207}
2208
Arthur Hung2c9a3342019-07-23 14:18:59 +08002209// A single input device is associated with a specific display. Check that:
2210// 1. Device is disabled if the viewport corresponding to the associated display is not found
2211// 2. Device is disabled when setEnabled API is called
2212TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002213 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002214
2215 // First Configuration.
2216 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2217
2218 // Device should be enabled by default.
2219 ASSERT_TRUE(mDevice->isEnabled());
2220
2221 // Prepare associated info.
2222 constexpr uint8_t hdmi = 1;
2223 const std::string UNIQUE_ID = "local:1";
2224
2225 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2226 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2227 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2228 // Device should be disabled because it is associated with a specific display via
2229 // input port <-> display port association, but the corresponding display is not found
2230 ASSERT_FALSE(mDevice->isEnabled());
2231
2232 // Prepare displays.
2233 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2234 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2235 ViewportType::VIEWPORT_INTERNAL);
2236 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2237 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2238 ASSERT_TRUE(mDevice->isEnabled());
2239
2240 // Device should be disabled after set disable.
2241 mFakePolicy->addDisabledDevice(mDevice->getId());
2242 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2243 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2244 ASSERT_FALSE(mDevice->isEnabled());
2245
2246 // Device should still be disabled even found the associated display.
2247 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2248 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2249 ASSERT_FALSE(mDevice->isEnabled());
2250}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002251
2252// --- InputMapperTest ---
2253
2254class InputMapperTest : public testing::Test {
2255protected:
2256 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002257 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002258 static const int32_t DEVICE_ID;
2259 static const int32_t DEVICE_GENERATION;
2260 static const int32_t DEVICE_CONTROLLER_NUMBER;
2261 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002262 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002263
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002264 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002265 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002266 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002267 FakeInputReaderContext* mFakeContext;
2268 InputDevice* mDevice;
2269
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002270 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002271 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002272 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002273 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2275 InputDeviceIdentifier identifier;
2276 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002277 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002278 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002280 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002281 }
2282
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002283 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2284
Prabir Pradhan28efc192019-11-05 01:10:04 +00002285 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286 delete mDevice;
2287 delete mFakeContext;
2288 mFakeListener.clear();
2289 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002290 }
2291
2292 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002293 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002294 }
2295
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002296 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002297 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2298 mFakeContext->updatePointerDisplay();
2299 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002300 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2301 }
2302
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002303 template <class T, typename... Args>
2304 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002305 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002306 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002307 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002308 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002309 }
2310
2311 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002312 int32_t orientation, const std::string& uniqueId,
2313 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002314 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002315 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002316 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2317 }
2318
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002319 void clearViewports() {
2320 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002321 }
2322
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002323 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2324 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325 RawEvent event;
2326 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002327 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002328 event.type = type;
2329 event.code = code;
2330 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002331 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332 }
2333
2334 static void assertMotionRange(const InputDeviceInfo& info,
2335 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2336 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002337 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002338 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2339 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2340 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2341 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2342 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2343 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2344 }
2345
2346 static void assertPointerCoords(const PointerCoords& coords,
2347 float x, float y, float pressure, float size,
2348 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2349 float orientation, float distance) {
2350 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2351 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2352 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2353 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2354 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2355 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2356 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2357 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2358 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2359 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2360 }
2361
Michael Wright7a376672020-06-26 20:51:44 +01002362 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363 float actualX, actualY;
Michael Wright7a376672020-06-26 20:51:44 +01002364 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365 ASSERT_NEAR(x, actualX, 1);
2366 ASSERT_NEAR(y, actualY, 1);
2367 }
2368};
2369
2370const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002371const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002372const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002373const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2374const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2375const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002376const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002377
2378// --- SwitchInputMapperTest ---
2379
2380class SwitchInputMapperTest : public InputMapperTest {
2381protected:
2382};
2383
2384TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002385 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002387 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002388}
2389
2390TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002391 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002393 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002394 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002396 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002397 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398}
2399
2400TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002401 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002403 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2404 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2405 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2406 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407
2408 NotifySwitchArgs args;
2409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2410 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002411 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2412 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002413 args.switchMask);
2414 ASSERT_EQ(uint32_t(0), args.policyFlags);
2415}
2416
2417
2418// --- KeyboardInputMapperTest ---
2419
2420class KeyboardInputMapperTest : public InputMapperTest {
2421protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002422 const std::string UNIQUE_ID = "local:0";
2423
2424 void prepareDisplay(int32_t orientation);
2425
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002426 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002427 int32_t originalKeyCode, int32_t rotatedKeyCode,
2428 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002429};
2430
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002431/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2432 * orientation.
2433 */
2434void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2435 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002436 orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002437}
2438
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002439void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002440 int32_t originalScanCode, int32_t originalKeyCode,
2441 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002442 NotifyKeyArgs args;
2443
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002444 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2446 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2447 ASSERT_EQ(originalScanCode, args.scanCode);
2448 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002449 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002451 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2453 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2454 ASSERT_EQ(originalScanCode, args.scanCode);
2455 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002456 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457}
2458
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002460 KeyboardInputMapper& mapper =
2461 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2462 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002464 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465}
2466
2467TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2468 const int32_t USAGE_A = 0x070004;
2469 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002470 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2471 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002473 KeyboardInputMapper& mapper =
2474 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2475 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476
2477 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002478 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002479 NotifyKeyArgs args;
2480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2481 ASSERT_EQ(DEVICE_ID, args.deviceId);
2482 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2483 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2484 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2485 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2486 ASSERT_EQ(KEY_HOME, args.scanCode);
2487 ASSERT_EQ(AMETA_NONE, args.metaState);
2488 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2489 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2490 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2491
2492 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002493 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2495 ASSERT_EQ(DEVICE_ID, args.deviceId);
2496 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2497 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2498 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2499 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2500 ASSERT_EQ(KEY_HOME, args.scanCode);
2501 ASSERT_EQ(AMETA_NONE, args.metaState);
2502 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2503 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2504 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2505
2506 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002507 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2508 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2510 ASSERT_EQ(DEVICE_ID, args.deviceId);
2511 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2512 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2513 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2514 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2515 ASSERT_EQ(0, args.scanCode);
2516 ASSERT_EQ(AMETA_NONE, args.metaState);
2517 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2518 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2519 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2520
2521 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002522 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2523 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2525 ASSERT_EQ(DEVICE_ID, args.deviceId);
2526 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2527 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2528 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2529 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2530 ASSERT_EQ(0, args.scanCode);
2531 ASSERT_EQ(AMETA_NONE, args.metaState);
2532 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2533 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2534 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2535
2536 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002537 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2538 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2540 ASSERT_EQ(DEVICE_ID, args.deviceId);
2541 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2542 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2543 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2544 ASSERT_EQ(0, args.keyCode);
2545 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2546 ASSERT_EQ(AMETA_NONE, args.metaState);
2547 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2548 ASSERT_EQ(0U, args.policyFlags);
2549 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2550
2551 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002552 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2553 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2555 ASSERT_EQ(DEVICE_ID, args.deviceId);
2556 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2557 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2558 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2559 ASSERT_EQ(0, args.keyCode);
2560 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2561 ASSERT_EQ(AMETA_NONE, args.metaState);
2562 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2563 ASSERT_EQ(0U, args.policyFlags);
2564 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2565}
2566
2567TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002568 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2569 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002570
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002571 KeyboardInputMapper& mapper =
2572 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2573 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002574
2575 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002576 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577
2578 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002579 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580 NotifyKeyArgs args;
2581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2582 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002583 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002584 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2585
2586 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002587 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002590 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591
2592 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002593 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2595 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002596 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002597
2598 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002599 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2601 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002602 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2604}
2605
2606TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002607 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2608 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2609 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2610 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002611
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002612 KeyboardInputMapper& mapper =
2613 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2614 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002615
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002616 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2618 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2619 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2620 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2622 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2624 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2625}
2626
2627TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002628 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2629 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2630 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2631 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632
Michael Wrightd02c5b62014-02-10 15:10:22 -08002633 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002634 KeyboardInputMapper& mapper =
2635 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2636 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002638 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002639 ASSERT_NO_FATAL_FAILURE(
2640 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2641 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2642 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2644 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2646 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002648 clearViewports();
2649 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002650 ASSERT_NO_FATAL_FAILURE(
2651 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2652 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2653 AKEYCODE_DPAD_UP, DISPLAY_ID));
2654 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2655 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2656 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2657 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002659 clearViewports();
2660 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002661 ASSERT_NO_FATAL_FAILURE(
2662 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2663 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2664 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2665 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2666 AKEYCODE_DPAD_UP, DISPLAY_ID));
2667 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2668 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002670 clearViewports();
2671 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002672 ASSERT_NO_FATAL_FAILURE(
2673 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2674 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2675 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2676 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2677 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2678 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2679 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680
2681 // Special case: if orientation changes while key is down, we still emit the same keycode
2682 // in the key up as we did in the key down.
2683 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002684 clearViewports();
2685 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002686 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2688 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2689 ASSERT_EQ(KEY_UP, args.scanCode);
2690 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2691
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002692 clearViewports();
2693 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002694 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2696 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2697 ASSERT_EQ(KEY_UP, args.scanCode);
2698 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2699}
2700
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002701TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2702 // If the keyboard is not orientation aware,
2703 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002705
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002706 KeyboardInputMapper& mapper =
2707 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2708 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002709 NotifyKeyArgs args;
2710
2711 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002712 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002714 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2716 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2717
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002718 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002719 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002721 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2723 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2724}
2725
2726TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2727 // If the keyboard is orientation aware,
2728 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002729 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002730
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002731 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002732 KeyboardInputMapper& mapper =
2733 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2734 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002735 NotifyKeyArgs args;
2736
2737 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2738 // ^--- already checked by the previous test
2739
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002740 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002741 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002742 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002744 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2746 ASSERT_EQ(DISPLAY_ID, args.displayId);
2747
2748 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002749 clearViewports();
2750 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002751 UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002752 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002754 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2756 ASSERT_EQ(newDisplayId, args.displayId);
2757}
2758
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002760 KeyboardInputMapper& mapper =
2761 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2762 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002764 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002765 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002767 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002768 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769}
2770
2771TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002772 KeyboardInputMapper& mapper =
2773 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2774 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002776 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002777 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002779 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002780 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781}
2782
2783TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002784 KeyboardInputMapper& mapper =
2785 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2786 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002788 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002789
2790 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2791 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002792 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793 ASSERT_TRUE(flags[0]);
2794 ASSERT_FALSE(flags[1]);
2795}
2796
2797TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002798 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2799 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2800 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2801 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2802 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2803 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002805 KeyboardInputMapper& mapper =
2806 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2807 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808
2809 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002810 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2811 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2812 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002813
2814 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002815 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2816 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002817 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2818 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2819 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002820 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821
2822 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002823 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2824 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002825 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2826 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2827 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002828 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829
2830 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002831 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2832 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002833 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2834 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2835 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002836 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837
2838 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002839 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2840 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002841 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2842 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2843 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002844 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845
2846 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002847 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2848 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002849 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2850 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2851 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002852 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002853
2854 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002855 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2856 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002857 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2858 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2859 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002860 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861}
2862
Arthur Hung2c9a3342019-07-23 14:18:59 +08002863TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2864 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002865 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2866 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2867 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2868 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002869
2870 // keyboard 2.
2871 const std::string USB2 = "USB2";
2872 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002873 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002874 InputDeviceIdentifier identifier;
2875 identifier.name = "KEYBOARD2";
2876 identifier.location = USB2;
2877 std::unique_ptr<InputDevice> device2 =
2878 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002879 identifier);
2880 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2881 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2882 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2883 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2884 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002885
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002886 KeyboardInputMapper& mapper =
2887 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2888 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002889
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002890 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002891 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002892 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002893 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2894 device2->reset(ARBITRARY_TIME);
2895
2896 // Prepared displays and associated info.
2897 constexpr uint8_t hdmi1 = 0;
2898 constexpr uint8_t hdmi2 = 1;
2899 const std::string SECONDARY_UNIQUE_ID = "local:1";
2900
2901 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2902 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2903
2904 // No associated display viewport found, should disable the device.
2905 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2906 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2907 ASSERT_FALSE(device2->isEnabled());
2908
2909 // Prepare second display.
2910 constexpr int32_t newDisplayId = 2;
2911 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2912 UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2913 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2914 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2915 // Default device will reconfigure above, need additional reconfiguration for another device.
2916 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2917 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2918
2919 // Device should be enabled after the associated display is found.
2920 ASSERT_TRUE(mDevice->isEnabled());
2921 ASSERT_TRUE(device2->isEnabled());
2922
2923 // Test pad key events
2924 ASSERT_NO_FATAL_FAILURE(
2925 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2926 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2927 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2928 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2929 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2930 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2931 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2932
2933 ASSERT_NO_FATAL_FAILURE(
2934 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2935 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2936 AKEYCODE_DPAD_RIGHT, newDisplayId));
2937 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2938 AKEYCODE_DPAD_DOWN, newDisplayId));
2939 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2940 AKEYCODE_DPAD_LEFT, newDisplayId));
2941}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002942
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002943// --- KeyboardInputMapperTest_ExternalDevice ---
2944
2945class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2946protected:
2947 virtual void SetUp() override {
2948 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2949 }
2950};
2951
2952TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002953 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2954 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002955
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002956 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2957 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2958 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2959 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002961 KeyboardInputMapper& mapper =
2962 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2963 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002964
2965 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2966 NotifyKeyArgs args;
2967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2968 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2969
2970 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2972 ASSERT_EQ(uint32_t(0), args.policyFlags);
2973
2974 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2976 ASSERT_EQ(uint32_t(0), args.policyFlags);
2977
2978 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2980 ASSERT_EQ(uint32_t(0), args.policyFlags);
2981
2982 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2984 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2985
2986 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2988 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2989}
2990
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002991TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002992 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002993
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002994 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2995 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002997
Powei Fengd041c5d2019-05-03 17:11:33 -07002998 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002999 KeyboardInputMapper& mapper =
3000 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3001 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003002
3003 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3004 NotifyKeyArgs args;
3005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3006 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3007
3008 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3010 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3011
3012 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3014 ASSERT_EQ(uint32_t(0), args.policyFlags);
3015
3016 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3018 ASSERT_EQ(uint32_t(0), args.policyFlags);
3019
3020 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3022 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3023
3024 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3026 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3027}
3028
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029// --- CursorInputMapperTest ---
3030
3031class CursorInputMapperTest : public InputMapperTest {
3032protected:
3033 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3034
Michael Wright7a376672020-06-26 20:51:44 +01003035 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036
Prabir Pradhan28efc192019-11-05 01:10:04 +00003037 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003038 InputMapperTest::SetUp();
3039
Michael Wright7a376672020-06-26 20:51:44 +01003040 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003041 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042 }
3043
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003044 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3045 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003046
3047 void prepareDisplay(int32_t orientation) {
3048 const std::string uniqueId = "local:0";
3049 const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
3050 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3051 orientation, uniqueId, NO_PORT, viewportType);
3052 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053};
3054
3055const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3056
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003057void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3058 int32_t originalY, int32_t rotatedX,
3059 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060 NotifyMotionArgs args;
3061
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003062 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3063 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3064 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3068 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3069 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3070 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3071}
3072
3073TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003075 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003077 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078}
3079
3080TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003082 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085}
3086
3087TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090
3091 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003092 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093
3094 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003095 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3096 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3098 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3099
3100 // When the bounds are set, then there should be a valid motion range.
3101 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3102
3103 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003104 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105
3106 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3107 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3108 1, 800 - 1, 0.0f, 0.0f));
3109 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3110 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3111 2, 480 - 1, 0.0f, 0.0f));
3112 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3113 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3114 0.0f, 1.0f, 0.0f, 0.0f));
3115}
3116
3117TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003119 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120
3121 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003122 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123
3124 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3125 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3126 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3127 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3128 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3129 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3130 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3131 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3132 0.0f, 1.0f, 0.0f, 0.0f));
3133}
3134
3135TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003137 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138
3139 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3140
3141 NotifyMotionArgs args;
3142
3143 // Button press.
3144 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003145 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3146 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3148 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3149 ASSERT_EQ(DEVICE_ID, args.deviceId);
3150 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3151 ASSERT_EQ(uint32_t(0), args.policyFlags);
3152 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3153 ASSERT_EQ(0, args.flags);
3154 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3155 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3156 ASSERT_EQ(0, args.edgeFlags);
3157 ASSERT_EQ(uint32_t(1), args.pointerCount);
3158 ASSERT_EQ(0, args.pointerProperties[0].id);
3159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3161 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3162 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3163 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3164 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3165
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3167 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3168 ASSERT_EQ(DEVICE_ID, args.deviceId);
3169 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3170 ASSERT_EQ(uint32_t(0), args.policyFlags);
3171 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3172 ASSERT_EQ(0, args.flags);
3173 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3174 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3175 ASSERT_EQ(0, args.edgeFlags);
3176 ASSERT_EQ(uint32_t(1), args.pointerCount);
3177 ASSERT_EQ(0, args.pointerProperties[0].id);
3178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3180 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3181 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3182 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3183 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3184
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003186 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3187 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3189 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3190 ASSERT_EQ(DEVICE_ID, args.deviceId);
3191 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3192 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003193 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3194 ASSERT_EQ(0, args.flags);
3195 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3196 ASSERT_EQ(0, args.buttonState);
3197 ASSERT_EQ(0, args.edgeFlags);
3198 ASSERT_EQ(uint32_t(1), args.pointerCount);
3199 ASSERT_EQ(0, args.pointerProperties[0].id);
3200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3202 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3203 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3204 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3205 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3206
3207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3208 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3209 ASSERT_EQ(DEVICE_ID, args.deviceId);
3210 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3211 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3213 ASSERT_EQ(0, args.flags);
3214 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3215 ASSERT_EQ(0, args.buttonState);
3216 ASSERT_EQ(0, args.edgeFlags);
3217 ASSERT_EQ(uint32_t(1), args.pointerCount);
3218 ASSERT_EQ(0, args.pointerProperties[0].id);
3219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3221 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3222 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3223 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3224 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3225}
3226
3227TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003229 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230
3231 NotifyMotionArgs args;
3232
3233 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003234 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3235 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3238 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3239 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3240
3241 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003242 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3243 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3247 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3248}
3249
3250TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253
3254 NotifyMotionArgs args;
3255
3256 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003257 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3258 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3260 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3262 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3263
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3267 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3268
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003270 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3271 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3275 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3276
3277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3280 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3281}
3282
3283TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003285 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286
3287 NotifyMotionArgs args;
3288
3289 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003290 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3291 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3292 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3293 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3297 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3298 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3299
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3303 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3304 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3305
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003307 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3308 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3309 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3313 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3314 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3315
3316 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003317 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3318 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003320 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3322 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3323
3324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3327 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3328}
3329
3330TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003332 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003334 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3336 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3337 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3338 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3339 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3340 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3341 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3343}
3344
3345TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 addConfigurationProperty("cursor.mode", "navigation");
3347 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003348 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003350 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3352 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3353 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3354 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3355 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3356 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3357 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3358 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3359
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003360 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3362 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3363 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3364 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3365 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3366 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3367 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3368 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3369
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003370 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3374 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3375 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3376 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3377 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3378 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3379
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003380 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3382 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3383 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3384 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3385 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3386 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3387 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3388 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3389}
3390
3391TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003393 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394
3395 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3396 mFakePointerController->setPosition(100, 200);
3397 mFakePointerController->setButtonState(0);
3398
3399 NotifyMotionArgs motionArgs;
3400 NotifyKeyArgs keyArgs;
3401
3402 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003403 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3404 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3406 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3407 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3408 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3410 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3411
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3413 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3414 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3415 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3417 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3418
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003419 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3420 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423 ASSERT_EQ(0, motionArgs.buttonState);
3424 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3426 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3427
3428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003429 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430 ASSERT_EQ(0, motionArgs.buttonState);
3431 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3433 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3434
3435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -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));
3441
3442 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003443 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3444 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3445 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3447 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3448 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3449 motionArgs.buttonState);
3450 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3451 mFakePointerController->getButtonState());
3452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3453 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3454
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3457 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3458 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3459 mFakePointerController->getButtonState());
3460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3461 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3462
3463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3465 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3466 motionArgs.buttonState);
3467 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3468 mFakePointerController->getButtonState());
3469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3470 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3471
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003472 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3473 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003475 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3477 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3479 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3480
3481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003483 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3484 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3486 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3487
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003488 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3489 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3492 ASSERT_EQ(0, motionArgs.buttonState);
3493 ASSERT_EQ(0, mFakePointerController->getButtonState());
3494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3495 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 -08003496 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3497 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003498
3499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500 ASSERT_EQ(0, motionArgs.buttonState);
3501 ASSERT_EQ(0, mFakePointerController->getButtonState());
3502 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3504 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 -08003505
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3507 ASSERT_EQ(0, motionArgs.buttonState);
3508 ASSERT_EQ(0, mFakePointerController->getButtonState());
3509 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3511 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3512
3513 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003514 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3515 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3517 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3518 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003519
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003521 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3523 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3525 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3526
3527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3528 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3529 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3530 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3532 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3533
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003534 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3535 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538 ASSERT_EQ(0, motionArgs.buttonState);
3539 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3541 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3542
3543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003545 ASSERT_EQ(0, motionArgs.buttonState);
3546 ASSERT_EQ(0, mFakePointerController->getButtonState());
3547
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3549 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3551 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3552 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3553
3554 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003555 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3556 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3558 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3559 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003560
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003562 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3564 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3566 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3567
3568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3570 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3571 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3573 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3574
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003575 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3576 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003578 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579 ASSERT_EQ(0, motionArgs.buttonState);
3580 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3582 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 -08003583
3584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3585 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3586 ASSERT_EQ(0, motionArgs.buttonState);
3587 ASSERT_EQ(0, mFakePointerController->getButtonState());
3588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3589 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3590
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3592 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3593 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3594
3595 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003596 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3597 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3599 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3600 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003601
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3605 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3607 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3608
3609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3610 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3611 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3612 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3614 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3615
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003616 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3617 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 ASSERT_EQ(0, motionArgs.buttonState);
3621 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3623 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 -08003624
3625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3626 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3627 ASSERT_EQ(0, motionArgs.buttonState);
3628 ASSERT_EQ(0, mFakePointerController->getButtonState());
3629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3630 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3631
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3633 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3634 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3635
3636 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003637 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3638 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3640 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3641 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003642
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003644 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3646 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3648 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3649
3650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3651 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3652 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3653 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3655 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3656
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003657 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3658 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661 ASSERT_EQ(0, motionArgs.buttonState);
3662 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3664 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 -08003665
3666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3668 ASSERT_EQ(0, motionArgs.buttonState);
3669 ASSERT_EQ(0, mFakePointerController->getButtonState());
3670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3671 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3672
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3674 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3675 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3676}
3677
3678TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003680 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681
3682 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3683 mFakePointerController->setPosition(100, 200);
3684 mFakePointerController->setButtonState(0);
3685
3686 NotifyMotionArgs args;
3687
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003688 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3689 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3690 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003692 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3693 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3695 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright7a376672020-06-26 20:51:44 +01003696 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003697}
3698
3699TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003700 addConfigurationProperty("cursor.mode", "pointer");
3701 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003702 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003703
3704 NotifyDeviceResetArgs resetArgs;
3705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3706 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3707 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3708
3709 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3710 mFakePointerController->setPosition(100, 200);
3711 mFakePointerController->setButtonState(0);
3712
3713 NotifyMotionArgs args;
3714
3715 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003716 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3717 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3718 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3720 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3723 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright7a376672020-06-26 20:51:44 +01003724 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003725
3726 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003727 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3728 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3730 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3731 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3733 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3735 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3736 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3738 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3739
3740 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003741 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3742 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3744 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3745 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3747 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3749 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3750 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3752 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3753
3754 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003755 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3756 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3757 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3759 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3762 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright7a376672020-06-26 20:51:44 +01003763 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003764
3765 // Disable pointer capture and check that the device generation got bumped
3766 // and events are generated the usual way.
3767 const uint32_t generation = mFakeContext->getGeneration();
3768 mFakePolicy->setPointerCapture(false);
3769 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3770 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3771
3772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3773 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3774 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3775
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003776 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3777 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3778 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3780 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3783 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright7a376672020-06-26 20:51:44 +01003784 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003785}
3786
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003787TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003788 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003789
Garfield Tan888a6a42020-01-09 11:39:16 -08003790 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003791 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003792 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3793 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3794 SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3795 ViewportType::VIEWPORT_EXTERNAL);
3796 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3797 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3798
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003799 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3800 mFakePointerController->setPosition(100, 200);
3801 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003802
3803 NotifyMotionArgs args;
3804 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3805 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3806 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3808 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3809 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3811 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright7a376672020-06-26 20:51:44 +01003812 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003813 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3814}
3815
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816// --- TouchInputMapperTest ---
3817
3818class TouchInputMapperTest : public InputMapperTest {
3819protected:
3820 static const int32_t RAW_X_MIN;
3821 static const int32_t RAW_X_MAX;
3822 static const int32_t RAW_Y_MIN;
3823 static const int32_t RAW_Y_MAX;
3824 static const int32_t RAW_TOUCH_MIN;
3825 static const int32_t RAW_TOUCH_MAX;
3826 static const int32_t RAW_TOOL_MIN;
3827 static const int32_t RAW_TOOL_MAX;
3828 static const int32_t RAW_PRESSURE_MIN;
3829 static const int32_t RAW_PRESSURE_MAX;
3830 static const int32_t RAW_ORIENTATION_MIN;
3831 static const int32_t RAW_ORIENTATION_MAX;
3832 static const int32_t RAW_DISTANCE_MIN;
3833 static const int32_t RAW_DISTANCE_MAX;
3834 static const int32_t RAW_TILT_MIN;
3835 static const int32_t RAW_TILT_MAX;
3836 static const int32_t RAW_ID_MIN;
3837 static const int32_t RAW_ID_MAX;
3838 static const int32_t RAW_SLOT_MIN;
3839 static const int32_t RAW_SLOT_MAX;
3840 static const float X_PRECISION;
3841 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003842 static const float X_PRECISION_VIRTUAL;
3843 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003844
3845 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003846 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847
3848 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3849
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003850 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003851 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003852
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853 enum Axes {
3854 POSITION = 1 << 0,
3855 TOUCH = 1 << 1,
3856 TOOL = 1 << 2,
3857 PRESSURE = 1 << 3,
3858 ORIENTATION = 1 << 4,
3859 MINOR = 1 << 5,
3860 ID = 1 << 6,
3861 DISTANCE = 1 << 7,
3862 TILT = 1 << 8,
3863 SLOT = 1 << 9,
3864 TOOL_TYPE = 1 << 10,
3865 };
3866
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003867 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3868 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003869 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003871 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872 int32_t toRawX(float displayX);
3873 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003874 float toCookedX(float rawX, float rawY);
3875 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003877 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003879 float toDisplayY(int32_t rawY, int32_t displayHeight);
3880
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881};
3882
3883const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3884const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3885const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3886const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3887const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3888const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3889const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3890const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003891const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3892const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3894const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3895const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3896const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3897const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3898const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3899const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3900const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3901const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3902const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3903const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3904const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003905const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3906 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3907const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3908 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003909const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3910 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911
3912const float TouchInputMapperTest::GEOMETRIC_SCALE =
3913 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3914 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3915
3916const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3917 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3918 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3919};
3920
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003921void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003922 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003923 UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3924}
3925
3926void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3927 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3928 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929}
3930
Santos Cordonfa5cf462017-04-05 10:37:00 -07003931void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003932 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3933 VIRTUAL_DISPLAY_HEIGHT, orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003934 VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003935}
3936
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003938 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3939 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3940 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3941 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942}
3943
Jason Gerecke489fda82012-09-07 17:19:40 -07003944void TouchInputMapperTest::prepareLocationCalibration() {
3945 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3946}
3947
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948int32_t TouchInputMapperTest::toRawX(float displayX) {
3949 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3950}
3951
3952int32_t TouchInputMapperTest::toRawY(float displayY) {
3953 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3954}
3955
Jason Gerecke489fda82012-09-07 17:19:40 -07003956float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3957 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3958 return rawX;
3959}
3960
3961float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3962 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3963 return rawY;
3964}
3965
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003967 return toDisplayX(rawX, DISPLAY_WIDTH);
3968}
3969
3970float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3971 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003972}
3973
3974float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003975 return toDisplayY(rawY, DISPLAY_HEIGHT);
3976}
3977
3978float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3979 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980}
3981
3982
3983// --- SingleTouchInputMapperTest ---
3984
3985class SingleTouchInputMapperTest : public TouchInputMapperTest {
3986protected:
3987 void prepareButtons();
3988 void prepareAxes(int axes);
3989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003990 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3991 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3992 void processUp(SingleTouchInputMapper& mappery);
3993 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3994 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3995 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3996 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3997 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3998 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999};
4000
4001void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004002 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003}
4004
4005void SingleTouchInputMapperTest::prepareAxes(int axes) {
4006 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004007 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4008 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004009 }
4010 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004011 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4012 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013 }
4014 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004015 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4016 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 }
4018 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004019 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4020 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 }
4022 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004023 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4024 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025 }
4026}
4027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004028void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004029 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4030 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4031 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032}
4033
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004034void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004035 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4036 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037}
4038
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004039void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004040 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041}
4042
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004043void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004044 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045}
4046
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004047void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4048 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004049 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050}
4051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004052void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004053 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054}
4055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004056void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4057 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004058 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4059 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060}
4061
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004062void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4063 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004064 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065}
4066
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004067void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004068 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069}
4070
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 prepareButtons();
4073 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004074 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004076 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077}
4078
4079TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004080 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4081 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004082 prepareButtons();
4083 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004084 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004086 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087}
4088
4089TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090 prepareButtons();
4091 prepareAxes(POSITION);
4092 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004093 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004095 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096}
4097
4098TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 prepareButtons();
4100 prepareAxes(POSITION);
4101 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004102 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004104 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105}
4106
4107TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108 addConfigurationProperty("touch.deviceType", "touchScreen");
4109 prepareDisplay(DISPLAY_ORIENTATION_0);
4110 prepareButtons();
4111 prepareAxes(POSITION);
4112 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004113 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114
4115 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004116 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117
4118 // Virtual key is down.
4119 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4120 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4121 processDown(mapper, x, y);
4122 processSync(mapper);
4123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004125 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004126
4127 // Virtual key is up.
4128 processUp(mapper);
4129 processSync(mapper);
4130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004132 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133}
4134
4135TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136 addConfigurationProperty("touch.deviceType", "touchScreen");
4137 prepareDisplay(DISPLAY_ORIENTATION_0);
4138 prepareButtons();
4139 prepareAxes(POSITION);
4140 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004141 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142
4143 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004144 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145
4146 // Virtual key is down.
4147 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4148 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4149 processDown(mapper, x, y);
4150 processSync(mapper);
4151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4152
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004153 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154
4155 // Virtual key is up.
4156 processUp(mapper);
4157 processSync(mapper);
4158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004160 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161}
4162
4163TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164 addConfigurationProperty("touch.deviceType", "touchScreen");
4165 prepareDisplay(DISPLAY_ORIENTATION_0);
4166 prepareButtons();
4167 prepareAxes(POSITION);
4168 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004169 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170
4171 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4172 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004173 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174 ASSERT_TRUE(flags[0]);
4175 ASSERT_FALSE(flags[1]);
4176}
4177
4178TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 addConfigurationProperty("touch.deviceType", "touchScreen");
4180 prepareDisplay(DISPLAY_ORIENTATION_0);
4181 prepareButtons();
4182 prepareAxes(POSITION);
4183 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004184 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185
4186 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4187
4188 NotifyKeyArgs args;
4189
4190 // Press virtual key.
4191 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4192 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4193 processDown(mapper, x, y);
4194 processSync(mapper);
4195
4196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4197 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4198 ASSERT_EQ(DEVICE_ID, args.deviceId);
4199 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4200 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4201 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4202 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4203 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4204 ASSERT_EQ(KEY_HOME, args.scanCode);
4205 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4206 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4207
4208 // Release virtual key.
4209 processUp(mapper);
4210 processSync(mapper);
4211
4212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4213 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4214 ASSERT_EQ(DEVICE_ID, args.deviceId);
4215 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4216 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4217 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4218 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4219 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4220 ASSERT_EQ(KEY_HOME, args.scanCode);
4221 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4222 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4223
4224 // Should not have sent any motions.
4225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4226}
4227
4228TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 addConfigurationProperty("touch.deviceType", "touchScreen");
4230 prepareDisplay(DISPLAY_ORIENTATION_0);
4231 prepareButtons();
4232 prepareAxes(POSITION);
4233 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004234 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235
4236 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4237
4238 NotifyKeyArgs keyArgs;
4239
4240 // Press virtual key.
4241 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4242 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4243 processDown(mapper, x, y);
4244 processSync(mapper);
4245
4246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4247 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4248 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4249 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4250 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4251 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4252 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4253 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4254 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4255 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4256 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4257
4258 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4259 // into the display area.
4260 y -= 100;
4261 processMove(mapper, x, y);
4262 processSync(mapper);
4263
4264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4265 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4266 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4267 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4268 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4269 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4270 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4271 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4272 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4273 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4274 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4275 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4276
4277 NotifyMotionArgs motionArgs;
4278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4280 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4281 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4282 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4283 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4284 ASSERT_EQ(0, motionArgs.flags);
4285 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4286 ASSERT_EQ(0, motionArgs.buttonState);
4287 ASSERT_EQ(0, motionArgs.edgeFlags);
4288 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4289 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4292 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4293 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4294 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4295 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4296
4297 // Keep moving out of bounds. Should generate a pointer move.
4298 y -= 50;
4299 processMove(mapper, x, y);
4300 processSync(mapper);
4301
4302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4303 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4304 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4305 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4306 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4308 ASSERT_EQ(0, motionArgs.flags);
4309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4310 ASSERT_EQ(0, motionArgs.buttonState);
4311 ASSERT_EQ(0, motionArgs.edgeFlags);
4312 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4313 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4316 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4317 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4318 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4320
4321 // Release out of bounds. Should generate a pointer up.
4322 processUp(mapper);
4323 processSync(mapper);
4324
4325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4327 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4328 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4329 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4331 ASSERT_EQ(0, motionArgs.flags);
4332 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4333 ASSERT_EQ(0, motionArgs.buttonState);
4334 ASSERT_EQ(0, motionArgs.edgeFlags);
4335 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4336 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4339 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4340 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4341 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4342 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4343
4344 // Should not have sent any more keys or motions.
4345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4347}
4348
4349TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 addConfigurationProperty("touch.deviceType", "touchScreen");
4351 prepareDisplay(DISPLAY_ORIENTATION_0);
4352 prepareButtons();
4353 prepareAxes(POSITION);
4354 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004355 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356
4357 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4358
4359 NotifyMotionArgs motionArgs;
4360
4361 // Initially go down out of bounds.
4362 int32_t x = -10;
4363 int32_t y = -10;
4364 processDown(mapper, x, y);
4365 processSync(mapper);
4366
4367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4368
4369 // Move into the display area. Should generate a pointer down.
4370 x = 50;
4371 y = 75;
4372 processMove(mapper, x, y);
4373 processSync(mapper);
4374
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4376 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4377 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4378 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4379 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4380 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4381 ASSERT_EQ(0, motionArgs.flags);
4382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4383 ASSERT_EQ(0, motionArgs.buttonState);
4384 ASSERT_EQ(0, motionArgs.edgeFlags);
4385 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4386 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4389 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4390 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4391 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4392 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4393
4394 // Release. Should generate a pointer up.
4395 processUp(mapper);
4396 processSync(mapper);
4397
4398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4399 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4400 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4401 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4402 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4403 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4404 ASSERT_EQ(0, motionArgs.flags);
4405 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4406 ASSERT_EQ(0, motionArgs.buttonState);
4407 ASSERT_EQ(0, motionArgs.edgeFlags);
4408 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4409 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4412 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4413 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4414 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4416
4417 // Should not have sent any more keys or motions.
4418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4420}
4421
Santos Cordonfa5cf462017-04-05 10:37:00 -07004422TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004423 addConfigurationProperty("touch.deviceType", "touchScreen");
4424 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4425
4426 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4427 prepareButtons();
4428 prepareAxes(POSITION);
4429 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004430 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004431
4432 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4433
4434 NotifyMotionArgs motionArgs;
4435
4436 // Down.
4437 int32_t x = 100;
4438 int32_t y = 125;
4439 processDown(mapper, x, y);
4440 processSync(mapper);
4441
4442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4443 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4444 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4445 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4446 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4447 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4448 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4449 ASSERT_EQ(0, motionArgs.flags);
4450 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4451 ASSERT_EQ(0, motionArgs.buttonState);
4452 ASSERT_EQ(0, motionArgs.edgeFlags);
4453 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4454 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4457 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4458 1, 0, 0, 0, 0, 0, 0, 0));
4459 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4460 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4462
4463 // Move.
4464 x += 50;
4465 y += 75;
4466 processMove(mapper, x, y);
4467 processSync(mapper);
4468
4469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4470 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4471 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4472 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4473 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4474 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4476 ASSERT_EQ(0, motionArgs.flags);
4477 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4478 ASSERT_EQ(0, motionArgs.buttonState);
4479 ASSERT_EQ(0, motionArgs.edgeFlags);
4480 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4484 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4485 1, 0, 0, 0, 0, 0, 0, 0));
4486 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4487 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4488 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4489
4490 // Up.
4491 processUp(mapper);
4492 processSync(mapper);
4493
4494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4495 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4496 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4497 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4498 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4499 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4500 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4501 ASSERT_EQ(0, motionArgs.flags);
4502 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4503 ASSERT_EQ(0, motionArgs.buttonState);
4504 ASSERT_EQ(0, motionArgs.edgeFlags);
4505 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4506 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4507 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4509 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4510 1, 0, 0, 0, 0, 0, 0, 0));
4511 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4512 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4513 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4514
4515 // Should not have sent any more keys or motions.
4516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4518}
4519
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 addConfigurationProperty("touch.deviceType", "touchScreen");
4522 prepareDisplay(DISPLAY_ORIENTATION_0);
4523 prepareButtons();
4524 prepareAxes(POSITION);
4525 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004526 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527
4528 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4529
4530 NotifyMotionArgs motionArgs;
4531
4532 // Down.
4533 int32_t x = 100;
4534 int32_t y = 125;
4535 processDown(mapper, x, y);
4536 processSync(mapper);
4537
4538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4539 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4540 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4541 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4542 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4544 ASSERT_EQ(0, motionArgs.flags);
4545 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4546 ASSERT_EQ(0, motionArgs.buttonState);
4547 ASSERT_EQ(0, motionArgs.edgeFlags);
4548 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4549 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4552 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4553 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4554 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4555 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4556
4557 // Move.
4558 x += 50;
4559 y += 75;
4560 processMove(mapper, x, y);
4561 processSync(mapper);
4562
4563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4564 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4565 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4566 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4567 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4569 ASSERT_EQ(0, motionArgs.flags);
4570 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4571 ASSERT_EQ(0, motionArgs.buttonState);
4572 ASSERT_EQ(0, motionArgs.edgeFlags);
4573 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4574 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4577 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4578 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4579 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4580 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4581
4582 // Up.
4583 processUp(mapper);
4584 processSync(mapper);
4585
4586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4587 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4588 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4589 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4590 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4591 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4592 ASSERT_EQ(0, motionArgs.flags);
4593 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4594 ASSERT_EQ(0, motionArgs.buttonState);
4595 ASSERT_EQ(0, motionArgs.edgeFlags);
4596 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4597 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4600 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4601 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4602 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4603 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4604
4605 // Should not have sent any more keys or motions.
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4608}
4609
4610TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 addConfigurationProperty("touch.deviceType", "touchScreen");
4612 prepareButtons();
4613 prepareAxes(POSITION);
4614 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004615 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616
4617 NotifyMotionArgs args;
4618
4619 // Rotation 90.
4620 prepareDisplay(DISPLAY_ORIENTATION_90);
4621 processDown(mapper, toRawX(50), toRawY(75));
4622 processSync(mapper);
4623
4624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4625 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4626 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4627
4628 processUp(mapper);
4629 processSync(mapper);
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4631}
4632
4633TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 addConfigurationProperty("touch.deviceType", "touchScreen");
4635 prepareButtons();
4636 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004637 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638
4639 NotifyMotionArgs args;
4640
4641 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004642 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 prepareDisplay(DISPLAY_ORIENTATION_0);
4644 processDown(mapper, toRawX(50), toRawY(75));
4645 processSync(mapper);
4646
4647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4648 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4649 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4650
4651 processUp(mapper);
4652 processSync(mapper);
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4654
4655 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004656 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 prepareDisplay(DISPLAY_ORIENTATION_90);
4658 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4659 processSync(mapper);
4660
4661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4662 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4663 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4664
4665 processUp(mapper);
4666 processSync(mapper);
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4668
4669 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004670 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671 prepareDisplay(DISPLAY_ORIENTATION_180);
4672 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4673 processSync(mapper);
4674
4675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4676 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4677 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4678
4679 processUp(mapper);
4680 processSync(mapper);
4681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4682
4683 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004684 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 prepareDisplay(DISPLAY_ORIENTATION_270);
4686 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4687 processSync(mapper);
4688
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4690 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4691 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4692
4693 processUp(mapper);
4694 processSync(mapper);
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4696}
4697
4698TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 addConfigurationProperty("touch.deviceType", "touchScreen");
4700 prepareDisplay(DISPLAY_ORIENTATION_0);
4701 prepareButtons();
4702 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004703 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704
4705 // These calculations are based on the input device calibration documentation.
4706 int32_t rawX = 100;
4707 int32_t rawY = 200;
4708 int32_t rawPressure = 10;
4709 int32_t rawToolMajor = 12;
4710 int32_t rawDistance = 2;
4711 int32_t rawTiltX = 30;
4712 int32_t rawTiltY = 110;
4713
4714 float x = toDisplayX(rawX);
4715 float y = toDisplayY(rawY);
4716 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4717 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4718 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4719 float distance = float(rawDistance);
4720
4721 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4722 float tiltScale = M_PI / 180;
4723 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4724 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4725 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4726 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4727
4728 processDown(mapper, rawX, rawY);
4729 processPressure(mapper, rawPressure);
4730 processToolMajor(mapper, rawToolMajor);
4731 processDistance(mapper, rawDistance);
4732 processTilt(mapper, rawTiltX, rawTiltY);
4733 processSync(mapper);
4734
4735 NotifyMotionArgs args;
4736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4738 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4739 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4740}
4741
Jason Gerecke489fda82012-09-07 17:19:40 -07004742TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004743 addConfigurationProperty("touch.deviceType", "touchScreen");
4744 prepareDisplay(DISPLAY_ORIENTATION_0);
4745 prepareLocationCalibration();
4746 prepareButtons();
4747 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004748 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004749
4750 int32_t rawX = 100;
4751 int32_t rawY = 200;
4752
4753 float x = toDisplayX(toCookedX(rawX, rawY));
4754 float y = toDisplayY(toCookedY(rawX, rawY));
4755
4756 processDown(mapper, rawX, rawY);
4757 processSync(mapper);
4758
4759 NotifyMotionArgs args;
4760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4762 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4763}
4764
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766 addConfigurationProperty("touch.deviceType", "touchScreen");
4767 prepareDisplay(DISPLAY_ORIENTATION_0);
4768 prepareButtons();
4769 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004770 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771
4772 NotifyMotionArgs motionArgs;
4773 NotifyKeyArgs keyArgs;
4774
4775 processDown(mapper, 100, 200);
4776 processSync(mapper);
4777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4779 ASSERT_EQ(0, motionArgs.buttonState);
4780
4781 // press BTN_LEFT, release BTN_LEFT
4782 processKey(mapper, BTN_LEFT, 1);
4783 processSync(mapper);
4784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4786 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4787
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4789 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4790 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4791
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792 processKey(mapper, BTN_LEFT, 0);
4793 processSync(mapper);
4794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004795 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004797
4798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004800 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801
4802 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4803 processKey(mapper, BTN_RIGHT, 1);
4804 processKey(mapper, BTN_MIDDLE, 1);
4805 processSync(mapper);
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4808 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4809 motionArgs.buttonState);
4810
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4812 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4813 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4814
4815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4816 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4817 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4818 motionArgs.buttonState);
4819
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820 processKey(mapper, BTN_RIGHT, 0);
4821 processSync(mapper);
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004823 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004825
4826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004828 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829
4830 processKey(mapper, BTN_MIDDLE, 0);
4831 processSync(mapper);
4832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004835
4836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004838 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839
4840 // press BTN_BACK, release BTN_BACK
4841 processKey(mapper, BTN_BACK, 1);
4842 processSync(mapper);
4843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4844 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4845 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004846
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004849 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4850
4851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4853 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854
4855 processKey(mapper, BTN_BACK, 0);
4856 processSync(mapper);
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004860
4861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004863 ASSERT_EQ(0, motionArgs.buttonState);
4864
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4866 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4867 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4868
4869 // press BTN_SIDE, release BTN_SIDE
4870 processKey(mapper, BTN_SIDE, 1);
4871 processSync(mapper);
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4873 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4874 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004875
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004878 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4879
4880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4881 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4882 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883
4884 processKey(mapper, BTN_SIDE, 0);
4885 processSync(mapper);
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004889
4890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004892 ASSERT_EQ(0, motionArgs.buttonState);
4893
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4895 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4896 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4897
4898 // press BTN_FORWARD, release BTN_FORWARD
4899 processKey(mapper, BTN_FORWARD, 1);
4900 processSync(mapper);
4901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4902 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4903 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004904
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004907 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4908
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4911 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912
4913 processKey(mapper, BTN_FORWARD, 0);
4914 processSync(mapper);
4915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004916 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004918
4919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004921 ASSERT_EQ(0, motionArgs.buttonState);
4922
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4924 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4925 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4926
4927 // press BTN_EXTRA, release BTN_EXTRA
4928 processKey(mapper, BTN_EXTRA, 1);
4929 processSync(mapper);
4930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4931 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4932 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004933
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004936 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4937
4938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4939 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4940 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941
4942 processKey(mapper, BTN_EXTRA, 0);
4943 processSync(mapper);
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004947
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004950 ASSERT_EQ(0, motionArgs.buttonState);
4951
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4953 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4954 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4955
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4957
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958 // press BTN_STYLUS, release BTN_STYLUS
4959 processKey(mapper, BTN_STYLUS, 1);
4960 processSync(mapper);
4961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004963 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4964
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4967 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968
4969 processKey(mapper, BTN_STYLUS, 0);
4970 processSync(mapper);
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004972 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004974
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004977 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978
4979 // press BTN_STYLUS2, release BTN_STYLUS2
4980 processKey(mapper, BTN_STYLUS2, 1);
4981 processSync(mapper);
4982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004984 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4985
4986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4988 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989
4990 processKey(mapper, BTN_STYLUS2, 0);
4991 processSync(mapper);
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004993 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004995
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004998 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999
5000 // release touch
5001 processUp(mapper);
5002 processSync(mapper);
5003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5004 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5005 ASSERT_EQ(0, motionArgs.buttonState);
5006}
5007
5008TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009 addConfigurationProperty("touch.deviceType", "touchScreen");
5010 prepareDisplay(DISPLAY_ORIENTATION_0);
5011 prepareButtons();
5012 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005013 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014
5015 NotifyMotionArgs motionArgs;
5016
5017 // default tool type is finger
5018 processDown(mapper, 100, 200);
5019 processSync(mapper);
5020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5021 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5023
5024 // eraser
5025 processKey(mapper, BTN_TOOL_RUBBER, 1);
5026 processSync(mapper);
5027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5030
5031 // stylus
5032 processKey(mapper, BTN_TOOL_RUBBER, 0);
5033 processKey(mapper, BTN_TOOL_PEN, 1);
5034 processSync(mapper);
5035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5038
5039 // brush
5040 processKey(mapper, BTN_TOOL_PEN, 0);
5041 processKey(mapper, BTN_TOOL_BRUSH, 1);
5042 processSync(mapper);
5043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5046
5047 // pencil
5048 processKey(mapper, BTN_TOOL_BRUSH, 0);
5049 processKey(mapper, BTN_TOOL_PENCIL, 1);
5050 processSync(mapper);
5051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5054
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005055 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005056 processKey(mapper, BTN_TOOL_PENCIL, 0);
5057 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5058 processSync(mapper);
5059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5062
5063 // mouse
5064 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5065 processKey(mapper, BTN_TOOL_MOUSE, 1);
5066 processSync(mapper);
5067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5070
5071 // lens
5072 processKey(mapper, BTN_TOOL_MOUSE, 0);
5073 processKey(mapper, BTN_TOOL_LENS, 1);
5074 processSync(mapper);
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5078
5079 // double-tap
5080 processKey(mapper, BTN_TOOL_LENS, 0);
5081 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5082 processSync(mapper);
5083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5086
5087 // triple-tap
5088 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5089 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5090 processSync(mapper);
5091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5094
5095 // quad-tap
5096 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5097 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5098 processSync(mapper);
5099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5102
5103 // finger
5104 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5105 processKey(mapper, BTN_TOOL_FINGER, 1);
5106 processSync(mapper);
5107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5110
5111 // stylus trumps finger
5112 processKey(mapper, BTN_TOOL_PEN, 1);
5113 processSync(mapper);
5114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5117
5118 // eraser trumps stylus
5119 processKey(mapper, BTN_TOOL_RUBBER, 1);
5120 processSync(mapper);
5121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5124
5125 // mouse trumps eraser
5126 processKey(mapper, BTN_TOOL_MOUSE, 1);
5127 processSync(mapper);
5128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5131
5132 // back to default tool type
5133 processKey(mapper, BTN_TOOL_MOUSE, 0);
5134 processKey(mapper, BTN_TOOL_RUBBER, 0);
5135 processKey(mapper, BTN_TOOL_PEN, 0);
5136 processKey(mapper, BTN_TOOL_FINGER, 0);
5137 processSync(mapper);
5138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5141}
5142
5143TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144 addConfigurationProperty("touch.deviceType", "touchScreen");
5145 prepareDisplay(DISPLAY_ORIENTATION_0);
5146 prepareButtons();
5147 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005148 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005149 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150
5151 NotifyMotionArgs motionArgs;
5152
5153 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5154 processKey(mapper, BTN_TOOL_FINGER, 1);
5155 processMove(mapper, 100, 200);
5156 processSync(mapper);
5157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5158 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5160 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5161
5162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5163 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5165 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5166
5167 // move a little
5168 processMove(mapper, 150, 250);
5169 processSync(mapper);
5170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5171 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5173 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5174
5175 // down when BTN_TOUCH is pressed, pressure defaults to 1
5176 processKey(mapper, BTN_TOUCH, 1);
5177 processSync(mapper);
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5179 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5181 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5182
5183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5184 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5186 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5187
5188 // up when BTN_TOUCH is released, hover restored
5189 processKey(mapper, BTN_TOUCH, 0);
5190 processSync(mapper);
5191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5192 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5194 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5195
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5199 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5200
5201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5202 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5204 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5205
5206 // exit hover when pointer goes away
5207 processKey(mapper, BTN_TOOL_FINGER, 0);
5208 processSync(mapper);
5209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5210 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5212 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5213}
5214
5215TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216 addConfigurationProperty("touch.deviceType", "touchScreen");
5217 prepareDisplay(DISPLAY_ORIENTATION_0);
5218 prepareButtons();
5219 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005220 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221
5222 NotifyMotionArgs motionArgs;
5223
5224 // initially hovering because pressure is 0
5225 processDown(mapper, 100, 200);
5226 processPressure(mapper, 0);
5227 processSync(mapper);
5228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5229 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5231 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5232
5233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5234 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5236 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5237
5238 // move a little
5239 processMove(mapper, 150, 250);
5240 processSync(mapper);
5241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5242 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5244 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5245
5246 // down when pressure is non-zero
5247 processPressure(mapper, RAW_PRESSURE_MAX);
5248 processSync(mapper);
5249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5250 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5252 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5253
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5255 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5257 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5258
5259 // up when pressure becomes 0, hover restored
5260 processPressure(mapper, 0);
5261 processSync(mapper);
5262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5263 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5265 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5266
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5270 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5271
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5275 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5276
5277 // exit hover when pointer goes away
5278 processUp(mapper);
5279 processSync(mapper);
5280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5283 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5284}
5285
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286// --- MultiTouchInputMapperTest ---
5287
5288class MultiTouchInputMapperTest : public TouchInputMapperTest {
5289protected:
5290 void prepareAxes(int axes);
5291
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005292 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5293 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5294 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5295 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5296 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5297 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5298 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5299 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5300 void processId(MultiTouchInputMapper& mapper, int32_t id);
5301 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5302 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5303 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5304 void processMTSync(MultiTouchInputMapper& mapper);
5305 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005306};
5307
5308void MultiTouchInputMapperTest::prepareAxes(int axes) {
5309 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005310 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5311 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312 }
5313 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005314 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5315 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005317 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5318 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319 }
5320 }
5321 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005322 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5323 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005325 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5326 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327 }
5328 }
5329 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005330 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5331 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005332 }
5333 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005334 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5335 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336 }
5337 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005338 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5339 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 }
5341 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005342 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5343 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344 }
5345 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005346 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5347 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348 }
5349 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005350 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351 }
5352}
5353
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005354void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5355 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005356 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5357 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358}
5359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005360void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5361 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005362 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363}
5364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005365void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5366 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005367 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368}
5369
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005370void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005371 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372}
5373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005375 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376}
5377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005378void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5379 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005380 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381}
5382
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005383void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005384 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385}
5386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005387void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005388 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389}
5390
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005391void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005392 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393}
5394
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005395void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005396 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397}
5398
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005399void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005400 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401}
5402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005403void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5404 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005405 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406}
5407
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005408void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005409 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410}
5411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005412void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005413 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414}
5415
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417 addConfigurationProperty("touch.deviceType", "touchScreen");
5418 prepareDisplay(DISPLAY_ORIENTATION_0);
5419 prepareAxes(POSITION);
5420 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005421 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422
5423 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5424
5425 NotifyMotionArgs motionArgs;
5426
5427 // Two fingers down at once.
5428 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5429 processPosition(mapper, x1, y1);
5430 processMTSync(mapper);
5431 processPosition(mapper, x2, y2);
5432 processMTSync(mapper);
5433 processSync(mapper);
5434
5435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5436 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5437 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5438 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5439 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5441 ASSERT_EQ(0, motionArgs.flags);
5442 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5443 ASSERT_EQ(0, motionArgs.buttonState);
5444 ASSERT_EQ(0, motionArgs.edgeFlags);
5445 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5446 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5449 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5450 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5451 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5453
5454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5456 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5457 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5458 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5459 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5460 motionArgs.action);
5461 ASSERT_EQ(0, motionArgs.flags);
5462 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5463 ASSERT_EQ(0, motionArgs.buttonState);
5464 ASSERT_EQ(0, motionArgs.edgeFlags);
5465 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5466 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5468 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5471 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5473 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5474 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5475 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5476 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5477
5478 // Move.
5479 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5480 processPosition(mapper, x1, y1);
5481 processMTSync(mapper);
5482 processPosition(mapper, x2, y2);
5483 processMTSync(mapper);
5484 processSync(mapper);
5485
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5487 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5488 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5489 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5490 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5492 ASSERT_EQ(0, motionArgs.flags);
5493 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5494 ASSERT_EQ(0, motionArgs.buttonState);
5495 ASSERT_EQ(0, motionArgs.edgeFlags);
5496 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5497 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5499 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5502 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5504 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5505 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5506 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5507 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5508
5509 // First finger up.
5510 x2 += 15; y2 -= 20;
5511 processPosition(mapper, x2, y2);
5512 processMTSync(mapper);
5513 processSync(mapper);
5514
5515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5516 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5517 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5518 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5519 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5520 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5521 motionArgs.action);
5522 ASSERT_EQ(0, motionArgs.flags);
5523 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5524 ASSERT_EQ(0, motionArgs.buttonState);
5525 ASSERT_EQ(0, motionArgs.edgeFlags);
5526 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5527 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5529 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5532 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5534 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5535 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5536 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5537 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5538
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5540 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5541 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5542 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5543 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5545 ASSERT_EQ(0, motionArgs.flags);
5546 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5547 ASSERT_EQ(0, motionArgs.buttonState);
5548 ASSERT_EQ(0, motionArgs.edgeFlags);
5549 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5550 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5553 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5554 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5555 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5556 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5557
5558 // Move.
5559 x2 += 20; y2 -= 25;
5560 processPosition(mapper, x2, y2);
5561 processMTSync(mapper);
5562 processSync(mapper);
5563
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5565 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5566 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5567 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5568 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5570 ASSERT_EQ(0, motionArgs.flags);
5571 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5572 ASSERT_EQ(0, motionArgs.buttonState);
5573 ASSERT_EQ(0, motionArgs.edgeFlags);
5574 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5575 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5578 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5579 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5580 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5581 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5582
5583 // New finger down.
5584 int32_t x3 = 700, y3 = 300;
5585 processPosition(mapper, x2, y2);
5586 processMTSync(mapper);
5587 processPosition(mapper, x3, y3);
5588 processMTSync(mapper);
5589 processSync(mapper);
5590
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5592 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5593 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5594 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5595 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5596 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5597 motionArgs.action);
5598 ASSERT_EQ(0, motionArgs.flags);
5599 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5600 ASSERT_EQ(0, motionArgs.buttonState);
5601 ASSERT_EQ(0, motionArgs.edgeFlags);
5602 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5603 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5605 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5608 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5610 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5611 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5612 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5613 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5614
5615 // Second finger up.
5616 x3 += 30; y3 -= 20;
5617 processPosition(mapper, x3, y3);
5618 processMTSync(mapper);
5619 processSync(mapper);
5620
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5622 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5623 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5624 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5625 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5626 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5627 motionArgs.action);
5628 ASSERT_EQ(0, motionArgs.flags);
5629 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5630 ASSERT_EQ(0, motionArgs.buttonState);
5631 ASSERT_EQ(0, motionArgs.edgeFlags);
5632 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5635 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5638 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5640 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5641 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5642 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5643 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5644
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5646 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5647 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5648 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5649 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5651 ASSERT_EQ(0, motionArgs.flags);
5652 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5653 ASSERT_EQ(0, motionArgs.buttonState);
5654 ASSERT_EQ(0, motionArgs.edgeFlags);
5655 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5656 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5659 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5660 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5661 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5662 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5663
5664 // Last finger up.
5665 processMTSync(mapper);
5666 processSync(mapper);
5667
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5670 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5671 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5672 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5673 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5674 ASSERT_EQ(0, motionArgs.flags);
5675 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5676 ASSERT_EQ(0, motionArgs.buttonState);
5677 ASSERT_EQ(0, motionArgs.edgeFlags);
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 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5684 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5685 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5686
5687 // Should not have sent any more keys or motions.
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5690}
5691
5692TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 addConfigurationProperty("touch.deviceType", "touchScreen");
5694 prepareDisplay(DISPLAY_ORIENTATION_0);
5695 prepareAxes(POSITION | ID);
5696 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005697 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698
5699 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5700
5701 NotifyMotionArgs motionArgs;
5702
5703 // Two fingers down at once.
5704 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5705 processPosition(mapper, x1, y1);
5706 processId(mapper, 1);
5707 processMTSync(mapper);
5708 processPosition(mapper, x2, y2);
5709 processId(mapper, 2);
5710 processMTSync(mapper);
5711 processSync(mapper);
5712
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5714 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5715 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5716 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5719 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5720
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5722 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5723 motionArgs.action);
5724 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5725 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5727 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5730 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5732 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5733
5734 // Move.
5735 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5736 processPosition(mapper, x1, y1);
5737 processId(mapper, 1);
5738 processMTSync(mapper);
5739 processPosition(mapper, x2, y2);
5740 processId(mapper, 2);
5741 processMTSync(mapper);
5742 processSync(mapper);
5743
5744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5746 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5747 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5749 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5752 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5754 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5755
5756 // First finger up.
5757 x2 += 15; y2 -= 20;
5758 processPosition(mapper, x2, y2);
5759 processId(mapper, 2);
5760 processMTSync(mapper);
5761 processSync(mapper);
5762
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5764 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5765 motionArgs.action);
5766 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5767 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5769 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5770 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5772 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5774 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5775
5776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5778 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5779 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5782 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5783
5784 // Move.
5785 x2 += 20; y2 -= 25;
5786 processPosition(mapper, x2, y2);
5787 processId(mapper, 2);
5788 processMTSync(mapper);
5789 processSync(mapper);
5790
5791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5793 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5794 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5797 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5798
5799 // New finger down.
5800 int32_t x3 = 700, y3 = 300;
5801 processPosition(mapper, x2, y2);
5802 processId(mapper, 2);
5803 processMTSync(mapper);
5804 processPosition(mapper, x3, y3);
5805 processId(mapper, 3);
5806 processMTSync(mapper);
5807 processSync(mapper);
5808
5809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5810 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5811 motionArgs.action);
5812 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5813 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5815 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5818 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5820 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5821
5822 // Second finger up.
5823 x3 += 30; y3 -= 20;
5824 processPosition(mapper, x3, y3);
5825 processId(mapper, 3);
5826 processMTSync(mapper);
5827 processSync(mapper);
5828
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5830 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5831 motionArgs.action);
5832 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5833 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5835 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5836 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5838 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5840 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5841
5842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5844 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5845 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5848 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5849
5850 // Last finger up.
5851 processMTSync(mapper);
5852 processSync(mapper);
5853
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5855 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5856 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5857 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5860 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5861
5862 // Should not have sent any more keys or motions.
5863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5865}
5866
5867TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868 addConfigurationProperty("touch.deviceType", "touchScreen");
5869 prepareDisplay(DISPLAY_ORIENTATION_0);
5870 prepareAxes(POSITION | ID | SLOT);
5871 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005872 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005873
5874 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5875
5876 NotifyMotionArgs motionArgs;
5877
5878 // Two fingers down at once.
5879 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5880 processPosition(mapper, x1, y1);
5881 processId(mapper, 1);
5882 processSlot(mapper, 1);
5883 processPosition(mapper, x2, y2);
5884 processId(mapper, 2);
5885 processSync(mapper);
5886
5887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5888 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5889 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5890 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5893 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5894
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5896 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5897 motionArgs.action);
5898 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5899 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5900 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5901 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5904 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5906 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5907
5908 // Move.
5909 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5910 processSlot(mapper, 0);
5911 processPosition(mapper, x1, y1);
5912 processSlot(mapper, 1);
5913 processPosition(mapper, x2, y2);
5914 processSync(mapper);
5915
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5918 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5919 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5921 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5924 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5926 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5927
5928 // First finger up.
5929 x2 += 15; y2 -= 20;
5930 processSlot(mapper, 0);
5931 processId(mapper, -1);
5932 processSlot(mapper, 1);
5933 processPosition(mapper, x2, y2);
5934 processSync(mapper);
5935
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5937 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5938 motionArgs.action);
5939 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5940 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5942 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5945 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5947 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5948
5949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5951 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5952 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5955 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5956
5957 // Move.
5958 x2 += 20; y2 -= 25;
5959 processPosition(mapper, x2, y2);
5960 processSync(mapper);
5961
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5964 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5965 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5968 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5969
5970 // New finger down.
5971 int32_t x3 = 700, y3 = 300;
5972 processPosition(mapper, x2, y2);
5973 processSlot(mapper, 0);
5974 processId(mapper, 3);
5975 processPosition(mapper, x3, y3);
5976 processSync(mapper);
5977
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5979 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5980 motionArgs.action);
5981 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5982 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5984 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5985 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5987 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5989 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5990
5991 // Second finger up.
5992 x3 += 30; y3 -= 20;
5993 processSlot(mapper, 1);
5994 processId(mapper, -1);
5995 processSlot(mapper, 0);
5996 processPosition(mapper, x3, y3);
5997 processSync(mapper);
5998
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6000 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6001 motionArgs.action);
6002 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6003 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6005 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6006 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6008 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6010 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6011
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6014 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6015 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6016 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6018 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6019
6020 // Last finger up.
6021 processId(mapper, -1);
6022 processSync(mapper);
6023
6024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6025 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6026 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6027 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6030 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6031
6032 // Should not have sent any more keys or motions.
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6035}
6036
6037TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006038 addConfigurationProperty("touch.deviceType", "touchScreen");
6039 prepareDisplay(DISPLAY_ORIENTATION_0);
6040 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006041 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042
6043 // These calculations are based on the input device calibration documentation.
6044 int32_t rawX = 100;
6045 int32_t rawY = 200;
6046 int32_t rawTouchMajor = 7;
6047 int32_t rawTouchMinor = 6;
6048 int32_t rawToolMajor = 9;
6049 int32_t rawToolMinor = 8;
6050 int32_t rawPressure = 11;
6051 int32_t rawDistance = 0;
6052 int32_t rawOrientation = 3;
6053 int32_t id = 5;
6054
6055 float x = toDisplayX(rawX);
6056 float y = toDisplayY(rawY);
6057 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6058 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6059 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6060 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6061 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6062 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6063 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6064 float distance = float(rawDistance);
6065
6066 processPosition(mapper, rawX, rawY);
6067 processTouchMajor(mapper, rawTouchMajor);
6068 processTouchMinor(mapper, rawTouchMinor);
6069 processToolMajor(mapper, rawToolMajor);
6070 processToolMinor(mapper, rawToolMinor);
6071 processPressure(mapper, rawPressure);
6072 processOrientation(mapper, rawOrientation);
6073 processDistance(mapper, rawDistance);
6074 processId(mapper, id);
6075 processMTSync(mapper);
6076 processSync(mapper);
6077
6078 NotifyMotionArgs args;
6079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6080 ASSERT_EQ(0, args.pointerProperties[0].id);
6081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6082 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6083 orientation, distance));
6084}
6085
6086TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087 addConfigurationProperty("touch.deviceType", "touchScreen");
6088 prepareDisplay(DISPLAY_ORIENTATION_0);
6089 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6090 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006091 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092
6093 // These calculations are based on the input device calibration documentation.
6094 int32_t rawX = 100;
6095 int32_t rawY = 200;
6096 int32_t rawTouchMajor = 140;
6097 int32_t rawTouchMinor = 120;
6098 int32_t rawToolMajor = 180;
6099 int32_t rawToolMinor = 160;
6100
6101 float x = toDisplayX(rawX);
6102 float y = toDisplayY(rawY);
6103 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6104 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6105 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6106 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6107 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6108
6109 processPosition(mapper, rawX, rawY);
6110 processTouchMajor(mapper, rawTouchMajor);
6111 processTouchMinor(mapper, rawTouchMinor);
6112 processToolMajor(mapper, rawToolMajor);
6113 processToolMinor(mapper, rawToolMinor);
6114 processMTSync(mapper);
6115 processSync(mapper);
6116
6117 NotifyMotionArgs args;
6118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6120 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6121}
6122
6123TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124 addConfigurationProperty("touch.deviceType", "touchScreen");
6125 prepareDisplay(DISPLAY_ORIENTATION_0);
6126 prepareAxes(POSITION | TOUCH | TOOL);
6127 addConfigurationProperty("touch.size.calibration", "diameter");
6128 addConfigurationProperty("touch.size.scale", "10");
6129 addConfigurationProperty("touch.size.bias", "160");
6130 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006131 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132
6133 // These calculations are based on the input device calibration documentation.
6134 // Note: We only provide a single common touch/tool value because the device is assumed
6135 // not to emit separate values for each pointer (isSummed = 1).
6136 int32_t rawX = 100;
6137 int32_t rawY = 200;
6138 int32_t rawX2 = 150;
6139 int32_t rawY2 = 250;
6140 int32_t rawTouchMajor = 5;
6141 int32_t rawToolMajor = 8;
6142
6143 float x = toDisplayX(rawX);
6144 float y = toDisplayY(rawY);
6145 float x2 = toDisplayX(rawX2);
6146 float y2 = toDisplayY(rawY2);
6147 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6148 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6149 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6150
6151 processPosition(mapper, rawX, rawY);
6152 processTouchMajor(mapper, rawTouchMajor);
6153 processToolMajor(mapper, rawToolMajor);
6154 processMTSync(mapper);
6155 processPosition(mapper, rawX2, rawY2);
6156 processTouchMajor(mapper, rawTouchMajor);
6157 processToolMajor(mapper, rawToolMajor);
6158 processMTSync(mapper);
6159 processSync(mapper);
6160
6161 NotifyMotionArgs args;
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6163 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6164
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6166 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6167 args.action);
6168 ASSERT_EQ(size_t(2), args.pointerCount);
6169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6170 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6172 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6173}
6174
6175TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176 addConfigurationProperty("touch.deviceType", "touchScreen");
6177 prepareDisplay(DISPLAY_ORIENTATION_0);
6178 prepareAxes(POSITION | TOUCH | TOOL);
6179 addConfigurationProperty("touch.size.calibration", "area");
6180 addConfigurationProperty("touch.size.scale", "43");
6181 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006182 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183
6184 // These calculations are based on the input device calibration documentation.
6185 int32_t rawX = 100;
6186 int32_t rawY = 200;
6187 int32_t rawTouchMajor = 5;
6188 int32_t rawToolMajor = 8;
6189
6190 float x = toDisplayX(rawX);
6191 float y = toDisplayY(rawY);
6192 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6193 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6194 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6195
6196 processPosition(mapper, rawX, rawY);
6197 processTouchMajor(mapper, rawTouchMajor);
6198 processToolMajor(mapper, rawToolMajor);
6199 processMTSync(mapper);
6200 processSync(mapper);
6201
6202 NotifyMotionArgs args;
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6205 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6206}
6207
6208TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 addConfigurationProperty("touch.deviceType", "touchScreen");
6210 prepareDisplay(DISPLAY_ORIENTATION_0);
6211 prepareAxes(POSITION | PRESSURE);
6212 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6213 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006214 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215
Michael Wrightaa449c92017-12-13 21:21:43 +00006216 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006217 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006218 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6219 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6220 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6221
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222 // These calculations are based on the input device calibration documentation.
6223 int32_t rawX = 100;
6224 int32_t rawY = 200;
6225 int32_t rawPressure = 60;
6226
6227 float x = toDisplayX(rawX);
6228 float y = toDisplayY(rawY);
6229 float pressure = float(rawPressure) * 0.01f;
6230
6231 processPosition(mapper, rawX, rawY);
6232 processPressure(mapper, rawPressure);
6233 processMTSync(mapper);
6234 processSync(mapper);
6235
6236 NotifyMotionArgs args;
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6238 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6239 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6240}
6241
6242TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243 addConfigurationProperty("touch.deviceType", "touchScreen");
6244 prepareDisplay(DISPLAY_ORIENTATION_0);
6245 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006246 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247
6248 NotifyMotionArgs motionArgs;
6249 NotifyKeyArgs keyArgs;
6250
6251 processId(mapper, 1);
6252 processPosition(mapper, 100, 200);
6253 processSync(mapper);
6254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6255 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6256 ASSERT_EQ(0, motionArgs.buttonState);
6257
6258 // press BTN_LEFT, release BTN_LEFT
6259 processKey(mapper, BTN_LEFT, 1);
6260 processSync(mapper);
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6263 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6264
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6267 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6268
Michael Wrightd02c5b62014-02-10 15:10:22 -08006269 processKey(mapper, BTN_LEFT, 0);
6270 processSync(mapper);
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006274
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278
6279 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6280 processKey(mapper, BTN_RIGHT, 1);
6281 processKey(mapper, BTN_MIDDLE, 1);
6282 processSync(mapper);
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6285 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6286 motionArgs.buttonState);
6287
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6290 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6295 motionArgs.buttonState);
6296
Michael Wrightd02c5b62014-02-10 15:10:22 -08006297 processKey(mapper, BTN_RIGHT, 0);
6298 processSync(mapper);
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006305 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306
6307 processKey(mapper, BTN_MIDDLE, 0);
6308 processSync(mapper);
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006310 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006315 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316
6317 // press BTN_BACK, release BTN_BACK
6318 processKey(mapper, BTN_BACK, 1);
6319 processSync(mapper);
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6321 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6322 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006323
Michael Wrightd02c5b62014-02-10 15:10:22 -08006324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6327
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6330 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331
6332 processKey(mapper, BTN_BACK, 0);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006337
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006340 ASSERT_EQ(0, motionArgs.buttonState);
6341
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6343 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6344 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6345
6346 // press BTN_SIDE, release BTN_SIDE
6347 processKey(mapper, BTN_SIDE, 1);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6350 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6351 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006352
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006355 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6356
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360
6361 processKey(mapper, BTN_SIDE, 0);
6362 processSync(mapper);
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006366
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_EQ(0, motionArgs.buttonState);
6370
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6372 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6373 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6374
6375 // press BTN_FORWARD, release BTN_FORWARD
6376 processKey(mapper, BTN_FORWARD, 1);
6377 processSync(mapper);
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6379 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6380 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006381
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6385
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6388 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389
6390 processKey(mapper, BTN_FORWARD, 0);
6391 processSync(mapper);
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006395
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398 ASSERT_EQ(0, motionArgs.buttonState);
6399
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6402 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6403
6404 // press BTN_EXTRA, release BTN_EXTRA
6405 processKey(mapper, BTN_EXTRA, 1);
6406 processSync(mapper);
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6408 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6409 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006410
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006413 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6414
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6417 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418
6419 processKey(mapper, BTN_EXTRA, 0);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006424
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427 ASSERT_EQ(0, motionArgs.buttonState);
6428
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6430 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6431 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6432
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6434
Michael Wrightd02c5b62014-02-10 15:10:22 -08006435 // press BTN_STYLUS, release BTN_STYLUS
6436 processKey(mapper, BTN_STYLUS, 1);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006440 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6441
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6444 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006445
6446 processKey(mapper, BTN_STYLUS, 0);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006451
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006454 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455
6456 // press BTN_STYLUS2, release BTN_STYLUS2
6457 processKey(mapper, BTN_STYLUS2, 1);
6458 processSync(mapper);
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006461 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6462
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6465 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006466
6467 processKey(mapper, BTN_STYLUS2, 0);
6468 processSync(mapper);
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006472
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006475 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006476
6477 // release touch
6478 processId(mapper, -1);
6479 processSync(mapper);
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6481 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6482 ASSERT_EQ(0, motionArgs.buttonState);
6483}
6484
6485TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 addConfigurationProperty("touch.deviceType", "touchScreen");
6487 prepareDisplay(DISPLAY_ORIENTATION_0);
6488 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006489 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490
6491 NotifyMotionArgs motionArgs;
6492
6493 // default tool type is finger
6494 processId(mapper, 1);
6495 processPosition(mapper, 100, 200);
6496 processSync(mapper);
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6498 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6499 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6500
6501 // eraser
6502 processKey(mapper, BTN_TOOL_RUBBER, 1);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6507
6508 // stylus
6509 processKey(mapper, BTN_TOOL_RUBBER, 0);
6510 processKey(mapper, BTN_TOOL_PEN, 1);
6511 processSync(mapper);
6512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6515
6516 // brush
6517 processKey(mapper, BTN_TOOL_PEN, 0);
6518 processKey(mapper, BTN_TOOL_BRUSH, 1);
6519 processSync(mapper);
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6523
6524 // pencil
6525 processKey(mapper, BTN_TOOL_BRUSH, 0);
6526 processKey(mapper, BTN_TOOL_PENCIL, 1);
6527 processSync(mapper);
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6529 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6531
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006532 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533 processKey(mapper, BTN_TOOL_PENCIL, 0);
6534 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6535 processSync(mapper);
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6539
6540 // mouse
6541 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6542 processKey(mapper, BTN_TOOL_MOUSE, 1);
6543 processSync(mapper);
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6547
6548 // lens
6549 processKey(mapper, BTN_TOOL_MOUSE, 0);
6550 processKey(mapper, BTN_TOOL_LENS, 1);
6551 processSync(mapper);
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6555
6556 // double-tap
6557 processKey(mapper, BTN_TOOL_LENS, 0);
6558 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6559 processSync(mapper);
6560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6563
6564 // triple-tap
6565 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6566 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6567 processSync(mapper);
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6571
6572 // quad-tap
6573 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6574 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6575 processSync(mapper);
6576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6579
6580 // finger
6581 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6582 processKey(mapper, BTN_TOOL_FINGER, 1);
6583 processSync(mapper);
6584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6587
6588 // stylus trumps finger
6589 processKey(mapper, BTN_TOOL_PEN, 1);
6590 processSync(mapper);
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6594
6595 // eraser trumps stylus
6596 processKey(mapper, BTN_TOOL_RUBBER, 1);
6597 processSync(mapper);
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6601
6602 // mouse trumps eraser
6603 processKey(mapper, BTN_TOOL_MOUSE, 1);
6604 processSync(mapper);
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6608
6609 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6610 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6615
6616 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6617 processToolType(mapper, MT_TOOL_PEN);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6622
6623 // back to default tool type
6624 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6625 processKey(mapper, BTN_TOOL_MOUSE, 0);
6626 processKey(mapper, BTN_TOOL_RUBBER, 0);
6627 processKey(mapper, BTN_TOOL_PEN, 0);
6628 processKey(mapper, BTN_TOOL_FINGER, 0);
6629 processSync(mapper);
6630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6633}
6634
6635TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006636 addConfigurationProperty("touch.deviceType", "touchScreen");
6637 prepareDisplay(DISPLAY_ORIENTATION_0);
6638 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006639 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006640 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006641
6642 NotifyMotionArgs motionArgs;
6643
6644 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6645 processId(mapper, 1);
6646 processPosition(mapper, 100, 200);
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6649 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6651 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6652
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6654 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6656 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6657
6658 // move a little
6659 processPosition(mapper, 150, 250);
6660 processSync(mapper);
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6662 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6664 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6665
6666 // down when BTN_TOUCH is pressed, pressure defaults to 1
6667 processKey(mapper, BTN_TOUCH, 1);
6668 processSync(mapper);
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6670 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6672 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6673
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6677 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6678
6679 // up when BTN_TOUCH is released, hover restored
6680 processKey(mapper, BTN_TOUCH, 0);
6681 processSync(mapper);
6682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6683 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6685 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6686
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6688 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6690 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6691
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6693 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6695 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6696
6697 // exit hover when pointer goes away
6698 processId(mapper, -1);
6699 processSync(mapper);
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6703 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6704}
6705
6706TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006707 addConfigurationProperty("touch.deviceType", "touchScreen");
6708 prepareDisplay(DISPLAY_ORIENTATION_0);
6709 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006710 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006711
6712 NotifyMotionArgs motionArgs;
6713
6714 // initially hovering because pressure is 0
6715 processId(mapper, 1);
6716 processPosition(mapper, 100, 200);
6717 processPressure(mapper, 0);
6718 processSync(mapper);
6719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6720 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6722 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6723
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6725 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6727 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6728
6729 // move a little
6730 processPosition(mapper, 150, 250);
6731 processSync(mapper);
6732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6735 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6736
6737 // down when pressure becomes non-zero
6738 processPressure(mapper, RAW_PRESSURE_MAX);
6739 processSync(mapper);
6740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6741 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6743 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6744
6745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6746 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6748 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6749
6750 // up when pressure becomes 0, hover restored
6751 processPressure(mapper, 0);
6752 processSync(mapper);
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6754 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6756 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6757
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6759 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6761 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6762
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6764 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6766 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6767
6768 // exit hover when pointer goes away
6769 processId(mapper, -1);
6770 processSync(mapper);
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6772 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6774 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6775}
6776
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006777/**
6778 * Set the input device port <--> display port associations, and check that the
6779 * events are routed to the display that matches the display port.
6780 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6781 */
6782TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006783 const std::string usb2 = "USB2";
6784 const uint8_t hdmi1 = 0;
6785 const uint8_t hdmi2 = 1;
6786 const std::string secondaryUniqueId = "uniqueId2";
6787 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6788
6789 addConfigurationProperty("touch.deviceType", "touchScreen");
6790 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006791 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006792
6793 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6794 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6795
6796 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6797 // for this input device is specified, and the matching viewport is not present,
6798 // the input device should be disabled (at the mapper level).
6799
6800 // Add viewport for display 2 on hdmi2
6801 prepareSecondaryDisplay(type, hdmi2);
6802 // Send a touch event
6803 processPosition(mapper, 100, 100);
6804 processSync(mapper);
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6806
6807 // Add viewport for display 1 on hdmi1
6808 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6809 // Send a touch event again
6810 processPosition(mapper, 100, 100);
6811 processSync(mapper);
6812
6813 NotifyMotionArgs args;
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6815 ASSERT_EQ(DISPLAY_ID, args.displayId);
6816}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006817
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006818TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006819 // Setup for second display.
Michael Wright7a376672020-06-26 20:51:44 +01006820 std::shared_ptr<FakePointerController> fakePointerController =
6821 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006822 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006823 fakePointerController->setPosition(100, 200);
6824 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006825 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6826
Garfield Tan888a6a42020-01-09 11:39:16 -08006827 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6828 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6829
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006830 prepareDisplay(DISPLAY_ORIENTATION_0);
6831 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006832 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006833
6834 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006835 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006836
6837 NotifyMotionArgs motionArgs;
6838 processPosition(mapper, 100, 100);
6839 processSync(mapper);
6840
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6842 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6843 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6844}
6845
Arthur Hung7c645402019-01-25 17:45:42 +08006846TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6847 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006848 prepareAxes(POSITION | ID | SLOT);
6849 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006850 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006851
6852 // Create the second touch screen device, and enable multi fingers.
6853 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006854 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006855 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006856 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006857 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006858 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006859 std::unique_ptr<InputDevice> device2 =
6860 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006861 identifier);
6862 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6863 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6864 0 /*flat*/, 0 /*fuzz*/);
6865 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6866 0 /*flat*/, 0 /*fuzz*/);
6867 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6868 0 /*flat*/, 0 /*fuzz*/);
6869 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6870 0 /*flat*/, 0 /*fuzz*/);
6871 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6872 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6873 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006874
6875 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006876 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006877 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6878 device2->reset(ARBITRARY_TIME);
6879
6880 // Setup PointerController.
Michael Wright7a376672020-06-26 20:51:44 +01006881 std::shared_ptr<FakePointerController> fakePointerController =
6882 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006883 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6884 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6885
6886 // Setup policy for associated displays and show touches.
6887 const uint8_t hdmi1 = 0;
6888 const uint8_t hdmi2 = 1;
6889 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6890 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6891 mFakePolicy->setShowTouches(true);
6892
6893 // Create displays.
6894 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6895 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6896
6897 // Default device will reconfigure above, need additional reconfiguration for another device.
6898 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6899 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6900
6901 // Two fingers down at default display.
6902 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6903 processPosition(mapper, x1, y1);
6904 processId(mapper, 1);
6905 processSlot(mapper, 1);
6906 processPosition(mapper, x2, y2);
6907 processId(mapper, 2);
6908 processSync(mapper);
6909
6910 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6911 fakePointerController->getSpots().find(DISPLAY_ID);
6912 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6913 ASSERT_EQ(size_t(2), iter->second.size());
6914
6915 // Two fingers down at second display.
6916 processPosition(mapper2, x1, y1);
6917 processId(mapper2, 1);
6918 processSlot(mapper2, 1);
6919 processPosition(mapper2, x2, y2);
6920 processId(mapper2, 2);
6921 processSync(mapper2);
6922
6923 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6924 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6925 ASSERT_EQ(size_t(2), iter->second.size());
6926}
6927
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006928TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006929 prepareAxes(POSITION);
6930 addConfigurationProperty("touch.deviceType", "touchScreen");
6931 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006932 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006933
6934 NotifyMotionArgs motionArgs;
6935 // Unrotated video frame
6936 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6937 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006938 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006939 processPosition(mapper, 100, 200);
6940 processSync(mapper);
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6942 ASSERT_EQ(frames, motionArgs.videoFrames);
6943
6944 // Subsequent touch events should not have any videoframes
6945 // This is implemented separately in FakeEventHub,
6946 // but that should match the behaviour of TouchVideoDevice.
6947 processPosition(mapper, 200, 200);
6948 processSync(mapper);
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6950 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6951}
6952
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006953TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006954 prepareAxes(POSITION);
6955 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006956 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006957 // Unrotated video frame
6958 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6959 NotifyMotionArgs motionArgs;
6960
6961 // Test all 4 orientations
6962 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6963 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6964 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6965 clearViewports();
6966 prepareDisplay(orientation);
6967 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006968 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006969 processPosition(mapper, 100, 200);
6970 processSync(mapper);
6971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6972 frames[0].rotate(orientation);
6973 ASSERT_EQ(frames, motionArgs.videoFrames);
6974 }
6975}
6976
6977TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006978 prepareAxes(POSITION);
6979 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006980 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006981 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6982 // so mix these.
6983 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6984 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6985 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6986 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6987 NotifyMotionArgs motionArgs;
6988
6989 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006990 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006991 processPosition(mapper, 100, 200);
6992 processSync(mapper);
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6994 std::for_each(frames.begin(), frames.end(),
6995 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6996 ASSERT_EQ(frames, motionArgs.videoFrames);
6997}
6998
Arthur Hung9da14732019-09-02 16:16:58 +08006999/**
7000 * If we had defined port associations, but the viewport is not ready, the touch device would be
7001 * expected to be disabled, and it should be enabled after the viewport has found.
7002 */
7003TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007004 constexpr uint8_t hdmi2 = 1;
7005 const std::string secondaryUniqueId = "uniqueId2";
7006 constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
7007
7008 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7009
7010 addConfigurationProperty("touch.deviceType", "touchScreen");
7011 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007012 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007013
7014 ASSERT_EQ(mDevice->isEnabled(), false);
7015
7016 // Add display on hdmi2, the device should be enabled and can receive touch event.
7017 prepareSecondaryDisplay(type, hdmi2);
7018 ASSERT_EQ(mDevice->isEnabled(), true);
7019
7020 // Send a touch event.
7021 processPosition(mapper, 100, 100);
7022 processSync(mapper);
7023
7024 NotifyMotionArgs args;
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7026 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7027}
7028
Arthur Hung6cd19a42019-08-30 19:04:12 +08007029
Arthur Hung6cd19a42019-08-30 19:04:12 +08007030
Arthur Hung421eb1c2020-01-16 00:09:42 +08007031TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007032 addConfigurationProperty("touch.deviceType", "touchScreen");
7033 prepareDisplay(DISPLAY_ORIENTATION_0);
7034 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007035 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007036
7037 NotifyMotionArgs motionArgs;
7038
7039 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7040 // finger down
7041 processId(mapper, 1);
7042 processPosition(mapper, x1, y1);
7043 processSync(mapper);
7044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7045 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7047
7048 // finger move
7049 processId(mapper, 1);
7050 processPosition(mapper, x2, y2);
7051 processSync(mapper);
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7055
7056 // finger up.
7057 processId(mapper, -1);
7058 processSync(mapper);
7059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7060 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7062
7063 // new finger down
7064 processId(mapper, 1);
7065 processPosition(mapper, x3, y3);
7066 processSync(mapper);
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7068 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7070}
7071
7072/**
arthurhung65600042020-04-30 17:55:40 +08007073 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7074 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007075 */
arthurhung65600042020-04-30 17:55:40 +08007076TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007077 addConfigurationProperty("touch.deviceType", "touchScreen");
7078 prepareDisplay(DISPLAY_ORIENTATION_0);
7079 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007080 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007081
7082 NotifyMotionArgs motionArgs;
7083
7084 // default tool type is finger
7085 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhung65600042020-04-30 17:55:40 +08007086 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007087 processPosition(mapper, x1, y1);
7088 processSync(mapper);
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7090 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7092
7093 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7094 processToolType(mapper, MT_TOOL_PALM);
7095 processSync(mapper);
7096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7097 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7098
7099 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhung65600042020-04-30 17:55:40 +08007100 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007101 processPosition(mapper, x2, y2);
7102 processSync(mapper);
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7104
7105 // finger up.
arthurhung65600042020-04-30 17:55:40 +08007106 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007107 processSync(mapper);
7108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7109
7110 // new finger down
arthurhung65600042020-04-30 17:55:40 +08007111 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007112 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007113 processPosition(mapper, x3, y3);
7114 processSync(mapper);
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7116 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7118}
7119
arthurhungbf89a482020-04-17 17:37:55 +08007120/**
arthurhung65600042020-04-30 17:55:40 +08007121 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7122 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007123 */
arthurhung65600042020-04-30 17:55:40 +08007124TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007125 addConfigurationProperty("touch.deviceType", "touchScreen");
7126 prepareDisplay(DISPLAY_ORIENTATION_0);
7127 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7128 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7129
7130 NotifyMotionArgs motionArgs;
7131
7132 // default tool type is finger
arthurhung65600042020-04-30 17:55:40 +08007133 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7134 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007135 processPosition(mapper, x1, y1);
7136 processSync(mapper);
7137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7138 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7140
7141 // Second finger down.
arthurhung65600042020-04-30 17:55:40 +08007142 processSlot(mapper, SECOND_SLOT);
7143 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007144 processPosition(mapper, x2, y2);
arthurhung65600042020-04-30 17:55:40 +08007145 processSync(mapper);
7146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7147 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7148 motionArgs.action);
7149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7150
7151 // If the tool type of the first finger changes to MT_TOOL_PALM,
7152 // we expect to receive ACTION_POINTER_UP with cancel flag.
7153 processSlot(mapper, FIRST_SLOT);
7154 processId(mapper, FIRST_TRACKING_ID);
7155 processToolType(mapper, MT_TOOL_PALM);
7156 processSync(mapper);
7157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7158 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7159 motionArgs.action);
7160 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7161
7162 // The following MOVE events of second finger should be processed.
7163 processSlot(mapper, SECOND_SLOT);
7164 processId(mapper, SECOND_TRACKING_ID);
7165 processPosition(mapper, x2 + 1, y2 + 1);
7166 processSync(mapper);
7167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7169 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7170
7171 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7172 // it. Second finger receive move.
7173 processSlot(mapper, FIRST_SLOT);
7174 processId(mapper, INVALID_TRACKING_ID);
7175 processSync(mapper);
7176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7178 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7179
7180 // Second finger keeps moving.
7181 processSlot(mapper, SECOND_SLOT);
7182 processId(mapper, SECOND_TRACKING_ID);
7183 processPosition(mapper, x2 + 2, y2 + 2);
7184 processSync(mapper);
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7187 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7188
7189 // Second finger up.
7190 processId(mapper, INVALID_TRACKING_ID);
7191 processSync(mapper);
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7193 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7194 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7195}
7196
7197/**
7198 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7199 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7200 */
7201TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7202 addConfigurationProperty("touch.deviceType", "touchScreen");
7203 prepareDisplay(DISPLAY_ORIENTATION_0);
7204 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7205 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7206
7207 NotifyMotionArgs motionArgs;
7208
7209 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7210 // First finger down.
7211 processId(mapper, FIRST_TRACKING_ID);
7212 processPosition(mapper, x1, y1);
7213 processSync(mapper);
7214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7215 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7217
7218 // Second finger down.
7219 processSlot(mapper, SECOND_SLOT);
7220 processId(mapper, SECOND_TRACKING_ID);
7221 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007222 processSync(mapper);
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7224 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7225 motionArgs.action);
7226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7227
arthurhung65600042020-04-30 17:55:40 +08007228 // If the tool type of the first finger changes to MT_TOOL_PALM,
7229 // we expect to receive ACTION_POINTER_UP with cancel flag.
7230 processSlot(mapper, FIRST_SLOT);
7231 processId(mapper, FIRST_TRACKING_ID);
7232 processToolType(mapper, MT_TOOL_PALM);
7233 processSync(mapper);
7234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7235 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7236 motionArgs.action);
7237 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7238
7239 // Second finger keeps moving.
7240 processSlot(mapper, SECOND_SLOT);
7241 processId(mapper, SECOND_TRACKING_ID);
7242 processPosition(mapper, x2 + 1, y2 + 1);
7243 processSync(mapper);
7244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7246
7247 // second finger becomes palm, receive cancel due to only 1 finger is active.
7248 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007249 processToolType(mapper, MT_TOOL_PALM);
7250 processSync(mapper);
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7252 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7253
arthurhung65600042020-04-30 17:55:40 +08007254 // third finger down.
7255 processSlot(mapper, THIRD_SLOT);
7256 processId(mapper, THIRD_TRACKING_ID);
7257 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007258 processPosition(mapper, x3, y3);
7259 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhung65600042020-04-30 17:55:40 +08007263 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7264
7265 // third finger move
7266 processId(mapper, THIRD_TRACKING_ID);
7267 processPosition(mapper, x3 + 1, y3 + 1);
7268 processSync(mapper);
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7271
7272 // first finger up, third finger receive move.
7273 processSlot(mapper, FIRST_SLOT);
7274 processId(mapper, INVALID_TRACKING_ID);
7275 processSync(mapper);
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7278 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7279
7280 // second finger up, third finger receive move.
7281 processSlot(mapper, SECOND_SLOT);
7282 processId(mapper, INVALID_TRACKING_ID);
7283 processSync(mapper);
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7286 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7287
7288 // third finger up.
7289 processSlot(mapper, THIRD_SLOT);
7290 processId(mapper, INVALID_TRACKING_ID);
7291 processSync(mapper);
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7294 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7295}
7296
7297/**
7298 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7299 * and the active finger could still be allowed to receive the events
7300 */
7301TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7302 addConfigurationProperty("touch.deviceType", "touchScreen");
7303 prepareDisplay(DISPLAY_ORIENTATION_0);
7304 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7305 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7306
7307 NotifyMotionArgs motionArgs;
7308
7309 // default tool type is finger
7310 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7311 processId(mapper, FIRST_TRACKING_ID);
7312 processPosition(mapper, x1, y1);
7313 processSync(mapper);
7314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7315 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7317
7318 // Second finger down.
7319 processSlot(mapper, SECOND_SLOT);
7320 processId(mapper, SECOND_TRACKING_ID);
7321 processPosition(mapper, x2, y2);
7322 processSync(mapper);
7323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7324 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7325 motionArgs.action);
7326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7327
7328 // If the tool type of the second finger changes to MT_TOOL_PALM,
7329 // we expect to receive ACTION_POINTER_UP with cancel flag.
7330 processId(mapper, SECOND_TRACKING_ID);
7331 processToolType(mapper, MT_TOOL_PALM);
7332 processSync(mapper);
7333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7334 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7335 motionArgs.action);
7336 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7337
7338 // The following MOVE event should be processed.
7339 processSlot(mapper, FIRST_SLOT);
7340 processId(mapper, FIRST_TRACKING_ID);
7341 processPosition(mapper, x1 + 1, y1 + 1);
7342 processSync(mapper);
7343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7345 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7346
7347 // second finger up.
7348 processSlot(mapper, SECOND_SLOT);
7349 processId(mapper, INVALID_TRACKING_ID);
7350 processSync(mapper);
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7353
7354 // first finger keep moving
7355 processSlot(mapper, FIRST_SLOT);
7356 processId(mapper, FIRST_TRACKING_ID);
7357 processPosition(mapper, x1 + 2, y1 + 2);
7358 processSync(mapper);
7359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7361
7362 // first finger up.
7363 processId(mapper, INVALID_TRACKING_ID);
7364 processSync(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7367 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007368}
7369
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007370// --- MultiTouchInputMapperTest_ExternalDevice ---
7371
7372class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7373protected:
7374 virtual void SetUp() override {
7375 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
7376 }
7377};
7378
7379/**
7380 * Expect fallback to internal viewport if device is external and external viewport is not present.
7381 */
7382TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7383 prepareAxes(POSITION);
7384 addConfigurationProperty("touch.deviceType", "touchScreen");
7385 prepareDisplay(DISPLAY_ORIENTATION_0);
7386 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7387
7388 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7389
7390 NotifyMotionArgs motionArgs;
7391
7392 // Expect the event to be sent to the internal viewport,
7393 // because an external viewport is not present.
7394 processPosition(mapper, 100, 100);
7395 processSync(mapper);
7396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7397 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7398
7399 // Expect the event to be sent to the external viewport if it is present.
7400 prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
7401 processPosition(mapper, 100, 100);
7402 processSync(mapper);
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7404 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7405}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007406
7407/**
7408 * Test touch should not work if outside of surface.
7409 */
7410class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7411protected:
7412 void halfDisplayToCenterHorizontal(int32_t orientation) {
7413 std::optional<DisplayViewport> internalViewport =
7414 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
7415
7416 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7417 internalViewport->orientation = orientation;
7418 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7419 internalViewport->logicalLeft = 0;
7420 internalViewport->logicalTop = 0;
7421 internalViewport->logicalRight = DISPLAY_HEIGHT;
7422 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7423
7424 internalViewport->physicalLeft = 0;
7425 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7426 internalViewport->physicalRight = DISPLAY_HEIGHT;
7427 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7428
7429 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7430 internalViewport->deviceHeight = DISPLAY_WIDTH;
7431 } else {
7432 internalViewport->logicalLeft = 0;
7433 internalViewport->logicalTop = 0;
7434 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7435 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7436
7437 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7438 internalViewport->physicalTop = 0;
7439 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7440 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7441
7442 internalViewport->deviceWidth = DISPLAY_WIDTH;
7443 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7444 }
7445
7446 mFakePolicy->updateViewport(internalViewport.value());
7447 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7448 }
7449
7450 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7451 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7452 int32_t yExpected) {
7453 // touch on outside area should not work.
7454 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7455 processSync(mapper);
7456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7457
7458 // touch on inside area should receive the event.
7459 NotifyMotionArgs args;
7460 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7461 processSync(mapper);
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7463 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7464 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7465
7466 // Reset.
7467 mapper.reset(ARBITRARY_TIME);
7468 }
7469};
7470
7471TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7472 addConfigurationProperty("touch.deviceType", "touchScreen");
7473 prepareDisplay(DISPLAY_ORIENTATION_0);
7474 prepareAxes(POSITION);
7475 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7476
7477 // Touch on center of normal display should work.
7478 const int32_t x = DISPLAY_WIDTH / 4;
7479 const int32_t y = DISPLAY_HEIGHT / 2;
7480 processPosition(mapper, toRawX(x), toRawY(y));
7481 processSync(mapper);
7482 NotifyMotionArgs args;
7483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7485 0.0f, 0.0f, 0.0f, 0.0f));
7486 // Reset.
7487 mapper.reset(ARBITRARY_TIME);
7488
7489 // Let physical display be different to device, and make surface and physical could be 1:1.
7490 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7491
7492 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7493 const int32_t yExpected = y;
7494 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7495}
7496
7497TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7498 addConfigurationProperty("touch.deviceType", "touchScreen");
7499 prepareDisplay(DISPLAY_ORIENTATION_0);
7500 prepareAxes(POSITION);
7501 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7502
7503 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7504 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7505
7506 const int32_t x = DISPLAY_WIDTH / 4;
7507 const int32_t y = DISPLAY_HEIGHT / 2;
7508
7509 // expect x/y = swap x/y then reverse y.
7510 const int32_t xExpected = y;
7511 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7512 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7513}
7514
7515TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7516 addConfigurationProperty("touch.deviceType", "touchScreen");
7517 prepareDisplay(DISPLAY_ORIENTATION_0);
7518 prepareAxes(POSITION);
7519 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7520
7521 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7522 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7523
7524 const int32_t x = DISPLAY_WIDTH / 4;
7525 const int32_t y = DISPLAY_HEIGHT / 2;
7526
7527 // expect x/y = swap x/y then reverse x.
7528 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7529 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7530 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7531}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007532} // namespace android