blob: b2c16d05c97f946e829ae39ff6034f1f105457b8 [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 Wright17db18e2020-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.
arthurhungcc7f9802020-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
arthurhungcc7f9802020-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 Wright17db18e2020-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 Wright17db18e2020-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 Wright17db18e2020-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 Wright17db18e2020-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 Wright17db18e2020-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 Wright17db18e2020-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 Wright17db18e2020-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 =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001199 mFakePolicy->getDisplayViewportByType(ViewportType::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,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001204 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT,
1205 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001206
1207 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001208 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001209 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001210 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001211
1212 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001213 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001214 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001215 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001216
1217 mFakePolicy->clearViewports();
1218 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001219 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001220 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001221 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001222 ASSERT_FALSE(internalViewport);
1223}
1224
1225TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1226 const std::string internalUniqueId = "local:0";
1227 const std::string externalUniqueId = "local:1";
1228 const std::string virtualUniqueId1 = "virtual:2";
1229 const std::string virtualUniqueId2 = "virtual:3";
1230 constexpr int32_t virtualDisplayId1 = 2;
1231 constexpr int32_t virtualDisplayId2 = 3;
1232
1233 // Add an internal viewport
1234 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001235 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT,
1236 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001237 // Add an external viewport
1238 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001239 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT,
1240 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001241 // Add an virtual viewport
1242 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001243 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT,
1244 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001245 // Add another virtual viewport
1246 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001247 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT,
1248 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001249
1250 // Check matching by type for internal
1251 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001252 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001253 ASSERT_TRUE(internalViewport);
1254 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1255
1256 // Check matching by type for external
1257 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001258 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001259 ASSERT_TRUE(externalViewport);
1260 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1261
1262 // Check matching by uniqueId for virtual viewport #1
1263 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001264 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001265 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001266 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001267 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1268 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1269
1270 // Check matching by uniqueId for virtual viewport #2
1271 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001272 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001273 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001274 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001275 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1276 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1277}
1278
1279
1280/**
1281 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1282 * that lookup works by checking display id.
1283 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1284 */
1285TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1286 const std::string uniqueId1 = "uniqueId1";
1287 const std::string uniqueId2 = "uniqueId2";
1288 constexpr int32_t displayId1 = 2;
1289 constexpr int32_t displayId2 = 3;
1290
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001291 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1292 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001293 for (const ViewportType& type : types) {
1294 mFakePolicy->clearViewports();
1295 // Add a viewport
1296 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001297 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001298 // Add another viewport
1299 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001300 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001301
1302 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001303 std::optional<DisplayViewport> viewport1 =
1304 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001305 ASSERT_TRUE(viewport1);
1306 ASSERT_EQ(displayId1, viewport1->displayId);
1307 ASSERT_EQ(type, viewport1->type);
1308
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001309 std::optional<DisplayViewport> viewport2 =
1310 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001311 ASSERT_TRUE(viewport2);
1312 ASSERT_EQ(displayId2, viewport2->displayId);
1313 ASSERT_EQ(type, viewport2->type);
1314
1315 // When there are multiple viewports of the same kind, and uniqueId is not specified
1316 // in the call to getDisplayViewport, then that situation is not supported.
1317 // The viewports can be stored in any order, so we cannot rely on the order, since that
1318 // is just implementation detail.
1319 // However, we can check that it still returns *a* viewport, we just cannot assert
1320 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001321 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001322 ASSERT_TRUE(someViewport);
1323 }
1324}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001326/**
1327 * Check getDisplayViewportByPort
1328 */
1329TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001330 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001331 const std::string uniqueId1 = "uniqueId1";
1332 const std::string uniqueId2 = "uniqueId2";
1333 constexpr int32_t displayId1 = 1;
1334 constexpr int32_t displayId2 = 2;
1335 const uint8_t hdmi1 = 0;
1336 const uint8_t hdmi2 = 1;
1337 const uint8_t hdmi3 = 2;
1338
1339 mFakePolicy->clearViewports();
1340 // Add a viewport that's associated with some display port that's not of interest.
1341 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1342 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1343 // Add another viewport, connected to HDMI1 port
1344 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1345 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1346
1347 // Check that correct display viewport was returned by comparing the display ports.
1348 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1349 ASSERT_TRUE(hdmi1Viewport);
1350 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1351 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1352
1353 // Check that we can still get the same viewport using the uniqueId
1354 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1355 ASSERT_TRUE(hdmi1Viewport);
1356 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1357 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1358 ASSERT_EQ(type, hdmi1Viewport->type);
1359
1360 // Check that we cannot find a port with "HDMI2", because we never added one
1361 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1362 ASSERT_FALSE(hdmi2Viewport);
1363}
1364
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365// --- InputReaderTest ---
1366
1367class InputReaderTest : public testing::Test {
1368protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001369 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001370 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001371 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001372 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373
Prabir Pradhan28efc192019-11-05 01:10:04 +00001374 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001375 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001376 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001377 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001378
Prabir Pradhan28efc192019-11-05 01:10:04 +00001379 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1380 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381 }
1382
Prabir Pradhan28efc192019-11-05 01:10:04 +00001383 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001384 mFakeListener.clear();
1385 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 }
1387
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001388 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1389 const PropertyMap* configuration) {
1390 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001391
1392 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001393 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001394 }
1395 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001396 mReader->loopOnce();
1397 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001398 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1399 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001400 }
1401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001402 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001403 mFakePolicy->addDisabledDevice(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. Lewisf4916ef2020-01-14 11:57:18 -08001407 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001408 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001409 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001410 }
1411
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001412 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001413 const std::string& name, uint32_t classes,
1414 uint32_t sources,
1415 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001416 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1417 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001418 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001419 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001420 return mapper;
1421 }
1422};
1423
1424TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001425 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001426 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001427 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001428 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001429
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001430 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001431 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001432 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001433 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001434 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001435 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1436 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1437 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1438
1439 // Should also have received a notification describing the new input devices.
1440 inputDevices = mFakePolicy->getInputDevices();
1441 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001442 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001443 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001444 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1445 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1446 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1447}
1448
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001449TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001450 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001451 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001452 constexpr int32_t eventHubId = 1;
1453 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001454 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001455 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001456 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001457 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001458
Yi Kong9b14ac62018-07-17 13:48:38 -07001459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001460
1461 NotifyDeviceResetArgs resetArgs;
1462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001463 ASSERT_EQ(deviceId, resetArgs.deviceId);
1464
1465 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001466 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001467 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001468
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001470 ASSERT_EQ(deviceId, resetArgs.deviceId);
1471 ASSERT_EQ(device->isEnabled(), false);
1472
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001473 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001474 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001477 ASSERT_EQ(device->isEnabled(), false);
1478
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001479 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001480 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001482 ASSERT_EQ(deviceId, resetArgs.deviceId);
1483 ASSERT_EQ(device->isEnabled(), true);
1484}
1485
Michael Wrightd02c5b62014-02-10 15:10:22 -08001486TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001487 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1488 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1489 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001490 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001491 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001492 AINPUT_SOURCE_KEYBOARD, nullptr);
1493 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001494
1495 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1496 AINPUT_SOURCE_ANY, AKEYCODE_A))
1497 << "Should return unknown when the device id is >= 0 but unknown.";
1498
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001499 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1500 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1501 << "Should return unknown when the device id is valid but the sources are not "
1502 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001504 ASSERT_EQ(AKEY_STATE_DOWN,
1505 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1506 AKEYCODE_A))
1507 << "Should return value provided by mapper when device id is valid and the device "
1508 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001509
1510 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1511 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1512 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1513
1514 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1515 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1516 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1517}
1518
1519TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001520 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1521 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1522 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001523 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001524 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001525 AINPUT_SOURCE_KEYBOARD, nullptr);
1526 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001527
1528 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1529 AINPUT_SOURCE_ANY, KEY_A))
1530 << "Should return unknown when the device id is >= 0 but unknown.";
1531
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001532 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1533 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1534 << "Should return unknown when the device id is valid but the sources are not "
1535 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001536
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001537 ASSERT_EQ(AKEY_STATE_DOWN,
1538 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1539 KEY_A))
1540 << "Should return value provided by mapper when device id is valid and the device "
1541 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001542
1543 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1544 AINPUT_SOURCE_TRACKBALL, KEY_A))
1545 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1546
1547 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1548 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1549 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1550}
1551
1552TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001553 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1554 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1555 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001556 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001557 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001558 AINPUT_SOURCE_KEYBOARD, nullptr);
1559 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001560
1561 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1562 AINPUT_SOURCE_ANY, SW_LID))
1563 << "Should return unknown when the device id is >= 0 but unknown.";
1564
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001565 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1566 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1567 << "Should return unknown when the device id is valid but the sources are not "
1568 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001570 ASSERT_EQ(AKEY_STATE_DOWN,
1571 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1572 SW_LID))
1573 << "Should return value provided by mapper when device id is valid and the device "
1574 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001575
1576 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1577 AINPUT_SOURCE_TRACKBALL, SW_LID))
1578 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1579
1580 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1581 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1582 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1583}
1584
1585TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001586 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1587 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1588 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001589 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001590 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001591 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001592
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001593 mapper.addSupportedKeyCode(AKEYCODE_A);
1594 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595
1596 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1597 uint8_t flags[4] = { 0, 0, 0, 1 };
1598
1599 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1600 << "Should return false when device id is >= 0 but unknown.";
1601 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1602
1603 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001604 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1605 << "Should return false when device id is valid but the sources are not supported by "
1606 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1608
1609 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001610 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1611 keyCodes, flags))
1612 << "Should return value provided by mapper when device id is valid and the device "
1613 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001614 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1615
1616 flags[3] = 1;
1617 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1618 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1619 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1620
1621 flags[3] = 1;
1622 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1623 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1624 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1625}
1626
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001627TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001628 constexpr int32_t eventHubId = 1;
1629 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630
1631 NotifyConfigurationChangedArgs args;
1632
1633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1634 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1635}
1636
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001637TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001638 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1639 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1640 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001641 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001643 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001644
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001645 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001646 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001647 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1648
1649 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001650 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001652 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001653 ASSERT_EQ(EV_KEY, event.type);
1654 ASSERT_EQ(KEY_A, event.code);
1655 ASSERT_EQ(1, event.value);
1656}
1657
Garfield Tan1c7bc862020-01-28 13:24:04 -08001658TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001659 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001660 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001661 constexpr int32_t eventHubId = 1;
1662 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001663 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001664 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001665 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001666 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001667
1668 NotifyDeviceResetArgs resetArgs;
1669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001670 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001671
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001672 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001673 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001675 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001676 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001677
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001678 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001679 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001681 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001682 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001683
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001684 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001685 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001687 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001688 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001689}
1690
Garfield Tan1c7bc862020-01-28 13:24:04 -08001691TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1692 constexpr int32_t deviceId = 1;
1693 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1694 constexpr int32_t eventHubId = 1;
1695 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1696 // Must add at least one mapper or the device will be ignored!
1697 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1698 mReader->setNextDevice(device);
1699 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1700
1701 NotifyDeviceResetArgs resetArgs;
1702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1703 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1704}
1705
Arthur Hungc23540e2018-11-29 20:42:11 +08001706TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001707 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001708 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001709 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001710 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001711 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1712 FakeInputMapper& mapper =
1713 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001714 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001715
1716 const uint8_t hdmi1 = 1;
1717
1718 // Associated touch screen with second display.
1719 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1720
1721 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001722 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001723 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001724 DISPLAY_ORIENTATION_0, "local:0", NO_PORT,
1725 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001726 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001727 DISPLAY_ORIENTATION_0, "local:1", hdmi1,
1728 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001729 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001730 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001731
1732 // Add the device, and make sure all of the callbacks are triggered.
1733 // The device is added after the input port associations are processed since
1734 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001735 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001738 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001739
Arthur Hung2c9a3342019-07-23 14:18:59 +08001740 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001741 ASSERT_EQ(deviceId, device->getId());
1742 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1743 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001744
1745 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001746 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001747 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001748 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001749}
1750
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001751// --- InputReaderIntegrationTest ---
1752
1753// These tests create and interact with the InputReader only through its interface.
1754// The InputReader is started during SetUp(), which starts its processing in its own
1755// thread. The tests use linux uinput to emulate input devices.
1756// NOTE: Interacting with the physical device while these tests are running may cause
1757// the tests to fail.
1758class InputReaderIntegrationTest : public testing::Test {
1759protected:
1760 sp<TestInputListener> mTestListener;
1761 sp<FakeInputReaderPolicy> mFakePolicy;
1762 sp<InputReaderInterface> mReader;
1763
1764 virtual void SetUp() override {
1765 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001766 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1767 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001768
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001769 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001770 ASSERT_EQ(mReader->start(), OK);
1771
1772 // Since this test is run on a real device, all the input devices connected
1773 // to the test device will show up in mReader. We wait for those input devices to
1774 // show up before beginning the tests.
1775 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1776 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1777 }
1778
1779 virtual void TearDown() override {
1780 ASSERT_EQ(mReader->stop(), OK);
1781 mTestListener.clear();
1782 mFakePolicy.clear();
1783 }
1784};
1785
1786TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1787 // An invalid input device that is only used for this test.
1788 class InvalidUinputDevice : public UinputDevice {
1789 public:
1790 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1791
1792 private:
1793 void configureDevice(int fd, uinput_user_dev* device) override {}
1794 };
1795
1796 const size_t numDevices = mFakePolicy->getInputDevices().size();
1797
1798 // UinputDevice does not set any event or key bits, so InputReader should not
1799 // consider it as a valid device.
1800 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1801 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1802 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1803 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1804
1805 invalidDevice.reset();
1806 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1807 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1808 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1809}
1810
1811TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1812 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1813
1814 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1815 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1817 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1818
1819 // Find the test device by its name.
1820 std::vector<InputDeviceInfo> inputDevices;
1821 mReader->getInputDevices(inputDevices);
1822 InputDeviceInfo* keyboardInfo = nullptr;
1823 const char* keyboardName = keyboard->getName();
1824 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1825 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1826 keyboardInfo = &inputDevices[i];
1827 break;
1828 }
1829 }
1830 ASSERT_NE(keyboardInfo, nullptr);
1831 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1832 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1833 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1834
1835 keyboard.reset();
1836 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1837 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1838 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1839}
1840
1841TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1842 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1843 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1844
1845 NotifyConfigurationChangedArgs configChangedArgs;
1846 ASSERT_NO_FATAL_FAILURE(
1847 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001848 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001849 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1850
1851 NotifyKeyArgs keyArgs;
1852 keyboard->pressAndReleaseHomeKey();
1853 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1854 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001855 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001856 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001857 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1858 prevTimestamp = keyArgs.eventTime;
1859
1860 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1861 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001862 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001863 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1864}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001865
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001866/**
1867 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1868 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1869 * are passed to the listener.
1870 */
1871static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1872TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1873 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1874 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1875 NotifyKeyArgs keyArgs;
1876
1877 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1878 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1879 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1880 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1881
1882 controller->pressAndReleaseKey(BTN_GEAR_UP);
1883 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1884 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1885 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1886}
1887
Arthur Hungaab25622020-01-16 11:22:11 +08001888// --- TouchProcessTest ---
1889class TouchIntegrationTest : public InputReaderIntegrationTest {
1890protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001891 const std::string UNIQUE_ID = "local:0";
1892
1893 virtual void SetUp() override {
1894 InputReaderIntegrationTest::SetUp();
1895 // At least add an internal display.
1896 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1897 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001898 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001899
1900 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1901 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1902 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1903 }
1904
1905 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1906 int32_t orientation, const std::string& uniqueId,
1907 std::optional<uint8_t> physicalPort,
1908 ViewportType viewportType) {
1909 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1910 physicalPort, viewportType);
1911 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1912 }
1913
1914 std::unique_ptr<UinputTouchScreen> mDevice;
1915};
1916
1917TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1918 NotifyMotionArgs args;
1919 const Point centerPoint = mDevice->getCenterPoint();
1920
1921 // ACTION_DOWN
1922 mDevice->sendDown(centerPoint);
1923 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1925
1926 // ACTION_MOVE
1927 mDevice->sendMove(centerPoint + Point(1, 1));
1928 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1930
1931 // ACTION_UP
1932 mDevice->sendUp();
1933 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1934 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1935}
1936
1937TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1938 NotifyMotionArgs args;
1939 const Point centerPoint = mDevice->getCenterPoint();
1940
1941 // ACTION_DOWN
1942 mDevice->sendDown(centerPoint);
1943 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1944 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1945
1946 // ACTION_POINTER_DOWN (Second slot)
1947 const Point secondPoint = centerPoint + Point(100, 100);
1948 mDevice->sendSlot(SECOND_SLOT);
1949 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1950 mDevice->sendDown(secondPoint + Point(1, 1));
1951 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1952 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1953 args.action);
1954
1955 // ACTION_MOVE (Second slot)
1956 mDevice->sendMove(secondPoint);
1957 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1959
1960 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001961 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08001962 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08001963 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08001964 args.action);
1965
1966 // ACTION_UP
1967 mDevice->sendSlot(FIRST_SLOT);
1968 mDevice->sendUp();
1969 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1970 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1971}
1972
1973TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1974 NotifyMotionArgs args;
1975 const Point centerPoint = mDevice->getCenterPoint();
1976
1977 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001978 mDevice->sendSlot(FIRST_SLOT);
1979 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001980 mDevice->sendDown(centerPoint);
1981 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1982 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1983
arthurhungcc7f9802020-04-30 17:55:40 +08001984 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001985 const Point secondPoint = centerPoint + Point(100, 100);
1986 mDevice->sendSlot(SECOND_SLOT);
1987 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1988 mDevice->sendDown(secondPoint);
1989 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1990 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1991 args.action);
1992
arthurhungcc7f9802020-04-30 17:55:40 +08001993 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001994 mDevice->sendMove(secondPoint + Point(1, 1));
1995 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1997
arthurhungcc7f9802020-04-30 17:55:40 +08001998 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1999 // a palm event.
2000 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002001 mDevice->sendToolType(MT_TOOL_PALM);
2002 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002003 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2004 args.action);
2005 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002006
arthurhungcc7f9802020-04-30 17:55:40 +08002007 // Send up to second slot, expect first slot send moving.
2008 mDevice->sendPointerUp();
2009 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002011
arthurhungcc7f9802020-04-30 17:55:40 +08002012 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002013 mDevice->sendSlot(FIRST_SLOT);
2014 mDevice->sendUp();
2015
arthurhungcc7f9802020-04-30 17:55:40 +08002016 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2017 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002018}
2019
Michael Wrightd02c5b62014-02-10 15:10:22 -08002020// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002021class InputDeviceTest : public testing::Test {
2022protected:
2023 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002024 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002025 static const int32_t DEVICE_ID;
2026 static const int32_t DEVICE_GENERATION;
2027 static const int32_t DEVICE_CONTROLLER_NUMBER;
2028 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002029 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002030
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002031 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002032 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002033 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002034 FakeInputReaderContext* mFakeContext;
2035
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002036 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002037
Prabir Pradhan28efc192019-11-05 01:10:04 +00002038 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002039 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002040 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002041 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002042 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2043
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002044 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002045 InputDeviceIdentifier identifier;
2046 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002047 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002048 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2049 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002050 }
2051
Prabir Pradhan28efc192019-11-05 01:10:04 +00002052 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002053 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002054 delete mFakeContext;
2055 mFakeListener.clear();
2056 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002057 }
2058};
2059
2060const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002061const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002062const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002063const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2064const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
2065const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
2066 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002067const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002068
2069TEST_F(InputDeviceTest, ImmutableProperties) {
2070 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002071 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002072 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002073}
2074
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002075TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2076 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002077}
2078
Michael Wrightd02c5b62014-02-10 15:10:22 -08002079TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2080 // Configuration.
2081 InputReaderConfiguration config;
2082 mDevice->configure(ARBITRARY_TIME, &config, 0);
2083
2084 // Reset.
2085 mDevice->reset(ARBITRARY_TIME);
2086
2087 NotifyDeviceResetArgs resetArgs;
2088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2089 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2090 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2091
2092 // Metadata.
2093 ASSERT_TRUE(mDevice->isIgnored());
2094 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2095
2096 InputDeviceInfo info;
2097 mDevice->getDeviceInfo(&info);
2098 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002099 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2101 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2102
2103 // State queries.
2104 ASSERT_EQ(0, mDevice->getMetaState());
2105
2106 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2107 << "Ignored device should return unknown key code state.";
2108 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2109 << "Ignored device should return unknown scan code state.";
2110 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2111 << "Ignored device should return unknown switch state.";
2112
2113 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2114 uint8_t flags[2] = { 0, 1 };
2115 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2116 << "Ignored device should never mark any key codes.";
2117 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2118 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2119}
2120
2121TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2122 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002123 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002124
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002125 FakeInputMapper& mapper1 =
2126 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002127 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2128 mapper1.setMetaState(AMETA_ALT_ON);
2129 mapper1.addSupportedKeyCode(AKEYCODE_A);
2130 mapper1.addSupportedKeyCode(AKEYCODE_B);
2131 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2132 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2133 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2134 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2135 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002136
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002137 FakeInputMapper& mapper2 =
2138 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002139 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002140
2141 InputReaderConfiguration config;
2142 mDevice->configure(ARBITRARY_TIME, &config, 0);
2143
2144 String8 propertyValue;
2145 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2146 << "Device should have read configuration during configuration phase.";
2147 ASSERT_STREQ("value", propertyValue.string());
2148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002149 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2150 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002151
2152 // Reset
2153 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002154 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2155 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002156
2157 NotifyDeviceResetArgs resetArgs;
2158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2159 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2160 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2161
2162 // Metadata.
2163 ASSERT_FALSE(mDevice->isIgnored());
2164 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2165
2166 InputDeviceInfo info;
2167 mDevice->getDeviceInfo(&info);
2168 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002169 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002170 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2171 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2172
2173 // State queries.
2174 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2175 << "Should query mappers and combine meta states.";
2176
2177 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2178 << "Should return unknown key code state when source not supported.";
2179 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2180 << "Should return unknown scan code state when source not supported.";
2181 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2182 << "Should return unknown switch state when source not supported.";
2183
2184 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2185 << "Should query mapper when source is supported.";
2186 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2187 << "Should query mapper when source is supported.";
2188 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2189 << "Should query mapper when source is supported.";
2190
2191 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2192 uint8_t flags[4] = { 0, 0, 0, 1 };
2193 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2194 << "Should do nothing when source is unsupported.";
2195 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2196 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2197 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2198 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2199
2200 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2201 << "Should query mapper when source is supported.";
2202 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2203 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2204 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2205 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2206
2207 // Event handling.
2208 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002209 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002210 mDevice->process(&event, 1);
2211
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002212 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2213 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002214}
2215
Arthur Hung2c9a3342019-07-23 14:18:59 +08002216// A single input device is associated with a specific display. Check that:
2217// 1. Device is disabled if the viewport corresponding to the associated display is not found
2218// 2. Device is disabled when setEnabled API is called
2219TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002220 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002221
2222 // First Configuration.
2223 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2224
2225 // Device should be enabled by default.
2226 ASSERT_TRUE(mDevice->isEnabled());
2227
2228 // Prepare associated info.
2229 constexpr uint8_t hdmi = 1;
2230 const std::string UNIQUE_ID = "local:1";
2231
2232 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2233 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2234 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2235 // Device should be disabled because it is associated with a specific display via
2236 // input port <-> display port association, but the corresponding display is not found
2237 ASSERT_FALSE(mDevice->isEnabled());
2238
2239 // Prepare displays.
2240 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002241 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002242 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2243 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2244 ASSERT_TRUE(mDevice->isEnabled());
2245
2246 // Device should be disabled after set disable.
2247 mFakePolicy->addDisabledDevice(mDevice->getId());
2248 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2249 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2250 ASSERT_FALSE(mDevice->isEnabled());
2251
2252 // Device should still be disabled even found the associated display.
2253 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2254 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2255 ASSERT_FALSE(mDevice->isEnabled());
2256}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257
2258// --- InputMapperTest ---
2259
2260class InputMapperTest : public testing::Test {
2261protected:
2262 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002263 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002264 static const int32_t DEVICE_ID;
2265 static const int32_t DEVICE_GENERATION;
2266 static const int32_t DEVICE_CONTROLLER_NUMBER;
2267 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002268 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002269
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002270 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002271 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002272 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002273 FakeInputReaderContext* mFakeContext;
2274 InputDevice* mDevice;
2275
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002276 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002277 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002278 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002279 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002280 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2281 InputDeviceIdentifier identifier;
2282 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002283 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002284 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002285
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002286 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002287 }
2288
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002289 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2290
Prabir Pradhan28efc192019-11-05 01:10:04 +00002291 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002292 delete mDevice;
2293 delete mFakeContext;
2294 mFakeListener.clear();
2295 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002296 }
2297
2298 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002299 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002300 }
2301
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002302 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002303 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2304 mFakeContext->updatePointerDisplay();
2305 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002306 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2307 }
2308
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002309 template <class T, typename... Args>
2310 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002311 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002312 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002313 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002314 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002315 }
2316
2317 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002318 int32_t orientation, const std::string& uniqueId,
2319 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002320 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002321 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002322 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2323 }
2324
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002325 void clearViewports() {
2326 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002327 }
2328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002329 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2330 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002331 RawEvent event;
2332 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002333 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002334 event.type = type;
2335 event.code = code;
2336 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002337 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002338 }
2339
2340 static void assertMotionRange(const InputDeviceInfo& info,
2341 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2342 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002343 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002344 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2345 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2346 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2347 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2348 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2349 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2350 }
2351
2352 static void assertPointerCoords(const PointerCoords& coords,
2353 float x, float y, float pressure, float size,
2354 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2355 float orientation, float distance) {
2356 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2357 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2358 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2359 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2360 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2361 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2362 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2363 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2364 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2365 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2366 }
2367
Michael Wright17db18e2020-06-26 20:51:44 +01002368 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002369 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002370 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002371 ASSERT_NEAR(x, actualX, 1);
2372 ASSERT_NEAR(y, actualY, 1);
2373 }
2374};
2375
2376const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002377const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002378const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2380const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2381const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002382const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383
2384// --- SwitchInputMapperTest ---
2385
2386class SwitchInputMapperTest : public InputMapperTest {
2387protected:
2388};
2389
2390TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002391 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002393 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394}
2395
2396TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002397 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002399 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002400 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002402 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002403 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404}
2405
2406TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002407 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002409 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2410 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2411 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2412 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002413
2414 NotifySwitchArgs args;
2415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2416 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002417 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2418 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419 args.switchMask);
2420 ASSERT_EQ(uint32_t(0), args.policyFlags);
2421}
2422
2423
2424// --- KeyboardInputMapperTest ---
2425
2426class KeyboardInputMapperTest : public InputMapperTest {
2427protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002428 const std::string UNIQUE_ID = "local:0";
2429
2430 void prepareDisplay(int32_t orientation);
2431
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002432 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002433 int32_t originalKeyCode, int32_t rotatedKeyCode,
2434 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435};
2436
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002437/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2438 * orientation.
2439 */
2440void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002441 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2442 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002443}
2444
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002445void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002446 int32_t originalScanCode, int32_t originalKeyCode,
2447 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448 NotifyKeyArgs args;
2449
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002450 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2452 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2453 ASSERT_EQ(originalScanCode, args.scanCode);
2454 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002455 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002457 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2459 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2460 ASSERT_EQ(originalScanCode, args.scanCode);
2461 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002462 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463}
2464
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002466 KeyboardInputMapper& mapper =
2467 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2468 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002470 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471}
2472
2473TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2474 const int32_t USAGE_A = 0x070004;
2475 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002476 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2477 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002478
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002479 KeyboardInputMapper& mapper =
2480 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2481 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002482
2483 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002484 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485 NotifyKeyArgs args;
2486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2487 ASSERT_EQ(DEVICE_ID, args.deviceId);
2488 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2489 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2490 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2491 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2492 ASSERT_EQ(KEY_HOME, args.scanCode);
2493 ASSERT_EQ(AMETA_NONE, args.metaState);
2494 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2495 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2496 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2497
2498 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002499 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2501 ASSERT_EQ(DEVICE_ID, args.deviceId);
2502 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2503 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2504 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2505 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2506 ASSERT_EQ(KEY_HOME, args.scanCode);
2507 ASSERT_EQ(AMETA_NONE, args.metaState);
2508 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2509 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2510 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2511
2512 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002513 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2514 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2516 ASSERT_EQ(DEVICE_ID, args.deviceId);
2517 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2518 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2519 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2520 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2521 ASSERT_EQ(0, args.scanCode);
2522 ASSERT_EQ(AMETA_NONE, args.metaState);
2523 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2524 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2525 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2526
2527 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002528 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2529 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2531 ASSERT_EQ(DEVICE_ID, args.deviceId);
2532 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2533 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2534 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2535 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2536 ASSERT_EQ(0, args.scanCode);
2537 ASSERT_EQ(AMETA_NONE, args.metaState);
2538 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2539 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2540 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2541
2542 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002543 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2544 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2546 ASSERT_EQ(DEVICE_ID, args.deviceId);
2547 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2548 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2549 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2550 ASSERT_EQ(0, args.keyCode);
2551 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2552 ASSERT_EQ(AMETA_NONE, args.metaState);
2553 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2554 ASSERT_EQ(0U, args.policyFlags);
2555 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2556
2557 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002558 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2559 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2561 ASSERT_EQ(DEVICE_ID, args.deviceId);
2562 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2563 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2564 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2565 ASSERT_EQ(0, args.keyCode);
2566 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2567 ASSERT_EQ(AMETA_NONE, args.metaState);
2568 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2569 ASSERT_EQ(0U, args.policyFlags);
2570 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2571}
2572
2573TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002577 KeyboardInputMapper& mapper =
2578 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2579 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580
2581 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002582 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583
2584 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002585 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002586 NotifyKeyArgs args;
2587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2591
2592 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002593 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
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 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002599 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2601 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002602 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603
2604 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002605 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2607 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002608 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2610}
2611
2612TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002613 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2615 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2616 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002618 KeyboardInputMapper& mapper =
2619 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2620 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002622 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2624 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2626 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2627 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2628 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2629 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2630 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2631}
2632
2633TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2636 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2637 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002640 KeyboardInputMapper& mapper =
2641 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2642 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002644 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002645 ASSERT_NO_FATAL_FAILURE(
2646 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2647 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2648 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2649 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2650 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2651 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2652 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002654 clearViewports();
2655 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002656 ASSERT_NO_FATAL_FAILURE(
2657 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2658 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2659 AKEYCODE_DPAD_UP, DISPLAY_ID));
2660 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2661 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2662 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2663 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002665 clearViewports();
2666 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002667 ASSERT_NO_FATAL_FAILURE(
2668 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2669 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2670 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2671 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2672 AKEYCODE_DPAD_UP, DISPLAY_ID));
2673 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2674 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002676 clearViewports();
2677 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002678 ASSERT_NO_FATAL_FAILURE(
2679 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2680 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2681 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2682 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2683 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2684 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2685 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686
2687 // Special case: if orientation changes while key is down, we still emit the same keycode
2688 // in the key up as we did in the key down.
2689 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002690 clearViewports();
2691 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002692 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2694 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2695 ASSERT_EQ(KEY_UP, args.scanCode);
2696 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2697
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002698 clearViewports();
2699 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002700 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2702 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2703 ASSERT_EQ(KEY_UP, args.scanCode);
2704 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2705}
2706
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002707TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2708 // If the keyboard is not orientation aware,
2709 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002710 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002711
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002712 KeyboardInputMapper& mapper =
2713 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2714 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002715 NotifyKeyArgs args;
2716
2717 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002718 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002720 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2722 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2723
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002724 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002725 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002727 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2729 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2730}
2731
2732TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2733 // If the keyboard is orientation aware,
2734 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002735 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002736
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002737 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002738 KeyboardInputMapper& mapper =
2739 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2740 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002741 NotifyKeyArgs args;
2742
2743 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2744 // ^--- already checked by the previous test
2745
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002746 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002747 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002748 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002750 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2752 ASSERT_EQ(DISPLAY_ID, args.displayId);
2753
2754 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002755 clearViewports();
2756 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002757 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002758 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002760 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2762 ASSERT_EQ(newDisplayId, args.displayId);
2763}
2764
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002766 KeyboardInputMapper& mapper =
2767 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2768 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002770 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002771 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002773 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002774 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775}
2776
2777TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002778 KeyboardInputMapper& mapper =
2779 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2780 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002782 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002783 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002785 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002786 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787}
2788
2789TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002790 KeyboardInputMapper& mapper =
2791 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2792 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002794 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795
2796 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2797 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002798 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799 ASSERT_TRUE(flags[0]);
2800 ASSERT_FALSE(flags[1]);
2801}
2802
2803TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002804 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2805 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2806 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2807 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2808 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2809 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002811 KeyboardInputMapper& mapper =
2812 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2813 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814
2815 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2818 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819
2820 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002821 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2822 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002823 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827
2828 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002829 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2830 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002831 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2832 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2833 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002834 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835
2836 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002837 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2838 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002839 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2840 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2841 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002842 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843
2844 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002845 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2846 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002847 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2848 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2849 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002850 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851
2852 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002853 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2854 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002855 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2856 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2857 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002858 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859
2860 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002861 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2862 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002863 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2864 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2865 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002866 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867}
2868
Arthur Hung2c9a3342019-07-23 14:18:59 +08002869TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2870 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002871 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2872 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2873 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2874 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002875
2876 // keyboard 2.
2877 const std::string USB2 = "USB2";
2878 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002879 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002880 InputDeviceIdentifier identifier;
2881 identifier.name = "KEYBOARD2";
2882 identifier.location = USB2;
2883 std::unique_ptr<InputDevice> device2 =
2884 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002885 identifier);
2886 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2887 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2888 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2889 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2890 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002892 KeyboardInputMapper& mapper =
2893 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2894 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002895
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002896 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002897 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002898 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002899 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2900 device2->reset(ARBITRARY_TIME);
2901
2902 // Prepared displays and associated info.
2903 constexpr uint8_t hdmi1 = 0;
2904 constexpr uint8_t hdmi2 = 1;
2905 const std::string SECONDARY_UNIQUE_ID = "local:1";
2906
2907 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2908 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2909
2910 // No associated display viewport found, should disable the device.
2911 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2912 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2913 ASSERT_FALSE(device2->isEnabled());
2914
2915 // Prepare second display.
2916 constexpr int32_t newDisplayId = 2;
2917 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002918 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002919 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002920 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002921 // Default device will reconfigure above, need additional reconfiguration for another device.
2922 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2923 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2924
2925 // Device should be enabled after the associated display is found.
2926 ASSERT_TRUE(mDevice->isEnabled());
2927 ASSERT_TRUE(device2->isEnabled());
2928
2929 // Test pad key events
2930 ASSERT_NO_FATAL_FAILURE(
2931 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2932 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2933 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2934 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2935 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2936 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2937 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2938
2939 ASSERT_NO_FATAL_FAILURE(
2940 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2941 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2942 AKEYCODE_DPAD_RIGHT, newDisplayId));
2943 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2944 AKEYCODE_DPAD_DOWN, newDisplayId));
2945 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2946 AKEYCODE_DPAD_LEFT, newDisplayId));
2947}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002949// --- KeyboardInputMapperTest_ExternalDevice ---
2950
2951class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2952protected:
2953 virtual void SetUp() override {
2954 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2955 }
2956};
2957
2958TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002959 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2960 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002961
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002962 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2963 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2965 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002966
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002967 KeyboardInputMapper& mapper =
2968 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2969 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002970
2971 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2972 NotifyKeyArgs args;
2973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2974 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2975
2976 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2978 ASSERT_EQ(uint32_t(0), args.policyFlags);
2979
2980 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2982 ASSERT_EQ(uint32_t(0), args.policyFlags);
2983
2984 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2986 ASSERT_EQ(uint32_t(0), args.policyFlags);
2987
2988 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2990 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2991
2992 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2994 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2995}
2996
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002997TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002998 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07002999
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003000 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003003
Powei Fengd041c5d2019-05-03 17:11:33 -07003004 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003005 KeyboardInputMapper& mapper =
3006 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3007 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003008
3009 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3010 NotifyKeyArgs args;
3011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3012 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3013
3014 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3016 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3017
3018 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3020 ASSERT_EQ(uint32_t(0), args.policyFlags);
3021
3022 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3024 ASSERT_EQ(uint32_t(0), args.policyFlags);
3025
3026 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3028 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3029
3030 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3032 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3033}
3034
Michael Wrightd02c5b62014-02-10 15:10:22 -08003035// --- CursorInputMapperTest ---
3036
3037class CursorInputMapperTest : public InputMapperTest {
3038protected:
3039 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3040
Michael Wright17db18e2020-06-26 20:51:44 +01003041 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042
Prabir Pradhan28efc192019-11-05 01:10:04 +00003043 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044 InputMapperTest::SetUp();
3045
Michael Wright17db18e2020-06-26 20:51:44 +01003046 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003047 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048 }
3049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003050 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3051 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003052
3053 void prepareDisplay(int32_t orientation) {
3054 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003055 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003056 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3057 orientation, uniqueId, NO_PORT, viewportType);
3058 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059};
3060
3061const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3062
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003063void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3064 int32_t originalY, int32_t rotatedX,
3065 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066 NotifyMotionArgs args;
3067
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003068 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3069 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3070 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3074 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3075 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3076 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3077}
3078
3079TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003083 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084}
3085
3086TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003090 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091}
3092
3093TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003095 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096
3097 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003098 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099
3100 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003101 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3102 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3104 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3105
3106 // When the bounds are set, then there should be a valid motion range.
3107 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3108
3109 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003110 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111
3112 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3113 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3114 1, 800 - 1, 0.0f, 0.0f));
3115 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3116 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3117 2, 480 - 1, 0.0f, 0.0f));
3118 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3119 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3120 0.0f, 1.0f, 0.0f, 0.0f));
3121}
3122
3123TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003125 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126
3127 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003128 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129
3130 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3131 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3132 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3133 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3134 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3135 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3136 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3137 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3138 0.0f, 1.0f, 0.0f, 0.0f));
3139}
3140
3141TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003143 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144
3145 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3146
3147 NotifyMotionArgs args;
3148
3149 // Button press.
3150 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003151 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3152 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3154 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3155 ASSERT_EQ(DEVICE_ID, args.deviceId);
3156 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3157 ASSERT_EQ(uint32_t(0), args.policyFlags);
3158 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3159 ASSERT_EQ(0, args.flags);
3160 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3161 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3162 ASSERT_EQ(0, args.edgeFlags);
3163 ASSERT_EQ(uint32_t(1), args.pointerCount);
3164 ASSERT_EQ(0, args.pointerProperties[0].id);
3165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3167 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3168 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3169 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3170 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3171
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3173 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3174 ASSERT_EQ(DEVICE_ID, args.deviceId);
3175 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3176 ASSERT_EQ(uint32_t(0), args.policyFlags);
3177 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3178 ASSERT_EQ(0, args.flags);
3179 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3180 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3181 ASSERT_EQ(0, args.edgeFlags);
3182 ASSERT_EQ(uint32_t(1), args.pointerCount);
3183 ASSERT_EQ(0, args.pointerProperties[0].id);
3184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3186 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3187 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3188 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3189 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3190
Michael Wrightd02c5b62014-02-10 15:10:22 -08003191 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003192 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3193 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3195 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3196 ASSERT_EQ(DEVICE_ID, args.deviceId);
3197 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3198 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003199 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3200 ASSERT_EQ(0, args.flags);
3201 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3202 ASSERT_EQ(0, args.buttonState);
3203 ASSERT_EQ(0, args.edgeFlags);
3204 ASSERT_EQ(uint32_t(1), args.pointerCount);
3205 ASSERT_EQ(0, args.pointerProperties[0].id);
3206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3208 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3209 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3210 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3211 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3212
3213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3214 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3215 ASSERT_EQ(DEVICE_ID, args.deviceId);
3216 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3217 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3219 ASSERT_EQ(0, args.flags);
3220 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3221 ASSERT_EQ(0, args.buttonState);
3222 ASSERT_EQ(0, args.edgeFlags);
3223 ASSERT_EQ(uint32_t(1), args.pointerCount);
3224 ASSERT_EQ(0, args.pointerProperties[0].id);
3225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3227 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3228 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3229 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3230 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3231}
3232
3233TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003235 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236
3237 NotifyMotionArgs args;
3238
3239 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003240 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3241 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3245 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3246
3247 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003248 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3249 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3253 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3254}
3255
3256TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003258 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259
3260 NotifyMotionArgs args;
3261
3262 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003263 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3264 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3266 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3268 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3269
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3273 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3274
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003276 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3277 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3281 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3282
3283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3286 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3287}
3288
3289TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003291 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292
3293 NotifyMotionArgs args;
3294
3295 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003296 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3297 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3298 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3299 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3301 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3309 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3310 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3311
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003313 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3314 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3315 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3319 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3320 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3321
3322 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003323 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3324 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003326 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3328 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3329
3330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3333 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3334}
3335
3336TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003338 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003340 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3345 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3346 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3347 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3348 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3349}
3350
3351TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 addConfigurationProperty("cursor.mode", "navigation");
3353 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003354 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003356 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3358 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3359 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3360 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3361 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3362 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3363 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3364 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3365
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003366 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3368 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3369 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3374 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3375
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003376 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3378 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3379 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3380 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3381 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3382 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3383 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3384 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3385
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003386 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3388 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3394 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3395}
3396
3397TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003399 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400
3401 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3402 mFakePointerController->setPosition(100, 200);
3403 mFakePointerController->setButtonState(0);
3404
3405 NotifyMotionArgs motionArgs;
3406 NotifyKeyArgs keyArgs;
3407
3408 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003409 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3410 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3412 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3413 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3414 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3416 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3417
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3419 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3420 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3421 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3423 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3424
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003425 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3426 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429 ASSERT_EQ(0, motionArgs.buttonState);
3430 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3432 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3433
3434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003435 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436 ASSERT_EQ(0, motionArgs.buttonState);
3437 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3439 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3440
3441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003443 ASSERT_EQ(0, motionArgs.buttonState);
3444 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3446 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3447
3448 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003449 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3450 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3451 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3453 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3454 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3455 motionArgs.buttonState);
3456 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3457 mFakePointerController->getButtonState());
3458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3459 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3460
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3463 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3464 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3465 mFakePointerController->getButtonState());
3466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3467 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3468
3469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3471 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3472 motionArgs.buttonState);
3473 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3474 mFakePointerController->getButtonState());
3475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3476 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3477
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003478 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3479 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003481 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3483 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3485 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3486
3487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003489 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3490 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3492 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3493
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003494 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3495 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3498 ASSERT_EQ(0, motionArgs.buttonState);
3499 ASSERT_EQ(0, mFakePointerController->getButtonState());
3500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3501 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 -08003502 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3503 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003504
3505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506 ASSERT_EQ(0, motionArgs.buttonState);
3507 ASSERT_EQ(0, mFakePointerController->getButtonState());
3508 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3510 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 -08003511
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3513 ASSERT_EQ(0, motionArgs.buttonState);
3514 ASSERT_EQ(0, mFakePointerController->getButtonState());
3515 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3517 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3518
3519 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003520 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3521 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3523 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3524 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003525
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3529 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3531 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3532
3533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3534 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3535 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3536 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3538 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3539
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003540 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3541 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544 ASSERT_EQ(0, motionArgs.buttonState);
3545 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3547 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3548
3549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003551 ASSERT_EQ(0, motionArgs.buttonState);
3552 ASSERT_EQ(0, mFakePointerController->getButtonState());
3553
Michael Wrightd02c5b62014-02-10 15:10:22 -08003554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3555 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3557 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3558 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3559
3560 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003561 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3562 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3564 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3565 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003566
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003568 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3570 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3572 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3573
3574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3575 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3576 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3577 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3579 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3580
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003581 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3582 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003585 ASSERT_EQ(0, motionArgs.buttonState);
3586 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3588 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 -08003589
3590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3591 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3592 ASSERT_EQ(0, motionArgs.buttonState);
3593 ASSERT_EQ(0, mFakePointerController->getButtonState());
3594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3595 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3596
Michael Wrightd02c5b62014-02-10 15:10:22 -08003597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3598 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3599 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3600
3601 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003602 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3603 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3605 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3606 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003607
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3611 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3613 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3614
3615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3616 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3617 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3618 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3620 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3621
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003622 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3623 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626 ASSERT_EQ(0, motionArgs.buttonState);
3627 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3629 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 -08003630
3631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3632 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3633 ASSERT_EQ(0, motionArgs.buttonState);
3634 ASSERT_EQ(0, mFakePointerController->getButtonState());
3635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3636 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3637
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3639 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3640 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3641
3642 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003643 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3644 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3646 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3647 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003648
Michael Wrightd02c5b62014-02-10 15:10:22 -08003649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003650 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003651 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3652 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3654 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3655
3656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3657 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3658 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3659 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3661 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3662
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003663 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3664 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003666 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003667 ASSERT_EQ(0, motionArgs.buttonState);
3668 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3670 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 -08003671
3672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3674 ASSERT_EQ(0, motionArgs.buttonState);
3675 ASSERT_EQ(0, mFakePointerController->getButtonState());
3676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3677 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3678
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3680 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3681 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3682}
3683
3684TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003685 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003686 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687
3688 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3689 mFakePointerController->setPosition(100, 200);
3690 mFakePointerController->setButtonState(0);
3691
3692 NotifyMotionArgs args;
3693
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003694 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3695 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3696 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003698 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3701 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01003702 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003703}
3704
3705TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003706 addConfigurationProperty("cursor.mode", "pointer");
3707 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003708 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003709
3710 NotifyDeviceResetArgs resetArgs;
3711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3712 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3713 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3714
3715 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3716 mFakePointerController->setPosition(100, 200);
3717 mFakePointerController->setButtonState(0);
3718
3719 NotifyMotionArgs args;
3720
3721 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003722 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3723 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3724 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3726 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3729 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01003730 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003731
3732 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003733 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3734 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3736 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3737 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3739 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3741 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3742 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3744 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3745
3746 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003747 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3748 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3750 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3751 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3753 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3755 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3756 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3758 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3759
3760 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003761 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3762 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3763 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3765 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3768 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01003769 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003770
3771 // Disable pointer capture and check that the device generation got bumped
3772 // and events are generated the usual way.
3773 const uint32_t generation = mFakeContext->getGeneration();
3774 mFakePolicy->setPointerCapture(false);
3775 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3776 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3777
3778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3779 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3780 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3781
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003782 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3783 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3784 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3786 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3789 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01003790 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003791}
3792
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003793TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003794 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003795
Garfield Tan888a6a42020-01-09 11:39:16 -08003796 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003797 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003798 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3799 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003800 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003801 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3802 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3803
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003804 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3805 mFakePointerController->setPosition(100, 200);
3806 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003807
3808 NotifyMotionArgs args;
3809 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3810 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3811 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3813 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3814 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3816 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01003817 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003818 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3819}
3820
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821// --- TouchInputMapperTest ---
3822
3823class TouchInputMapperTest : public InputMapperTest {
3824protected:
3825 static const int32_t RAW_X_MIN;
3826 static const int32_t RAW_X_MAX;
3827 static const int32_t RAW_Y_MIN;
3828 static const int32_t RAW_Y_MAX;
3829 static const int32_t RAW_TOUCH_MIN;
3830 static const int32_t RAW_TOUCH_MAX;
3831 static const int32_t RAW_TOOL_MIN;
3832 static const int32_t RAW_TOOL_MAX;
3833 static const int32_t RAW_PRESSURE_MIN;
3834 static const int32_t RAW_PRESSURE_MAX;
3835 static const int32_t RAW_ORIENTATION_MIN;
3836 static const int32_t RAW_ORIENTATION_MAX;
3837 static const int32_t RAW_DISTANCE_MIN;
3838 static const int32_t RAW_DISTANCE_MAX;
3839 static const int32_t RAW_TILT_MIN;
3840 static const int32_t RAW_TILT_MAX;
3841 static const int32_t RAW_ID_MIN;
3842 static const int32_t RAW_ID_MAX;
3843 static const int32_t RAW_SLOT_MIN;
3844 static const int32_t RAW_SLOT_MAX;
3845 static const float X_PRECISION;
3846 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003847 static const float X_PRECISION_VIRTUAL;
3848 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003849
3850 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003851 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852
3853 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3854
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003855 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003856 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003857
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858 enum Axes {
3859 POSITION = 1 << 0,
3860 TOUCH = 1 << 1,
3861 TOOL = 1 << 2,
3862 PRESSURE = 1 << 3,
3863 ORIENTATION = 1 << 4,
3864 MINOR = 1 << 5,
3865 ID = 1 << 6,
3866 DISTANCE = 1 << 7,
3867 TILT = 1 << 8,
3868 SLOT = 1 << 9,
3869 TOOL_TYPE = 1 << 10,
3870 };
3871
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003872 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3873 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003874 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003875 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003876 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877 int32_t toRawX(float displayX);
3878 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003879 float toCookedX(float rawX, float rawY);
3880 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003882 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003884 float toDisplayY(int32_t rawY, int32_t displayHeight);
3885
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886};
3887
3888const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3889const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3890const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3891const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3892const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3893const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3894const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3895const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003896const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3897const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3899const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3900const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3901const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3902const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3903const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3904const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3905const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3906const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3907const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3908const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3909const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003910const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3911 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3912const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3913 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003914const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3915 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916
3917const float TouchInputMapperTest::GEOMETRIC_SCALE =
3918 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3919 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3920
3921const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3922 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3923 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3924};
3925
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003926void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003927 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3928 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003929}
3930
3931void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3932 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3933 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003934}
3935
Santos Cordonfa5cf462017-04-05 10:37:00 -07003936void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003937 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
3938 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
3939 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003940}
3941
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003943 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3944 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3945 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3946 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003947}
3948
Jason Gerecke489fda82012-09-07 17:19:40 -07003949void TouchInputMapperTest::prepareLocationCalibration() {
3950 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3951}
3952
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953int32_t TouchInputMapperTest::toRawX(float displayX) {
3954 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3955}
3956
3957int32_t TouchInputMapperTest::toRawY(float displayY) {
3958 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3959}
3960
Jason Gerecke489fda82012-09-07 17:19:40 -07003961float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3962 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3963 return rawX;
3964}
3965
3966float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3967 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3968 return rawY;
3969}
3970
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003972 return toDisplayX(rawX, DISPLAY_WIDTH);
3973}
3974
3975float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3976 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977}
3978
3979float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003980 return toDisplayY(rawY, DISPLAY_HEIGHT);
3981}
3982
3983float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3984 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985}
3986
3987
3988// --- SingleTouchInputMapperTest ---
3989
3990class SingleTouchInputMapperTest : public TouchInputMapperTest {
3991protected:
3992 void prepareButtons();
3993 void prepareAxes(int axes);
3994
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003995 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3996 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3997 void processUp(SingleTouchInputMapper& mappery);
3998 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3999 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4000 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4001 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4002 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4003 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004};
4005
4006void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004007 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008}
4009
4010void SingleTouchInputMapperTest::prepareAxes(int axes) {
4011 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004012 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4013 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014 }
4015 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004016 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4017 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 }
4019 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004020 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4021 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022 }
4023 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004024 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4025 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026 }
4027 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004028 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4029 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 }
4031}
4032
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004033void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004034 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4035 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::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004040 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4041 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042}
4043
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004044void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004045 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046}
4047
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004048void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004049 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050}
4051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004052void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4053 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004054 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004055}
4056
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004057void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004058 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059}
4060
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004061void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4062 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004063 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4064 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065}
4066
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004067void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4068 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004069 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070}
4071
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004072void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004073 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074}
4075
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077 prepareButtons();
4078 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004079 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004081 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004082}
4083
4084TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004085 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4086 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 prepareButtons();
4088 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004089 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004091 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092}
4093
4094TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095 prepareButtons();
4096 prepareAxes(POSITION);
4097 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004098 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004100 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101}
4102
4103TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104 prepareButtons();
4105 prepareAxes(POSITION);
4106 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004107 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004109 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110}
4111
4112TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 addConfigurationProperty("touch.deviceType", "touchScreen");
4114 prepareDisplay(DISPLAY_ORIENTATION_0);
4115 prepareButtons();
4116 prepareAxes(POSITION);
4117 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004118 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119
4120 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004121 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122
4123 // Virtual key is down.
4124 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4125 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4126 processDown(mapper, x, y);
4127 processSync(mapper);
4128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4129
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004130 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131
4132 // Virtual key is up.
4133 processUp(mapper);
4134 processSync(mapper);
4135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004137 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138}
4139
4140TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 addConfigurationProperty("touch.deviceType", "touchScreen");
4142 prepareDisplay(DISPLAY_ORIENTATION_0);
4143 prepareButtons();
4144 prepareAxes(POSITION);
4145 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004146 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147
4148 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004149 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150
4151 // Virtual key is down.
4152 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4153 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4154 processDown(mapper, x, y);
4155 processSync(mapper);
4156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004158 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159
4160 // Virtual key is up.
4161 processUp(mapper);
4162 processSync(mapper);
4163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004165 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166}
4167
4168TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169 addConfigurationProperty("touch.deviceType", "touchScreen");
4170 prepareDisplay(DISPLAY_ORIENTATION_0);
4171 prepareButtons();
4172 prepareAxes(POSITION);
4173 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004174 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175
4176 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4177 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004178 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 ASSERT_TRUE(flags[0]);
4180 ASSERT_FALSE(flags[1]);
4181}
4182
4183TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 addConfigurationProperty("touch.deviceType", "touchScreen");
4185 prepareDisplay(DISPLAY_ORIENTATION_0);
4186 prepareButtons();
4187 prepareAxes(POSITION);
4188 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004189 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190
4191 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4192
4193 NotifyKeyArgs args;
4194
4195 // Press virtual key.
4196 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4197 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4198 processDown(mapper, x, y);
4199 processSync(mapper);
4200
4201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4202 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4203 ASSERT_EQ(DEVICE_ID, args.deviceId);
4204 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4205 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4206 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4207 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4208 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4209 ASSERT_EQ(KEY_HOME, args.scanCode);
4210 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4211 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4212
4213 // Release virtual key.
4214 processUp(mapper);
4215 processSync(mapper);
4216
4217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4218 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4219 ASSERT_EQ(DEVICE_ID, args.deviceId);
4220 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4221 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4222 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4223 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4224 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4225 ASSERT_EQ(KEY_HOME, args.scanCode);
4226 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4227 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4228
4229 // Should not have sent any motions.
4230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4231}
4232
4233TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234 addConfigurationProperty("touch.deviceType", "touchScreen");
4235 prepareDisplay(DISPLAY_ORIENTATION_0);
4236 prepareButtons();
4237 prepareAxes(POSITION);
4238 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004239 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240
4241 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4242
4243 NotifyKeyArgs keyArgs;
4244
4245 // Press virtual key.
4246 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4247 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4248 processDown(mapper, x, y);
4249 processSync(mapper);
4250
4251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4252 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4253 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4254 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4255 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4256 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4257 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4258 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4259 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4260 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4261 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4262
4263 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4264 // into the display area.
4265 y -= 100;
4266 processMove(mapper, x, y);
4267 processSync(mapper);
4268
4269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4270 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4271 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4272 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4273 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4274 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4275 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4276 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4277 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4278 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4280 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4281
4282 NotifyMotionArgs motionArgs;
4283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4284 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4285 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4286 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4287 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4288 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4289 ASSERT_EQ(0, motionArgs.flags);
4290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4291 ASSERT_EQ(0, motionArgs.buttonState);
4292 ASSERT_EQ(0, motionArgs.edgeFlags);
4293 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4294 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4297 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4298 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4299 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4301
4302 // Keep moving out of bounds. Should generate a pointer move.
4303 y -= 50;
4304 processMove(mapper, x, y);
4305 processSync(mapper);
4306
4307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4308 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4309 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4310 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4311 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4313 ASSERT_EQ(0, motionArgs.flags);
4314 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4315 ASSERT_EQ(0, motionArgs.buttonState);
4316 ASSERT_EQ(0, motionArgs.edgeFlags);
4317 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4318 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4321 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4322 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4323 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4324 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4325
4326 // Release out of bounds. Should generate a pointer up.
4327 processUp(mapper);
4328 processSync(mapper);
4329
4330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4331 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4332 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4333 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4334 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4335 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4336 ASSERT_EQ(0, motionArgs.flags);
4337 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4338 ASSERT_EQ(0, motionArgs.buttonState);
4339 ASSERT_EQ(0, motionArgs.edgeFlags);
4340 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4341 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4344 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4345 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4346 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4347 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4348
4349 // Should not have sent any more keys or motions.
4350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4352}
4353
4354TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 addConfigurationProperty("touch.deviceType", "touchScreen");
4356 prepareDisplay(DISPLAY_ORIENTATION_0);
4357 prepareButtons();
4358 prepareAxes(POSITION);
4359 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004360 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361
4362 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4363
4364 NotifyMotionArgs motionArgs;
4365
4366 // Initially go down out of bounds.
4367 int32_t x = -10;
4368 int32_t y = -10;
4369 processDown(mapper, x, y);
4370 processSync(mapper);
4371
4372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4373
4374 // Move into the display area. Should generate a pointer down.
4375 x = 50;
4376 y = 75;
4377 processMove(mapper, x, y);
4378 processSync(mapper);
4379
4380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4381 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4382 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4383 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4384 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4385 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4386 ASSERT_EQ(0, motionArgs.flags);
4387 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4388 ASSERT_EQ(0, motionArgs.buttonState);
4389 ASSERT_EQ(0, motionArgs.edgeFlags);
4390 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4391 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4394 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4395 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4396 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4397 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4398
4399 // Release. Should generate a pointer up.
4400 processUp(mapper);
4401 processSync(mapper);
4402
4403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4405 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4406 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4407 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4408 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4409 ASSERT_EQ(0, motionArgs.flags);
4410 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4411 ASSERT_EQ(0, motionArgs.buttonState);
4412 ASSERT_EQ(0, motionArgs.edgeFlags);
4413 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4414 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4417 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4418 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4419 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4421
4422 // Should not have sent any more keys or motions.
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4425}
4426
Santos Cordonfa5cf462017-04-05 10:37:00 -07004427TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004428 addConfigurationProperty("touch.deviceType", "touchScreen");
4429 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4430
4431 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4432 prepareButtons();
4433 prepareAxes(POSITION);
4434 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004435 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004436
4437 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4438
4439 NotifyMotionArgs motionArgs;
4440
4441 // Down.
4442 int32_t x = 100;
4443 int32_t y = 125;
4444 processDown(mapper, x, y);
4445 processSync(mapper);
4446
4447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4448 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4449 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4450 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4451 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4452 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4453 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4454 ASSERT_EQ(0, motionArgs.flags);
4455 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4456 ASSERT_EQ(0, motionArgs.buttonState);
4457 ASSERT_EQ(0, motionArgs.edgeFlags);
4458 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4459 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4462 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4463 1, 0, 0, 0, 0, 0, 0, 0));
4464 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4465 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4467
4468 // Move.
4469 x += 50;
4470 y += 75;
4471 processMove(mapper, x, y);
4472 processSync(mapper);
4473
4474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4476 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4477 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4478 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4479 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4481 ASSERT_EQ(0, motionArgs.flags);
4482 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4483 ASSERT_EQ(0, motionArgs.buttonState);
4484 ASSERT_EQ(0, motionArgs.edgeFlags);
4485 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4486 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4489 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4490 1, 0, 0, 0, 0, 0, 0, 0));
4491 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4492 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4493 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4494
4495 // Up.
4496 processUp(mapper);
4497 processSync(mapper);
4498
4499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4500 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4501 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4502 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4503 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4504 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4505 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4506 ASSERT_EQ(0, motionArgs.flags);
4507 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4508 ASSERT_EQ(0, motionArgs.buttonState);
4509 ASSERT_EQ(0, motionArgs.edgeFlags);
4510 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4511 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4514 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4515 1, 0, 0, 0, 0, 0, 0, 0));
4516 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4517 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4518 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4519
4520 // Should not have sent any more keys or motions.
4521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4523}
4524
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 addConfigurationProperty("touch.deviceType", "touchScreen");
4527 prepareDisplay(DISPLAY_ORIENTATION_0);
4528 prepareButtons();
4529 prepareAxes(POSITION);
4530 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004531 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532
4533 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4534
4535 NotifyMotionArgs motionArgs;
4536
4537 // Down.
4538 int32_t x = 100;
4539 int32_t y = 125;
4540 processDown(mapper, x, y);
4541 processSync(mapper);
4542
4543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4545 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4546 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4547 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4549 ASSERT_EQ(0, motionArgs.flags);
4550 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4551 ASSERT_EQ(0, motionArgs.buttonState);
4552 ASSERT_EQ(0, motionArgs.edgeFlags);
4553 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4554 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4557 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4558 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4559 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4560 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4561
4562 // Move.
4563 x += 50;
4564 y += 75;
4565 processMove(mapper, x, y);
4566 processSync(mapper);
4567
4568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4570 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4571 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4572 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4574 ASSERT_EQ(0, motionArgs.flags);
4575 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4576 ASSERT_EQ(0, motionArgs.buttonState);
4577 ASSERT_EQ(0, motionArgs.edgeFlags);
4578 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4579 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4582 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4583 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4584 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4585 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4586
4587 // Up.
4588 processUp(mapper);
4589 processSync(mapper);
4590
4591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4592 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4593 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4594 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4595 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4596 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4597 ASSERT_EQ(0, motionArgs.flags);
4598 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4599 ASSERT_EQ(0, motionArgs.buttonState);
4600 ASSERT_EQ(0, motionArgs.edgeFlags);
4601 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4602 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4605 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4606 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4607 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4608 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4609
4610 // Should not have sent any more keys or motions.
4611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4613}
4614
4615TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 addConfigurationProperty("touch.deviceType", "touchScreen");
4617 prepareButtons();
4618 prepareAxes(POSITION);
4619 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004620 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621
4622 NotifyMotionArgs args;
4623
4624 // Rotation 90.
4625 prepareDisplay(DISPLAY_ORIENTATION_90);
4626 processDown(mapper, toRawX(50), toRawY(75));
4627 processSync(mapper);
4628
4629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4630 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4631 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4632
4633 processUp(mapper);
4634 processSync(mapper);
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4636}
4637
4638TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639 addConfigurationProperty("touch.deviceType", "touchScreen");
4640 prepareButtons();
4641 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004642 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643
4644 NotifyMotionArgs args;
4645
4646 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004647 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 prepareDisplay(DISPLAY_ORIENTATION_0);
4649 processDown(mapper, toRawX(50), toRawY(75));
4650 processSync(mapper);
4651
4652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4653 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4654 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4655
4656 processUp(mapper);
4657 processSync(mapper);
4658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4659
4660 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004661 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 prepareDisplay(DISPLAY_ORIENTATION_90);
4663 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4664 processSync(mapper);
4665
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4667 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4668 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4669
4670 processUp(mapper);
4671 processSync(mapper);
4672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4673
4674 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004675 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676 prepareDisplay(DISPLAY_ORIENTATION_180);
4677 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4678 processSync(mapper);
4679
4680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4681 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4682 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4683
4684 processUp(mapper);
4685 processSync(mapper);
4686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4687
4688 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004689 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690 prepareDisplay(DISPLAY_ORIENTATION_270);
4691 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4692 processSync(mapper);
4693
4694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4695 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4696 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4697
4698 processUp(mapper);
4699 processSync(mapper);
4700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4701}
4702
4703TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704 addConfigurationProperty("touch.deviceType", "touchScreen");
4705 prepareDisplay(DISPLAY_ORIENTATION_0);
4706 prepareButtons();
4707 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004708 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709
4710 // These calculations are based on the input device calibration documentation.
4711 int32_t rawX = 100;
4712 int32_t rawY = 200;
4713 int32_t rawPressure = 10;
4714 int32_t rawToolMajor = 12;
4715 int32_t rawDistance = 2;
4716 int32_t rawTiltX = 30;
4717 int32_t rawTiltY = 110;
4718
4719 float x = toDisplayX(rawX);
4720 float y = toDisplayY(rawY);
4721 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4722 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4723 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4724 float distance = float(rawDistance);
4725
4726 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4727 float tiltScale = M_PI / 180;
4728 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4729 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4730 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4731 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4732
4733 processDown(mapper, rawX, rawY);
4734 processPressure(mapper, rawPressure);
4735 processToolMajor(mapper, rawToolMajor);
4736 processDistance(mapper, rawDistance);
4737 processTilt(mapper, rawTiltX, rawTiltY);
4738 processSync(mapper);
4739
4740 NotifyMotionArgs args;
4741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4743 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4744 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4745}
4746
Jason Gerecke489fda82012-09-07 17:19:40 -07004747TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004748 addConfigurationProperty("touch.deviceType", "touchScreen");
4749 prepareDisplay(DISPLAY_ORIENTATION_0);
4750 prepareLocationCalibration();
4751 prepareButtons();
4752 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004753 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004754
4755 int32_t rawX = 100;
4756 int32_t rawY = 200;
4757
4758 float x = toDisplayX(toCookedX(rawX, rawY));
4759 float y = toDisplayY(toCookedY(rawX, rawY));
4760
4761 processDown(mapper, rawX, rawY);
4762 processSync(mapper);
4763
4764 NotifyMotionArgs args;
4765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4767 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4768}
4769
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771 addConfigurationProperty("touch.deviceType", "touchScreen");
4772 prepareDisplay(DISPLAY_ORIENTATION_0);
4773 prepareButtons();
4774 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004775 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776
4777 NotifyMotionArgs motionArgs;
4778 NotifyKeyArgs keyArgs;
4779
4780 processDown(mapper, 100, 200);
4781 processSync(mapper);
4782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4783 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4784 ASSERT_EQ(0, motionArgs.buttonState);
4785
4786 // press BTN_LEFT, release BTN_LEFT
4787 processKey(mapper, BTN_LEFT, 1);
4788 processSync(mapper);
4789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4791 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4792
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4795 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4796
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797 processKey(mapper, BTN_LEFT, 0);
4798 processSync(mapper);
4799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004805 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806
4807 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4808 processKey(mapper, BTN_RIGHT, 1);
4809 processKey(mapper, BTN_MIDDLE, 1);
4810 processSync(mapper);
4811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4813 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4814 motionArgs.buttonState);
4815
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4817 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4818 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4819
4820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4822 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4823 motionArgs.buttonState);
4824
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825 processKey(mapper, BTN_RIGHT, 0);
4826 processSync(mapper);
4827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004828 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004830
4831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004833 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834
4835 processKey(mapper, BTN_MIDDLE, 0);
4836 processSync(mapper);
4837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004838 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004840
4841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004843 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844
4845 // press BTN_BACK, release BTN_BACK
4846 processKey(mapper, BTN_BACK, 1);
4847 processSync(mapper);
4848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4849 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4850 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004851
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004854 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4855
4856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4857 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4858 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859
4860 processKey(mapper, BTN_BACK, 0);
4861 processSync(mapper);
4862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004863 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004864 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004865
4866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004868 ASSERT_EQ(0, motionArgs.buttonState);
4869
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4871 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4872 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4873
4874 // press BTN_SIDE, release BTN_SIDE
4875 processKey(mapper, BTN_SIDE, 1);
4876 processSync(mapper);
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4878 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4879 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004880
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004883 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4884
4885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4886 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4887 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888
4889 processKey(mapper, BTN_SIDE, 0);
4890 processSync(mapper);
4891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004892 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004894
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004897 ASSERT_EQ(0, motionArgs.buttonState);
4898
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4900 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4901 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4902
4903 // press BTN_FORWARD, release BTN_FORWARD
4904 processKey(mapper, BTN_FORWARD, 1);
4905 processSync(mapper);
4906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4907 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4908 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004909
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004912 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4913
4914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4915 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4916 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917
4918 processKey(mapper, BTN_FORWARD, 0);
4919 processSync(mapper);
4920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004921 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004923
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004926 ASSERT_EQ(0, motionArgs.buttonState);
4927
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4929 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4930 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4931
4932 // press BTN_EXTRA, release BTN_EXTRA
4933 processKey(mapper, BTN_EXTRA, 1);
4934 processSync(mapper);
4935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4936 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4937 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004938
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004941 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4942
4943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4945 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946
4947 processKey(mapper, BTN_EXTRA, 0);
4948 processSync(mapper);
4949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004950 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004952
4953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004955 ASSERT_EQ(0, motionArgs.buttonState);
4956
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4958 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4959 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4960
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4962
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963 // press BTN_STYLUS, release BTN_STYLUS
4964 processKey(mapper, BTN_STYLUS, 1);
4965 processSync(mapper);
4966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004968 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4969
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4971 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4972 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973
4974 processKey(mapper, BTN_STYLUS, 0);
4975 processSync(mapper);
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004977 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004979
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004982 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983
4984 // press BTN_STYLUS2, release BTN_STYLUS2
4985 processKey(mapper, BTN_STYLUS2, 1);
4986 processSync(mapper);
4987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4988 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004989 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4990
4991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4992 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4993 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994
4995 processKey(mapper, BTN_STYLUS2, 0);
4996 processSync(mapper);
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004998 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005000
5001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005003 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004
5005 // release touch
5006 processUp(mapper);
5007 processSync(mapper);
5008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5009 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5010 ASSERT_EQ(0, motionArgs.buttonState);
5011}
5012
5013TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 addConfigurationProperty("touch.deviceType", "touchScreen");
5015 prepareDisplay(DISPLAY_ORIENTATION_0);
5016 prepareButtons();
5017 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005018 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019
5020 NotifyMotionArgs motionArgs;
5021
5022 // default tool type is finger
5023 processDown(mapper, 100, 200);
5024 processSync(mapper);
5025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5026 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5027 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5028
5029 // eraser
5030 processKey(mapper, BTN_TOOL_RUBBER, 1);
5031 processSync(mapper);
5032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5035
5036 // stylus
5037 processKey(mapper, BTN_TOOL_RUBBER, 0);
5038 processKey(mapper, BTN_TOOL_PEN, 1);
5039 processSync(mapper);
5040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5043
5044 // brush
5045 processKey(mapper, BTN_TOOL_PEN, 0);
5046 processKey(mapper, BTN_TOOL_BRUSH, 1);
5047 processSync(mapper);
5048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5051
5052 // pencil
5053 processKey(mapper, BTN_TOOL_BRUSH, 0);
5054 processKey(mapper, BTN_TOOL_PENCIL, 1);
5055 processSync(mapper);
5056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5059
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005060 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061 processKey(mapper, BTN_TOOL_PENCIL, 0);
5062 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5063 processSync(mapper);
5064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5067
5068 // mouse
5069 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5070 processKey(mapper, BTN_TOOL_MOUSE, 1);
5071 processSync(mapper);
5072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5075
5076 // lens
5077 processKey(mapper, BTN_TOOL_MOUSE, 0);
5078 processKey(mapper, BTN_TOOL_LENS, 1);
5079 processSync(mapper);
5080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5083
5084 // double-tap
5085 processKey(mapper, BTN_TOOL_LENS, 0);
5086 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5087 processSync(mapper);
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5091
5092 // triple-tap
5093 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5094 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5095 processSync(mapper);
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5099
5100 // quad-tap
5101 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5102 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5103 processSync(mapper);
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5107
5108 // finger
5109 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5110 processKey(mapper, BTN_TOOL_FINGER, 1);
5111 processSync(mapper);
5112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5114 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5115
5116 // stylus trumps finger
5117 processKey(mapper, BTN_TOOL_PEN, 1);
5118 processSync(mapper);
5119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5121 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5122
5123 // eraser trumps stylus
5124 processKey(mapper, BTN_TOOL_RUBBER, 1);
5125 processSync(mapper);
5126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5129
5130 // mouse trumps eraser
5131 processKey(mapper, BTN_TOOL_MOUSE, 1);
5132 processSync(mapper);
5133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5136
5137 // back to default tool type
5138 processKey(mapper, BTN_TOOL_MOUSE, 0);
5139 processKey(mapper, BTN_TOOL_RUBBER, 0);
5140 processKey(mapper, BTN_TOOL_PEN, 0);
5141 processKey(mapper, BTN_TOOL_FINGER, 0);
5142 processSync(mapper);
5143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5146}
5147
5148TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149 addConfigurationProperty("touch.deviceType", "touchScreen");
5150 prepareDisplay(DISPLAY_ORIENTATION_0);
5151 prepareButtons();
5152 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005153 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005154 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155
5156 NotifyMotionArgs motionArgs;
5157
5158 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5159 processKey(mapper, BTN_TOOL_FINGER, 1);
5160 processMove(mapper, 100, 200);
5161 processSync(mapper);
5162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5163 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5168 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5170 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5171
5172 // move a little
5173 processMove(mapper, 150, 250);
5174 processSync(mapper);
5175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5176 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5178 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5179
5180 // down when BTN_TOUCH is pressed, pressure defaults to 1
5181 processKey(mapper, BTN_TOUCH, 1);
5182 processSync(mapper);
5183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5184 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5186 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5187
5188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5189 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5191 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5192
5193 // up when BTN_TOUCH is released, hover restored
5194 processKey(mapper, BTN_TOUCH, 0);
5195 processSync(mapper);
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5197 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5199 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5200
5201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5202 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5207 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5209 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5210
5211 // exit hover when pointer goes away
5212 processKey(mapper, BTN_TOOL_FINGER, 0);
5213 processSync(mapper);
5214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5215 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5217 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5218}
5219
5220TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221 addConfigurationProperty("touch.deviceType", "touchScreen");
5222 prepareDisplay(DISPLAY_ORIENTATION_0);
5223 prepareButtons();
5224 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005225 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226
5227 NotifyMotionArgs motionArgs;
5228
5229 // initially hovering because pressure is 0
5230 processDown(mapper, 100, 200);
5231 processPressure(mapper, 0);
5232 processSync(mapper);
5233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5234 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5239 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5241 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5242
5243 // move a little
5244 processMove(mapper, 150, 250);
5245 processSync(mapper);
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5247 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5249 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5250
5251 // down when pressure is non-zero
5252 processPressure(mapper, RAW_PRESSURE_MAX);
5253 processSync(mapper);
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5255 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5257 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5258
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5260 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5262 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5263
5264 // up when pressure becomes 0, hover restored
5265 processPressure(mapper, 0);
5266 processSync(mapper);
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5268 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5270 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5271
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5278 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5280 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5281
5282 // exit hover when pointer goes away
5283 processUp(mapper);
5284 processSync(mapper);
5285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5288 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5289}
5290
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291// --- MultiTouchInputMapperTest ---
5292
5293class MultiTouchInputMapperTest : public TouchInputMapperTest {
5294protected:
5295 void prepareAxes(int axes);
5296
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005297 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5298 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5299 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5300 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5301 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5302 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5303 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5304 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5305 void processId(MultiTouchInputMapper& mapper, int32_t id);
5306 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5307 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5308 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5309 void processMTSync(MultiTouchInputMapper& mapper);
5310 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311};
5312
5313void MultiTouchInputMapperTest::prepareAxes(int axes) {
5314 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005315 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5316 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317 }
5318 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005319 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5320 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005322 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5323 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324 }
5325 }
5326 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005327 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5328 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005329 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005330 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5331 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005332 }
5333 }
5334 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005335 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5336 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337 }
5338 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005339 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5340 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341 }
5342 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005343 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5344 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345 }
5346 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005347 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5348 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349 }
5350 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005351 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5352 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353 }
5354 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005355 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005356 }
5357}
5358
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005359void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5360 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005361 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5362 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363}
5364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005365void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5366 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005367 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368}
5369
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005370void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5371 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005372 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373}
5374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005375void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005376 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377}
5378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005379void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005380 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381}
5382
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005383void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5384 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005385 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386}
5387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005388void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005389 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390}
5391
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005392void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005393 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394}
5395
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005396void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005397 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005398}
5399
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005400void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005401 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005402}
5403
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005404void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005405 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406}
5407
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005408void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5409 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005410 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411}
5412
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005413void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005414 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415}
5416
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005417void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005418 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419}
5420
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 addConfigurationProperty("touch.deviceType", "touchScreen");
5423 prepareDisplay(DISPLAY_ORIENTATION_0);
5424 prepareAxes(POSITION);
5425 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005426 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005427
5428 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5429
5430 NotifyMotionArgs motionArgs;
5431
5432 // Two fingers down at once.
5433 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5434 processPosition(mapper, x1, y1);
5435 processMTSync(mapper);
5436 processPosition(mapper, x2, y2);
5437 processMTSync(mapper);
5438 processSync(mapper);
5439
5440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5441 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5442 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5443 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5444 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5445 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5446 ASSERT_EQ(0, motionArgs.flags);
5447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5448 ASSERT_EQ(0, motionArgs.buttonState);
5449 ASSERT_EQ(0, motionArgs.edgeFlags);
5450 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5451 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5454 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5455 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5456 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5458
5459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5460 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5461 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5462 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5463 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5464 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5465 motionArgs.action);
5466 ASSERT_EQ(0, motionArgs.flags);
5467 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5468 ASSERT_EQ(0, motionArgs.buttonState);
5469 ASSERT_EQ(0, motionArgs.edgeFlags);
5470 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5471 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5473 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5476 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5478 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5479 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5480 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5481 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5482
5483 // Move.
5484 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5485 processPosition(mapper, x1, y1);
5486 processMTSync(mapper);
5487 processPosition(mapper, x2, y2);
5488 processMTSync(mapper);
5489 processSync(mapper);
5490
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5492 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5493 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5494 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5495 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5496 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5497 ASSERT_EQ(0, motionArgs.flags);
5498 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5499 ASSERT_EQ(0, motionArgs.buttonState);
5500 ASSERT_EQ(0, motionArgs.edgeFlags);
5501 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5502 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5504 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5507 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5509 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5510 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5511 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5512 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5513
5514 // First finger up.
5515 x2 += 15; y2 -= 20;
5516 processPosition(mapper, x2, y2);
5517 processMTSync(mapper);
5518 processSync(mapper);
5519
5520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5522 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5523 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5524 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5525 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5526 motionArgs.action);
5527 ASSERT_EQ(0, motionArgs.flags);
5528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5529 ASSERT_EQ(0, motionArgs.buttonState);
5530 ASSERT_EQ(0, motionArgs.edgeFlags);
5531 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5532 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5534 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5537 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5539 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5540 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5541 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5542 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5543
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5545 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5546 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5547 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5548 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5550 ASSERT_EQ(0, motionArgs.flags);
5551 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5552 ASSERT_EQ(0, motionArgs.buttonState);
5553 ASSERT_EQ(0, motionArgs.edgeFlags);
5554 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5555 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5558 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5559 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5560 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5561 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5562
5563 // Move.
5564 x2 += 20; y2 -= 25;
5565 processPosition(mapper, x2, y2);
5566 processMTSync(mapper);
5567 processSync(mapper);
5568
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5570 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5571 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5572 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5573 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5575 ASSERT_EQ(0, motionArgs.flags);
5576 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5577 ASSERT_EQ(0, motionArgs.buttonState);
5578 ASSERT_EQ(0, motionArgs.edgeFlags);
5579 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5580 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5583 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5584 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5585 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5586 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5587
5588 // New finger down.
5589 int32_t x3 = 700, y3 = 300;
5590 processPosition(mapper, x2, y2);
5591 processMTSync(mapper);
5592 processPosition(mapper, x3, y3);
5593 processMTSync(mapper);
5594 processSync(mapper);
5595
5596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5597 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5598 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5599 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5600 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5601 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5602 motionArgs.action);
5603 ASSERT_EQ(0, motionArgs.flags);
5604 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5605 ASSERT_EQ(0, motionArgs.buttonState);
5606 ASSERT_EQ(0, motionArgs.edgeFlags);
5607 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5608 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5610 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5613 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5615 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5616 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5617 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5618 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5619
5620 // Second finger up.
5621 x3 += 30; y3 -= 20;
5622 processPosition(mapper, x3, y3);
5623 processMTSync(mapper);
5624 processSync(mapper);
5625
5626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5628 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5629 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5630 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5631 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5632 motionArgs.action);
5633 ASSERT_EQ(0, motionArgs.flags);
5634 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5635 ASSERT_EQ(0, motionArgs.buttonState);
5636 ASSERT_EQ(0, motionArgs.edgeFlags);
5637 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5638 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5640 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5643 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5645 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5646 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5647 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5648 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5649
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5651 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5652 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5653 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5654 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5656 ASSERT_EQ(0, motionArgs.flags);
5657 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5658 ASSERT_EQ(0, motionArgs.buttonState);
5659 ASSERT_EQ(0, motionArgs.edgeFlags);
5660 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5661 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5664 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5665 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5666 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5667 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5668
5669 // Last finger up.
5670 processMTSync(mapper);
5671 processSync(mapper);
5672
5673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5674 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5675 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5676 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5677 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5678 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5679 ASSERT_EQ(0, motionArgs.flags);
5680 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5681 ASSERT_EQ(0, motionArgs.buttonState);
5682 ASSERT_EQ(0, motionArgs.edgeFlags);
5683 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5684 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5687 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5688 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5689 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5690 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5691
5692 // Should not have sent any more keys or motions.
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5695}
5696
5697TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698 addConfigurationProperty("touch.deviceType", "touchScreen");
5699 prepareDisplay(DISPLAY_ORIENTATION_0);
5700 prepareAxes(POSITION | ID);
5701 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005702 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703
5704 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5705
5706 NotifyMotionArgs motionArgs;
5707
5708 // Two fingers down at once.
5709 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5710 processPosition(mapper, x1, y1);
5711 processId(mapper, 1);
5712 processMTSync(mapper);
5713 processPosition(mapper, x2, y2);
5714 processId(mapper, 2);
5715 processMTSync(mapper);
5716 processSync(mapper);
5717
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5719 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5720 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5724 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5725
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5727 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5728 motionArgs.action);
5729 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5730 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5732 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5735 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5737 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5738
5739 // Move.
5740 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5741 processPosition(mapper, x1, y1);
5742 processId(mapper, 1);
5743 processMTSync(mapper);
5744 processPosition(mapper, x2, y2);
5745 processId(mapper, 2);
5746 processMTSync(mapper);
5747 processSync(mapper);
5748
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5751 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5752 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5754 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5757 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5759 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5760
5761 // First finger up.
5762 x2 += 15; y2 -= 20;
5763 processPosition(mapper, x2, y2);
5764 processId(mapper, 2);
5765 processMTSync(mapper);
5766 processSync(mapper);
5767
5768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5769 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5770 motionArgs.action);
5771 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5772 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5774 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5775 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5777 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5779 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5780
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5783 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5784 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5787 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5788
5789 // Move.
5790 x2 += 20; y2 -= 25;
5791 processPosition(mapper, x2, y2);
5792 processId(mapper, 2);
5793 processMTSync(mapper);
5794 processSync(mapper);
5795
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5798 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5799 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5802 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5803
5804 // New finger down.
5805 int32_t x3 = 700, y3 = 300;
5806 processPosition(mapper, x2, y2);
5807 processId(mapper, 2);
5808 processMTSync(mapper);
5809 processPosition(mapper, x3, y3);
5810 processId(mapper, 3);
5811 processMTSync(mapper);
5812 processSync(mapper);
5813
5814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5815 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5816 motionArgs.action);
5817 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5818 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5820 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5823 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5825 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5826
5827 // Second finger up.
5828 x3 += 30; y3 -= 20;
5829 processPosition(mapper, x3, y3);
5830 processId(mapper, 3);
5831 processMTSync(mapper);
5832 processSync(mapper);
5833
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5836 motionArgs.action);
5837 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5838 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5839 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5840 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5843 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5845 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5846
5847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5849 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5850 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5853 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5854
5855 // Last finger up.
5856 processMTSync(mapper);
5857 processSync(mapper);
5858
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5860 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5861 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5862 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5863 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5865 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5866
5867 // Should not have sent any more keys or motions.
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5870}
5871
5872TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005873 addConfigurationProperty("touch.deviceType", "touchScreen");
5874 prepareDisplay(DISPLAY_ORIENTATION_0);
5875 prepareAxes(POSITION | ID | SLOT);
5876 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005877 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005878
5879 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5880
5881 NotifyMotionArgs motionArgs;
5882
5883 // Two fingers down at once.
5884 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5885 processPosition(mapper, x1, y1);
5886 processId(mapper, 1);
5887 processSlot(mapper, 1);
5888 processPosition(mapper, x2, y2);
5889 processId(mapper, 2);
5890 processSync(mapper);
5891
5892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5893 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5894 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5895 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5898 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5899
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5901 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5902 motionArgs.action);
5903 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5904 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5905 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5906 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5909 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5911 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5912
5913 // Move.
5914 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5915 processSlot(mapper, 0);
5916 processPosition(mapper, x1, y1);
5917 processSlot(mapper, 1);
5918 processPosition(mapper, x2, y2);
5919 processSync(mapper);
5920
5921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5923 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5924 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5926 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5929 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5931 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5932
5933 // First finger up.
5934 x2 += 15; y2 -= 20;
5935 processSlot(mapper, 0);
5936 processId(mapper, -1);
5937 processSlot(mapper, 1);
5938 processPosition(mapper, x2, y2);
5939 processSync(mapper);
5940
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5942 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5943 motionArgs.action);
5944 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5945 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5947 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5948 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5950 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5952 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5953
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5956 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5957 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5958 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5960 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5961
5962 // Move.
5963 x2 += 20; y2 -= 25;
5964 processPosition(mapper, x2, y2);
5965 processSync(mapper);
5966
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5969 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5970 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5973 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5974
5975 // New finger down.
5976 int32_t x3 = 700, y3 = 300;
5977 processPosition(mapper, x2, y2);
5978 processSlot(mapper, 0);
5979 processId(mapper, 3);
5980 processPosition(mapper, x3, y3);
5981 processSync(mapper);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5985 motionArgs.action);
5986 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5987 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5989 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5992 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5994 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5995
5996 // Second finger up.
5997 x3 += 30; y3 -= 20;
5998 processSlot(mapper, 1);
5999 processId(mapper, -1);
6000 processSlot(mapper, 0);
6001 processPosition(mapper, x3, y3);
6002 processSync(mapper);
6003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6005 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6006 motionArgs.action);
6007 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6008 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6010 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6013 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6015 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6016
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6019 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6020 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6021 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6023 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6024
6025 // Last finger up.
6026 processId(mapper, -1);
6027 processSync(mapper);
6028
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6030 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6031 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6032 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6035 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6036
6037 // Should not have sent any more keys or motions.
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6040}
6041
6042TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006043 addConfigurationProperty("touch.deviceType", "touchScreen");
6044 prepareDisplay(DISPLAY_ORIENTATION_0);
6045 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006046 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047
6048 // These calculations are based on the input device calibration documentation.
6049 int32_t rawX = 100;
6050 int32_t rawY = 200;
6051 int32_t rawTouchMajor = 7;
6052 int32_t rawTouchMinor = 6;
6053 int32_t rawToolMajor = 9;
6054 int32_t rawToolMinor = 8;
6055 int32_t rawPressure = 11;
6056 int32_t rawDistance = 0;
6057 int32_t rawOrientation = 3;
6058 int32_t id = 5;
6059
6060 float x = toDisplayX(rawX);
6061 float y = toDisplayY(rawY);
6062 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6063 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6064 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6065 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6066 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6067 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6068 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6069 float distance = float(rawDistance);
6070
6071 processPosition(mapper, rawX, rawY);
6072 processTouchMajor(mapper, rawTouchMajor);
6073 processTouchMinor(mapper, rawTouchMinor);
6074 processToolMajor(mapper, rawToolMajor);
6075 processToolMinor(mapper, rawToolMinor);
6076 processPressure(mapper, rawPressure);
6077 processOrientation(mapper, rawOrientation);
6078 processDistance(mapper, rawDistance);
6079 processId(mapper, id);
6080 processMTSync(mapper);
6081 processSync(mapper);
6082
6083 NotifyMotionArgs args;
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6085 ASSERT_EQ(0, args.pointerProperties[0].id);
6086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6087 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6088 orientation, distance));
6089}
6090
6091TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092 addConfigurationProperty("touch.deviceType", "touchScreen");
6093 prepareDisplay(DISPLAY_ORIENTATION_0);
6094 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6095 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006096 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097
6098 // These calculations are based on the input device calibration documentation.
6099 int32_t rawX = 100;
6100 int32_t rawY = 200;
6101 int32_t rawTouchMajor = 140;
6102 int32_t rawTouchMinor = 120;
6103 int32_t rawToolMajor = 180;
6104 int32_t rawToolMinor = 160;
6105
6106 float x = toDisplayX(rawX);
6107 float y = toDisplayY(rawY);
6108 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6109 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6110 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6111 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6112 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6113
6114 processPosition(mapper, rawX, rawY);
6115 processTouchMajor(mapper, rawTouchMajor);
6116 processTouchMinor(mapper, rawTouchMinor);
6117 processToolMajor(mapper, rawToolMajor);
6118 processToolMinor(mapper, rawToolMinor);
6119 processMTSync(mapper);
6120 processSync(mapper);
6121
6122 NotifyMotionArgs args;
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6125 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6126}
6127
6128TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 addConfigurationProperty("touch.deviceType", "touchScreen");
6130 prepareDisplay(DISPLAY_ORIENTATION_0);
6131 prepareAxes(POSITION | TOUCH | TOOL);
6132 addConfigurationProperty("touch.size.calibration", "diameter");
6133 addConfigurationProperty("touch.size.scale", "10");
6134 addConfigurationProperty("touch.size.bias", "160");
6135 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006136 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137
6138 // These calculations are based on the input device calibration documentation.
6139 // Note: We only provide a single common touch/tool value because the device is assumed
6140 // not to emit separate values for each pointer (isSummed = 1).
6141 int32_t rawX = 100;
6142 int32_t rawY = 200;
6143 int32_t rawX2 = 150;
6144 int32_t rawY2 = 250;
6145 int32_t rawTouchMajor = 5;
6146 int32_t rawToolMajor = 8;
6147
6148 float x = toDisplayX(rawX);
6149 float y = toDisplayY(rawY);
6150 float x2 = toDisplayX(rawX2);
6151 float y2 = toDisplayY(rawY2);
6152 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6153 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6154 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6155
6156 processPosition(mapper, rawX, rawY);
6157 processTouchMajor(mapper, rawTouchMajor);
6158 processToolMajor(mapper, rawToolMajor);
6159 processMTSync(mapper);
6160 processPosition(mapper, rawX2, rawY2);
6161 processTouchMajor(mapper, rawTouchMajor);
6162 processToolMajor(mapper, rawToolMajor);
6163 processMTSync(mapper);
6164 processSync(mapper);
6165
6166 NotifyMotionArgs args;
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6168 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6169
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6171 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6172 args.action);
6173 ASSERT_EQ(size_t(2), args.pointerCount);
6174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6175 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6177 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6178}
6179
6180TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181 addConfigurationProperty("touch.deviceType", "touchScreen");
6182 prepareDisplay(DISPLAY_ORIENTATION_0);
6183 prepareAxes(POSITION | TOUCH | TOOL);
6184 addConfigurationProperty("touch.size.calibration", "area");
6185 addConfigurationProperty("touch.size.scale", "43");
6186 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006187 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188
6189 // These calculations are based on the input device calibration documentation.
6190 int32_t rawX = 100;
6191 int32_t rawY = 200;
6192 int32_t rawTouchMajor = 5;
6193 int32_t rawToolMajor = 8;
6194
6195 float x = toDisplayX(rawX);
6196 float y = toDisplayY(rawY);
6197 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6198 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6199 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6200
6201 processPosition(mapper, rawX, rawY);
6202 processTouchMajor(mapper, rawTouchMajor);
6203 processToolMajor(mapper, rawToolMajor);
6204 processMTSync(mapper);
6205 processSync(mapper);
6206
6207 NotifyMotionArgs args;
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6210 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6211}
6212
6213TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214 addConfigurationProperty("touch.deviceType", "touchScreen");
6215 prepareDisplay(DISPLAY_ORIENTATION_0);
6216 prepareAxes(POSITION | PRESSURE);
6217 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6218 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006219 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220
Michael Wrightaa449c92017-12-13 21:21:43 +00006221 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006222 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006223 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6224 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6225 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6226
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227 // These calculations are based on the input device calibration documentation.
6228 int32_t rawX = 100;
6229 int32_t rawY = 200;
6230 int32_t rawPressure = 60;
6231
6232 float x = toDisplayX(rawX);
6233 float y = toDisplayY(rawY);
6234 float pressure = float(rawPressure) * 0.01f;
6235
6236 processPosition(mapper, rawX, rawY);
6237 processPressure(mapper, rawPressure);
6238 processMTSync(mapper);
6239 processSync(mapper);
6240
6241 NotifyMotionArgs args;
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6244 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6245}
6246
6247TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248 addConfigurationProperty("touch.deviceType", "touchScreen");
6249 prepareDisplay(DISPLAY_ORIENTATION_0);
6250 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006251 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252
6253 NotifyMotionArgs motionArgs;
6254 NotifyKeyArgs keyArgs;
6255
6256 processId(mapper, 1);
6257 processPosition(mapper, 100, 200);
6258 processSync(mapper);
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6261 ASSERT_EQ(0, motionArgs.buttonState);
6262
6263 // press BTN_LEFT, release BTN_LEFT
6264 processKey(mapper, BTN_LEFT, 1);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6268 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6269
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6272 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6273
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274 processKey(mapper, BTN_LEFT, 0);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006282 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283
6284 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6285 processKey(mapper, BTN_RIGHT, 1);
6286 processKey(mapper, BTN_MIDDLE, 1);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6290 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6291 motionArgs.buttonState);
6292
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6295 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6296
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6299 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6300 motionArgs.buttonState);
6301
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302 processKey(mapper, BTN_RIGHT, 0);
6303 processSync(mapper);
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006307
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006310 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311
6312 processKey(mapper, BTN_MIDDLE, 0);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006320 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321
6322 // press BTN_BACK, release BTN_BACK
6323 processKey(mapper, BTN_BACK, 1);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6326 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6327 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006328
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006331 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6332
6333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6334 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6335 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336
6337 processKey(mapper, BTN_BACK, 0);
6338 processSync(mapper);
6339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006340 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006345 ASSERT_EQ(0, motionArgs.buttonState);
6346
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6348 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6349 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6350
6351 // press BTN_SIDE, release BTN_SIDE
6352 processKey(mapper, BTN_SIDE, 1);
6353 processSync(mapper);
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6355 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6356 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006357
Michael Wrightd02c5b62014-02-10 15:10:22 -08006358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6361
6362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6364 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365
6366 processKey(mapper, BTN_SIDE, 0);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006371
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006374 ASSERT_EQ(0, motionArgs.buttonState);
6375
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6377 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6378 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6379
6380 // press BTN_FORWARD, release BTN_FORWARD
6381 processKey(mapper, BTN_FORWARD, 1);
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6384 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6385 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006386
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006389 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6390
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6393 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394
6395 processKey(mapper, BTN_FORWARD, 0);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006403 ASSERT_EQ(0, motionArgs.buttonState);
6404
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6406 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6407 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6408
6409 // press BTN_EXTRA, release BTN_EXTRA
6410 processKey(mapper, BTN_EXTRA, 1);
6411 processSync(mapper);
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6413 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6414 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006415
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006418 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6419
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6422 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423
6424 processKey(mapper, BTN_EXTRA, 0);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006432 ASSERT_EQ(0, motionArgs.buttonState);
6433
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6435 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6436 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6437
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6439
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440 // press BTN_STYLUS, release BTN_STYLUS
6441 processKey(mapper, BTN_STYLUS, 1);
6442 processSync(mapper);
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006445 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6446
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6449 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450
6451 processKey(mapper, BTN_STYLUS, 0);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006454 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006459 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460
6461 // press BTN_STYLUS2, release BTN_STYLUS2
6462 processKey(mapper, BTN_STYLUS2, 1);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006466 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6467
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6469 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6470 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471
6472 processKey(mapper, BTN_STYLUS2, 0);
6473 processSync(mapper);
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006475 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006476 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006477
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006480 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481
6482 // release touch
6483 processId(mapper, -1);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6487 ASSERT_EQ(0, motionArgs.buttonState);
6488}
6489
6490TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006491 addConfigurationProperty("touch.deviceType", "touchScreen");
6492 prepareDisplay(DISPLAY_ORIENTATION_0);
6493 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006494 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006495
6496 NotifyMotionArgs motionArgs;
6497
6498 // default tool type is finger
6499 processId(mapper, 1);
6500 processPosition(mapper, 100, 200);
6501 processSync(mapper);
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6505
6506 // eraser
6507 processKey(mapper, BTN_TOOL_RUBBER, 1);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6512
6513 // stylus
6514 processKey(mapper, BTN_TOOL_RUBBER, 0);
6515 processKey(mapper, BTN_TOOL_PEN, 1);
6516 processSync(mapper);
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6520
6521 // brush
6522 processKey(mapper, BTN_TOOL_PEN, 0);
6523 processKey(mapper, BTN_TOOL_BRUSH, 1);
6524 processSync(mapper);
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6528
6529 // pencil
6530 processKey(mapper, BTN_TOOL_BRUSH, 0);
6531 processKey(mapper, BTN_TOOL_PENCIL, 1);
6532 processSync(mapper);
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6536
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006537 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538 processKey(mapper, BTN_TOOL_PENCIL, 0);
6539 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6544
6545 // mouse
6546 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6547 processKey(mapper, BTN_TOOL_MOUSE, 1);
6548 processSync(mapper);
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6552
6553 // lens
6554 processKey(mapper, BTN_TOOL_MOUSE, 0);
6555 processKey(mapper, BTN_TOOL_LENS, 1);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6560
6561 // double-tap
6562 processKey(mapper, BTN_TOOL_LENS, 0);
6563 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6568
6569 // triple-tap
6570 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6571 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6576
6577 // quad-tap
6578 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6579 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6580 processSync(mapper);
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6583 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6584
6585 // finger
6586 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6587 processKey(mapper, BTN_TOOL_FINGER, 1);
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6592
6593 // stylus trumps finger
6594 processKey(mapper, BTN_TOOL_PEN, 1);
6595 processSync(mapper);
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6599
6600 // eraser trumps stylus
6601 processKey(mapper, BTN_TOOL_RUBBER, 1);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6606
6607 // mouse trumps eraser
6608 processKey(mapper, BTN_TOOL_MOUSE, 1);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6613
6614 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6615 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6616 processSync(mapper);
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6620
6621 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6622 processToolType(mapper, MT_TOOL_PEN);
6623 processSync(mapper);
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6627
6628 // back to default tool type
6629 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6630 processKey(mapper, BTN_TOOL_MOUSE, 0);
6631 processKey(mapper, BTN_TOOL_RUBBER, 0);
6632 processKey(mapper, BTN_TOOL_PEN, 0);
6633 processKey(mapper, BTN_TOOL_FINGER, 0);
6634 processSync(mapper);
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6638}
6639
6640TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006641 addConfigurationProperty("touch.deviceType", "touchScreen");
6642 prepareDisplay(DISPLAY_ORIENTATION_0);
6643 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006644 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006645 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006646
6647 NotifyMotionArgs motionArgs;
6648
6649 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6650 processId(mapper, 1);
6651 processPosition(mapper, 100, 200);
6652 processSync(mapper);
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6654 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6661 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6662
6663 // move a little
6664 processPosition(mapper, 150, 250);
6665 processSync(mapper);
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6669 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6670
6671 // down when BTN_TOUCH is pressed, pressure defaults to 1
6672 processKey(mapper, BTN_TOUCH, 1);
6673 processSync(mapper);
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6677 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6678
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6682 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6683
6684 // up when BTN_TOUCH is released, hover restored
6685 processKey(mapper, BTN_TOUCH, 0);
6686 processSync(mapper);
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6688 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6690 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6691
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6693 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6698 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6700 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6701
6702 // exit hover when pointer goes away
6703 processId(mapper, -1);
6704 processSync(mapper);
6705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6706 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6708 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6709}
6710
6711TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006712 addConfigurationProperty("touch.deviceType", "touchScreen");
6713 prepareDisplay(DISPLAY_ORIENTATION_0);
6714 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006715 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006716
6717 NotifyMotionArgs motionArgs;
6718
6719 // initially hovering because pressure is 0
6720 processId(mapper, 1);
6721 processPosition(mapper, 100, 200);
6722 processPressure(mapper, 0);
6723 processSync(mapper);
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6725 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6730 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6732 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6733
6734 // move a little
6735 processPosition(mapper, 150, 250);
6736 processSync(mapper);
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6738 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6740 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6741
6742 // down when pressure becomes non-zero
6743 processPressure(mapper, RAW_PRESSURE_MAX);
6744 processSync(mapper);
6745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6746 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6748 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6749
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6751 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6753 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6754
6755 // up when pressure becomes 0, hover restored
6756 processPressure(mapper, 0);
6757 processSync(mapper);
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6759 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6761 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6762
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6764 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6769 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6771 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6772
6773 // exit hover when pointer goes away
6774 processId(mapper, -1);
6775 processSync(mapper);
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6777 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6779 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6780}
6781
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006782/**
6783 * Set the input device port <--> display port associations, and check that the
6784 * events are routed to the display that matches the display port.
6785 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6786 */
6787TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006788 const std::string usb2 = "USB2";
6789 const uint8_t hdmi1 = 0;
6790 const uint8_t hdmi2 = 1;
6791 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006792 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006793
6794 addConfigurationProperty("touch.deviceType", "touchScreen");
6795 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006796 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006797
6798 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6799 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6800
6801 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6802 // for this input device is specified, and the matching viewport is not present,
6803 // the input device should be disabled (at the mapper level).
6804
6805 // Add viewport for display 2 on hdmi2
6806 prepareSecondaryDisplay(type, hdmi2);
6807 // Send a touch event
6808 processPosition(mapper, 100, 100);
6809 processSync(mapper);
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6811
6812 // Add viewport for display 1 on hdmi1
6813 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6814 // Send a touch event again
6815 processPosition(mapper, 100, 100);
6816 processSync(mapper);
6817
6818 NotifyMotionArgs args;
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6820 ASSERT_EQ(DISPLAY_ID, args.displayId);
6821}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006822
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006823TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006824 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006825 std::shared_ptr<FakePointerController> fakePointerController =
6826 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006827 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006828 fakePointerController->setPosition(100, 200);
6829 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006830 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6831
Garfield Tan888a6a42020-01-09 11:39:16 -08006832 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006833 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006834
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006835 prepareDisplay(DISPLAY_ORIENTATION_0);
6836 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006837 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006838
6839 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006840 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006841
6842 NotifyMotionArgs motionArgs;
6843 processPosition(mapper, 100, 100);
6844 processSync(mapper);
6845
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6847 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6848 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6849}
6850
Arthur Hung7c645402019-01-25 17:45:42 +08006851TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6852 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006853 prepareAxes(POSITION | ID | SLOT);
6854 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006855 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006856
6857 // Create the second touch screen device, and enable multi fingers.
6858 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006859 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006860 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006861 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006862 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006863 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006864 std::unique_ptr<InputDevice> device2 =
6865 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006866 identifier);
6867 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6868 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6869 0 /*flat*/, 0 /*fuzz*/);
6870 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6871 0 /*flat*/, 0 /*fuzz*/);
6872 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6873 0 /*flat*/, 0 /*fuzz*/);
6874 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6875 0 /*flat*/, 0 /*fuzz*/);
6876 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6877 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6878 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006879
6880 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006881 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006882 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6883 device2->reset(ARBITRARY_TIME);
6884
6885 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006886 std::shared_ptr<FakePointerController> fakePointerController =
6887 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006888 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6889 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6890
6891 // Setup policy for associated displays and show touches.
6892 const uint8_t hdmi1 = 0;
6893 const uint8_t hdmi2 = 1;
6894 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6895 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6896 mFakePolicy->setShowTouches(true);
6897
6898 // Create displays.
6899 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006900 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006901
6902 // Default device will reconfigure above, need additional reconfiguration for another device.
6903 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006904 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08006905
6906 // Two fingers down at default display.
6907 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6908 processPosition(mapper, x1, y1);
6909 processId(mapper, 1);
6910 processSlot(mapper, 1);
6911 processPosition(mapper, x2, y2);
6912 processId(mapper, 2);
6913 processSync(mapper);
6914
6915 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6916 fakePointerController->getSpots().find(DISPLAY_ID);
6917 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6918 ASSERT_EQ(size_t(2), iter->second.size());
6919
6920 // Two fingers down at second display.
6921 processPosition(mapper2, x1, y1);
6922 processId(mapper2, 1);
6923 processSlot(mapper2, 1);
6924 processPosition(mapper2, x2, y2);
6925 processId(mapper2, 2);
6926 processSync(mapper2);
6927
6928 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6929 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6930 ASSERT_EQ(size_t(2), iter->second.size());
6931}
6932
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006933TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006934 prepareAxes(POSITION);
6935 addConfigurationProperty("touch.deviceType", "touchScreen");
6936 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006937 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006938
6939 NotifyMotionArgs motionArgs;
6940 // Unrotated video frame
6941 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6942 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006943 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006944 processPosition(mapper, 100, 200);
6945 processSync(mapper);
6946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6947 ASSERT_EQ(frames, motionArgs.videoFrames);
6948
6949 // Subsequent touch events should not have any videoframes
6950 // This is implemented separately in FakeEventHub,
6951 // but that should match the behaviour of TouchVideoDevice.
6952 processPosition(mapper, 200, 200);
6953 processSync(mapper);
6954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6955 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6956}
6957
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006958TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006959 prepareAxes(POSITION);
6960 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006961 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006962 // Unrotated video frame
6963 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6964 NotifyMotionArgs motionArgs;
6965
6966 // Test all 4 orientations
6967 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6968 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6969 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6970 clearViewports();
6971 prepareDisplay(orientation);
6972 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006973 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006974 processPosition(mapper, 100, 200);
6975 processSync(mapper);
6976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6977 frames[0].rotate(orientation);
6978 ASSERT_EQ(frames, motionArgs.videoFrames);
6979 }
6980}
6981
6982TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006983 prepareAxes(POSITION);
6984 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006985 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006986 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6987 // so mix these.
6988 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6989 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6990 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6991 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6992 NotifyMotionArgs motionArgs;
6993
6994 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006995 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006996 processPosition(mapper, 100, 200);
6997 processSync(mapper);
6998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6999 std::for_each(frames.begin(), frames.end(),
7000 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7001 ASSERT_EQ(frames, motionArgs.videoFrames);
7002}
7003
Arthur Hung9da14732019-09-02 16:16:58 +08007004/**
7005 * If we had defined port associations, but the viewport is not ready, the touch device would be
7006 * expected to be disabled, and it should be enabled after the viewport has found.
7007 */
7008TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007009 constexpr uint8_t hdmi2 = 1;
7010 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007011 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007012
7013 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7014
7015 addConfigurationProperty("touch.deviceType", "touchScreen");
7016 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007017 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007018
7019 ASSERT_EQ(mDevice->isEnabled(), false);
7020
7021 // Add display on hdmi2, the device should be enabled and can receive touch event.
7022 prepareSecondaryDisplay(type, hdmi2);
7023 ASSERT_EQ(mDevice->isEnabled(), true);
7024
7025 // Send a touch event.
7026 processPosition(mapper, 100, 100);
7027 processSync(mapper);
7028
7029 NotifyMotionArgs args;
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7031 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7032}
7033
Arthur Hung6cd19a42019-08-30 19:04:12 +08007034
Arthur Hung6cd19a42019-08-30 19:04:12 +08007035
Arthur Hung421eb1c2020-01-16 00:09:42 +08007036TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007037 addConfigurationProperty("touch.deviceType", "touchScreen");
7038 prepareDisplay(DISPLAY_ORIENTATION_0);
7039 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007040 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007041
7042 NotifyMotionArgs motionArgs;
7043
7044 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7045 // finger down
7046 processId(mapper, 1);
7047 processPosition(mapper, x1, y1);
7048 processSync(mapper);
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7050 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7052
7053 // finger move
7054 processId(mapper, 1);
7055 processPosition(mapper, x2, y2);
7056 processSync(mapper);
7057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7060
7061 // finger up.
7062 processId(mapper, -1);
7063 processSync(mapper);
7064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7065 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7067
7068 // new finger down
7069 processId(mapper, 1);
7070 processPosition(mapper, x3, y3);
7071 processSync(mapper);
7072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7073 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7075}
7076
7077/**
arthurhungcc7f9802020-04-30 17:55:40 +08007078 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7079 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007080 */
arthurhungcc7f9802020-04-30 17:55:40 +08007081TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007082 addConfigurationProperty("touch.deviceType", "touchScreen");
7083 prepareDisplay(DISPLAY_ORIENTATION_0);
7084 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007085 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007086
7087 NotifyMotionArgs motionArgs;
7088
7089 // default tool type is finger
7090 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007091 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007092 processPosition(mapper, x1, y1);
7093 processSync(mapper);
7094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7095 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7096 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7097
7098 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7099 processToolType(mapper, MT_TOOL_PALM);
7100 processSync(mapper);
7101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7102 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7103
7104 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007105 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007106 processPosition(mapper, x2, y2);
7107 processSync(mapper);
7108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7109
7110 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007111 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007112 processSync(mapper);
7113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7114
7115 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007116 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007117 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007118 processPosition(mapper, x3, y3);
7119 processSync(mapper);
7120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7121 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7123}
7124
arthurhungbf89a482020-04-17 17:37:55 +08007125/**
arthurhungcc7f9802020-04-30 17:55:40 +08007126 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7127 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007128 */
arthurhungcc7f9802020-04-30 17:55:40 +08007129TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007130 addConfigurationProperty("touch.deviceType", "touchScreen");
7131 prepareDisplay(DISPLAY_ORIENTATION_0);
7132 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7133 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7134
7135 NotifyMotionArgs motionArgs;
7136
7137 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007138 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7139 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007140 processPosition(mapper, x1, y1);
7141 processSync(mapper);
7142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7143 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7145
7146 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007147 processSlot(mapper, SECOND_SLOT);
7148 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007149 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007150 processSync(mapper);
7151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7152 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7153 motionArgs.action);
7154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7155
7156 // If the tool type of the first finger changes to MT_TOOL_PALM,
7157 // we expect to receive ACTION_POINTER_UP with cancel flag.
7158 processSlot(mapper, FIRST_SLOT);
7159 processId(mapper, FIRST_TRACKING_ID);
7160 processToolType(mapper, MT_TOOL_PALM);
7161 processSync(mapper);
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7163 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7164 motionArgs.action);
7165 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7166
7167 // The following MOVE events of second finger should be processed.
7168 processSlot(mapper, SECOND_SLOT);
7169 processId(mapper, SECOND_TRACKING_ID);
7170 processPosition(mapper, x2 + 1, y2 + 1);
7171 processSync(mapper);
7172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7174 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7175
7176 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7177 // it. Second finger receive move.
7178 processSlot(mapper, FIRST_SLOT);
7179 processId(mapper, INVALID_TRACKING_ID);
7180 processSync(mapper);
7181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7183 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7184
7185 // Second finger keeps moving.
7186 processSlot(mapper, SECOND_SLOT);
7187 processId(mapper, SECOND_TRACKING_ID);
7188 processPosition(mapper, x2 + 2, y2 + 2);
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7192 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7193
7194 // Second finger up.
7195 processId(mapper, INVALID_TRACKING_ID);
7196 processSync(mapper);
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7198 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7199 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7200}
7201
7202/**
7203 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7204 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7205 */
7206TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7207 addConfigurationProperty("touch.deviceType", "touchScreen");
7208 prepareDisplay(DISPLAY_ORIENTATION_0);
7209 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7210 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7211
7212 NotifyMotionArgs motionArgs;
7213
7214 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7215 // First finger down.
7216 processId(mapper, FIRST_TRACKING_ID);
7217 processPosition(mapper, x1, y1);
7218 processSync(mapper);
7219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7220 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7222
7223 // Second finger down.
7224 processSlot(mapper, SECOND_SLOT);
7225 processId(mapper, SECOND_TRACKING_ID);
7226 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007227 processSync(mapper);
7228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7229 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7230 motionArgs.action);
7231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7232
arthurhungcc7f9802020-04-30 17:55:40 +08007233 // If the tool type of the first finger changes to MT_TOOL_PALM,
7234 // we expect to receive ACTION_POINTER_UP with cancel flag.
7235 processSlot(mapper, FIRST_SLOT);
7236 processId(mapper, FIRST_TRACKING_ID);
7237 processToolType(mapper, MT_TOOL_PALM);
7238 processSync(mapper);
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7240 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7241 motionArgs.action);
7242 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7243
7244 // Second finger keeps moving.
7245 processSlot(mapper, SECOND_SLOT);
7246 processId(mapper, SECOND_TRACKING_ID);
7247 processPosition(mapper, x2 + 1, y2 + 1);
7248 processSync(mapper);
7249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7251
7252 // second finger becomes palm, receive cancel due to only 1 finger is active.
7253 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007254 processToolType(mapper, MT_TOOL_PALM);
7255 processSync(mapper);
7256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7257 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7258
arthurhungcc7f9802020-04-30 17:55:40 +08007259 // third finger down.
7260 processSlot(mapper, THIRD_SLOT);
7261 processId(mapper, THIRD_TRACKING_ID);
7262 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007263 processPosition(mapper, x3, y3);
7264 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7266 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007268 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7269
7270 // third finger move
7271 processId(mapper, THIRD_TRACKING_ID);
7272 processPosition(mapper, x3 + 1, y3 + 1);
7273 processSync(mapper);
7274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7276
7277 // first finger up, third finger receive move.
7278 processSlot(mapper, FIRST_SLOT);
7279 processId(mapper, INVALID_TRACKING_ID);
7280 processSync(mapper);
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7283 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7284
7285 // second finger up, third finger receive move.
7286 processSlot(mapper, SECOND_SLOT);
7287 processId(mapper, INVALID_TRACKING_ID);
7288 processSync(mapper);
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7291 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7292
7293 // third finger up.
7294 processSlot(mapper, THIRD_SLOT);
7295 processId(mapper, INVALID_TRACKING_ID);
7296 processSync(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7299 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7300}
7301
7302/**
7303 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7304 * and the active finger could still be allowed to receive the events
7305 */
7306TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7307 addConfigurationProperty("touch.deviceType", "touchScreen");
7308 prepareDisplay(DISPLAY_ORIENTATION_0);
7309 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7310 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7311
7312 NotifyMotionArgs motionArgs;
7313
7314 // default tool type is finger
7315 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7316 processId(mapper, FIRST_TRACKING_ID);
7317 processPosition(mapper, x1, y1);
7318 processSync(mapper);
7319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7320 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7322
7323 // Second finger down.
7324 processSlot(mapper, SECOND_SLOT);
7325 processId(mapper, SECOND_TRACKING_ID);
7326 processPosition(mapper, x2, y2);
7327 processSync(mapper);
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7329 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7330 motionArgs.action);
7331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7332
7333 // If the tool type of the second finger changes to MT_TOOL_PALM,
7334 // we expect to receive ACTION_POINTER_UP with cancel flag.
7335 processId(mapper, SECOND_TRACKING_ID);
7336 processToolType(mapper, MT_TOOL_PALM);
7337 processSync(mapper);
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7339 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7340 motionArgs.action);
7341 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7342
7343 // The following MOVE event should be processed.
7344 processSlot(mapper, FIRST_SLOT);
7345 processId(mapper, FIRST_TRACKING_ID);
7346 processPosition(mapper, x1 + 1, y1 + 1);
7347 processSync(mapper);
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7350 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7351
7352 // second finger up.
7353 processSlot(mapper, SECOND_SLOT);
7354 processId(mapper, INVALID_TRACKING_ID);
7355 processSync(mapper);
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7358
7359 // first finger keep moving
7360 processSlot(mapper, FIRST_SLOT);
7361 processId(mapper, FIRST_TRACKING_ID);
7362 processPosition(mapper, x1 + 2, y1 + 2);
7363 processSync(mapper);
7364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7366
7367 // first finger up.
7368 processId(mapper, INVALID_TRACKING_ID);
7369 processSync(mapper);
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7371 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7372 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007373}
7374
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007375// --- MultiTouchInputMapperTest_ExternalDevice ---
7376
7377class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7378protected:
7379 virtual void SetUp() override {
7380 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
7381 }
7382};
7383
7384/**
7385 * Expect fallback to internal viewport if device is external and external viewport is not present.
7386 */
7387TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7388 prepareAxes(POSITION);
7389 addConfigurationProperty("touch.deviceType", "touchScreen");
7390 prepareDisplay(DISPLAY_ORIENTATION_0);
7391 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7392
7393 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7394
7395 NotifyMotionArgs motionArgs;
7396
7397 // Expect the event to be sent to the internal viewport,
7398 // because an external viewport is not present.
7399 processPosition(mapper, 100, 100);
7400 processSync(mapper);
7401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7402 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7403
7404 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007405 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007406 processPosition(mapper, 100, 100);
7407 processSync(mapper);
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7409 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7410}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007411
7412/**
7413 * Test touch should not work if outside of surface.
7414 */
7415class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7416protected:
7417 void halfDisplayToCenterHorizontal(int32_t orientation) {
7418 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007419 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007420
7421 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7422 internalViewport->orientation = orientation;
7423 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7424 internalViewport->logicalLeft = 0;
7425 internalViewport->logicalTop = 0;
7426 internalViewport->logicalRight = DISPLAY_HEIGHT;
7427 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7428
7429 internalViewport->physicalLeft = 0;
7430 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7431 internalViewport->physicalRight = DISPLAY_HEIGHT;
7432 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7433
7434 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7435 internalViewport->deviceHeight = DISPLAY_WIDTH;
7436 } else {
7437 internalViewport->logicalLeft = 0;
7438 internalViewport->logicalTop = 0;
7439 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7440 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7441
7442 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7443 internalViewport->physicalTop = 0;
7444 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7445 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7446
7447 internalViewport->deviceWidth = DISPLAY_WIDTH;
7448 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7449 }
7450
7451 mFakePolicy->updateViewport(internalViewport.value());
7452 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7453 }
7454
7455 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7456 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7457 int32_t yExpected) {
7458 // touch on outside area should not work.
7459 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7460 processSync(mapper);
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7462
7463 // touch on inside area should receive the event.
7464 NotifyMotionArgs args;
7465 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7466 processSync(mapper);
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7468 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7469 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7470
7471 // Reset.
7472 mapper.reset(ARBITRARY_TIME);
7473 }
7474};
7475
7476TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7477 addConfigurationProperty("touch.deviceType", "touchScreen");
7478 prepareDisplay(DISPLAY_ORIENTATION_0);
7479 prepareAxes(POSITION);
7480 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7481
7482 // Touch on center of normal display should work.
7483 const int32_t x = DISPLAY_WIDTH / 4;
7484 const int32_t y = DISPLAY_HEIGHT / 2;
7485 processPosition(mapper, toRawX(x), toRawY(y));
7486 processSync(mapper);
7487 NotifyMotionArgs args;
7488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7490 0.0f, 0.0f, 0.0f, 0.0f));
7491 // Reset.
7492 mapper.reset(ARBITRARY_TIME);
7493
7494 // Let physical display be different to device, and make surface and physical could be 1:1.
7495 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7496
7497 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7498 const int32_t yExpected = y;
7499 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7500}
7501
7502TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7503 addConfigurationProperty("touch.deviceType", "touchScreen");
7504 prepareDisplay(DISPLAY_ORIENTATION_0);
7505 prepareAxes(POSITION);
7506 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7507
7508 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7509 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7510
7511 const int32_t x = DISPLAY_WIDTH / 4;
7512 const int32_t y = DISPLAY_HEIGHT / 2;
7513
7514 // expect x/y = swap x/y then reverse y.
7515 const int32_t xExpected = y;
7516 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7517 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7518}
7519
7520TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7521 addConfigurationProperty("touch.deviceType", "touchScreen");
7522 prepareDisplay(DISPLAY_ORIENTATION_0);
7523 prepareAxes(POSITION);
7524 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7525
7526 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7527 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7528
7529 const int32_t x = DISPLAY_WIDTH / 4;
7530 const int32_t y = DISPLAY_HEIGHT / 2;
7531
7532 // expect x/y = swap x/y then reverse x.
7533 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7534 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7535 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7536}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007537} // namespace android