blob: 232045be89fd0bb607164c642e4cba86f9d8d009 [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
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800300 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
301
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700303 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700304 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
305 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700306 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
307 || orientation == DISPLAY_ORIENTATION_270);
308 DisplayViewport v;
309 v.displayId = displayId;
310 v.orientation = orientation;
311 v.logicalLeft = 0;
312 v.logicalTop = 0;
313 v.logicalRight = isRotated ? height : width;
314 v.logicalBottom = isRotated ? width : height;
315 v.physicalLeft = 0;
316 v.physicalTop = 0;
317 v.physicalRight = isRotated ? height : width;
318 v.physicalBottom = isRotated ? width : height;
319 v.deviceWidth = isRotated ? height : width;
320 v.deviceHeight = isRotated ? width : height;
321 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700322 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100323 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700324 return v;
325 }
326
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
328 *outConfig = mConfig;
329 }
330
Michael Wright17db18e2020-06-26 20:51:44 +0100331 virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
332 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 }
334
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800335 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700336 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700338 mInputDevicesChanged = true;
339 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 }
341
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100342 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700343 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100346 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
347 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800349
350 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
351 std::unique_lock<std::mutex> lock(mLock);
352 base::ScopedLockAssertion assumeLocked(mLock);
353
354 const bool devicesChanged =
355 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
356 return mInputDevicesChanged;
357 });
358 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
359 mInputDevicesChanged = false;
360 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361};
362
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363// --- FakeEventHub ---
364
365class FakeEventHub : public EventHubInterface {
366 struct KeyInfo {
367 int32_t keyCode;
368 uint32_t flags;
369 };
370
371 struct Device {
372 InputDeviceIdentifier identifier;
373 uint32_t classes;
374 PropertyMap configuration;
375 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
376 KeyedVector<int, bool> relativeAxes;
377 KeyedVector<int32_t, int32_t> keyCodeStates;
378 KeyedVector<int32_t, int32_t> scanCodeStates;
379 KeyedVector<int32_t, int32_t> switchStates;
380 KeyedVector<int32_t, int32_t> absoluteAxisValue;
381 KeyedVector<int32_t, KeyInfo> keysByScanCode;
382 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
383 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800384 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700385 bool enabled;
386
387 status_t enable() {
388 enabled = true;
389 return OK;
390 }
391
392 status_t disable() {
393 enabled = false;
394 return OK;
395 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800396
Chih-Hung Hsieh6ca70ef2016-04-29 16:23:55 -0700397 explicit Device(uint32_t classes) :
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700398 classes(classes), enabled(true) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399 }
400 };
401
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700402 std::mutex mLock;
403 std::condition_variable mEventsCondition;
404
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100406 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700407 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600408 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700410public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 virtual ~FakeEventHub() {
412 for (size_t i = 0; i < mDevices.size(); i++) {
413 delete mDevices.valueAt(i);
414 }
415 }
416
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 FakeEventHub() { }
418
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100419 void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 Device* device = new Device(classes);
421 device->identifier.name = name;
422 mDevices.add(deviceId, device);
423
424 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
425 }
426
427 void removeDevice(int32_t deviceId) {
428 delete mDevices.valueFor(deviceId);
429 mDevices.removeItem(deviceId);
430
431 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
432 }
433
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700434 bool isDeviceEnabled(int32_t deviceId) {
435 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700436 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700437 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
438 return false;
439 }
440 return device->enabled;
441 }
442
443 status_t enableDevice(int32_t deviceId) {
444 status_t result;
445 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700446 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700447 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
448 return BAD_VALUE;
449 }
450 if (device->enabled) {
451 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
452 return OK;
453 }
454 result = device->enable();
455 return result;
456 }
457
458 status_t disableDevice(int32_t deviceId) {
459 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700460 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700461 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
462 return BAD_VALUE;
463 }
464 if (!device->enabled) {
465 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
466 return OK;
467 }
468 return device->disable();
469 }
470
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471 void finishDeviceScan() {
472 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
473 }
474
475 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
476 Device* device = getDevice(deviceId);
477 device->configuration.addProperty(key, value);
478 }
479
480 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
481 Device* device = getDevice(deviceId);
482 device->configuration.addAll(configuration);
483 }
484
485 void addAbsoluteAxis(int32_t deviceId, int axis,
486 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
487 Device* device = getDevice(deviceId);
488
489 RawAbsoluteAxisInfo info;
490 info.valid = true;
491 info.minValue = minValue;
492 info.maxValue = maxValue;
493 info.flat = flat;
494 info.fuzz = fuzz;
495 info.resolution = resolution;
496 device->absoluteAxes.add(axis, info);
497 }
498
499 void addRelativeAxis(int32_t deviceId, int32_t axis) {
500 Device* device = getDevice(deviceId);
501 device->relativeAxes.add(axis, true);
502 }
503
504 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
505 Device* device = getDevice(deviceId);
506 device->keyCodeStates.replaceValueFor(keyCode, state);
507 }
508
509 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
510 Device* device = getDevice(deviceId);
511 device->scanCodeStates.replaceValueFor(scanCode, state);
512 }
513
514 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
515 Device* device = getDevice(deviceId);
516 device->switchStates.replaceValueFor(switchCode, state);
517 }
518
519 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
520 Device* device = getDevice(deviceId);
521 device->absoluteAxisValue.replaceValueFor(axis, value);
522 }
523
524 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
525 int32_t keyCode, uint32_t flags) {
526 Device* device = getDevice(deviceId);
527 KeyInfo info;
528 info.keyCode = keyCode;
529 info.flags = flags;
530 if (scanCode) {
531 device->keysByScanCode.add(scanCode, info);
532 }
533 if (usageCode) {
534 device->keysByUsageCode.add(usageCode, info);
535 }
536 }
537
538 void addLed(int32_t deviceId, int32_t led, bool initialState) {
539 Device* device = getDevice(deviceId);
540 device->leds.add(led, initialState);
541 }
542
543 bool getLedState(int32_t deviceId, int32_t led) {
544 Device* device = getDevice(deviceId);
545 return device->leds.valueFor(led);
546 }
547
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100548 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549 return mExcludedDevices;
550 }
551
552 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
553 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800554 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555 }
556
557 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
558 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700559 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560 RawEvent event;
561 event.when = when;
562 event.deviceId = deviceId;
563 event.type = type;
564 event.code = code;
565 event.value = value;
566 mEvents.push_back(event);
567
568 if (type == EV_ABS) {
569 setAbsoluteAxisValue(deviceId, code, value);
570 }
571 }
572
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600573 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
574 std::vector<TouchVideoFrame>> videoFrames) {
575 mVideoFrames = std::move(videoFrames);
576 }
577
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700579 std::unique_lock<std::mutex> lock(mLock);
580 base::ScopedLockAssertion assumeLocked(mLock);
581 const bool queueIsEmpty =
582 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
583 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
584 if (!queueIsEmpty) {
585 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
586 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587 }
588
589private:
590 Device* getDevice(int32_t deviceId) const {
591 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100592 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 }
594
595 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
596 Device* device = getDevice(deviceId);
597 return device ? device->classes : 0;
598 }
599
600 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
601 Device* device = getDevice(deviceId);
602 return device ? device->identifier : InputDeviceIdentifier();
603 }
604
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100605 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 return 0;
607 }
608
609 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
610 Device* device = getDevice(deviceId);
611 if (device) {
612 *outConfiguration = device->configuration;
613 }
614 }
615
616 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
617 RawAbsoluteAxisInfo* outAxisInfo) const {
618 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800619 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620 ssize_t index = device->absoluteAxes.indexOfKey(axis);
621 if (index >= 0) {
622 *outAxisInfo = device->absoluteAxes.valueAt(index);
623 return OK;
624 }
625 }
626 outAxisInfo->clear();
627 return -1;
628 }
629
630 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
631 Device* device = getDevice(deviceId);
632 if (device) {
633 return device->relativeAxes.indexOfKey(axis) >= 0;
634 }
635 return false;
636 }
637
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100638 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800639 return false;
640 }
641
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700642 virtual status_t mapKey(int32_t deviceId,
643 int32_t scanCode, int32_t usageCode, int32_t metaState,
644 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 Device* device = getDevice(deviceId);
646 if (device) {
647 const KeyInfo* key = getKey(device, scanCode, usageCode);
648 if (key) {
649 if (outKeycode) {
650 *outKeycode = key->keyCode;
651 }
652 if (outFlags) {
653 *outFlags = key->flags;
654 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700655 if (outMetaState) {
656 *outMetaState = metaState;
657 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 return OK;
659 }
660 }
661 return NAME_NOT_FOUND;
662 }
663
664 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
665 if (usageCode) {
666 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
667 if (index >= 0) {
668 return &device->keysByUsageCode.valueAt(index);
669 }
670 }
671 if (scanCode) {
672 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
673 if (index >= 0) {
674 return &device->keysByScanCode.valueAt(index);
675 }
676 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700677 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678 }
679
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100680 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681 return NAME_NOT_FOUND;
682 }
683
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100684 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 mExcludedDevices = devices;
686 }
687
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100688 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700689 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800690 if (mEvents.empty()) {
691 return 0;
692 }
693
694 *buffer = *mEvents.begin();
695 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700696 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 return 1;
698 }
699
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800700 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600701 auto it = mVideoFrames.find(deviceId);
702 if (it != mVideoFrames.end()) {
703 std::vector<TouchVideoFrame> frames = std::move(it->second);
704 mVideoFrames.erase(deviceId);
705 return frames;
706 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800707 return {};
708 }
709
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
711 Device* device = getDevice(deviceId);
712 if (device) {
713 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
714 if (index >= 0) {
715 return device->scanCodeStates.valueAt(index);
716 }
717 }
718 return AKEY_STATE_UNKNOWN;
719 }
720
721 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
722 Device* device = getDevice(deviceId);
723 if (device) {
724 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
725 if (index >= 0) {
726 return device->keyCodeStates.valueAt(index);
727 }
728 }
729 return AKEY_STATE_UNKNOWN;
730 }
731
732 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
733 Device* device = getDevice(deviceId);
734 if (device) {
735 ssize_t index = device->switchStates.indexOfKey(sw);
736 if (index >= 0) {
737 return device->switchStates.valueAt(index);
738 }
739 }
740 return AKEY_STATE_UNKNOWN;
741 }
742
743 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
744 int32_t* outValue) const {
745 Device* device = getDevice(deviceId);
746 if (device) {
747 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
748 if (index >= 0) {
749 *outValue = device->absoluteAxisValue.valueAt(index);
750 return OK;
751 }
752 }
753 *outValue = 0;
754 return -1;
755 }
756
757 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
758 uint8_t* outFlags) const {
759 bool result = false;
760 Device* device = getDevice(deviceId);
761 if (device) {
762 for (size_t i = 0; i < numCodes; i++) {
763 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
764 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
765 outFlags[i] = 1;
766 result = true;
767 }
768 }
769 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
770 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
771 outFlags[i] = 1;
772 result = true;
773 }
774 }
775 }
776 }
777 return result;
778 }
779
780 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
781 Device* device = getDevice(deviceId);
782 if (device) {
783 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
784 return index >= 0;
785 }
786 return false;
787 }
788
789 virtual bool hasLed(int32_t deviceId, int32_t led) const {
790 Device* device = getDevice(deviceId);
791 return device && device->leds.indexOfKey(led) >= 0;
792 }
793
794 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
795 Device* device = getDevice(deviceId);
796 if (device) {
797 ssize_t index = device->leds.indexOfKey(led);
798 if (index >= 0) {
799 device->leds.replaceValueAt(led, on);
800 } else {
801 ADD_FAILURE()
802 << "Attempted to set the state of an LED that the EventHub declared "
803 "was not present. led=" << led;
804 }
805 }
806 }
807
808 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800809 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 outVirtualKeys.clear();
811
812 Device* device = getDevice(deviceId);
813 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800814 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800815 }
816 }
817
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100818 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700819 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 }
821
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100822 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 return false;
824 }
825
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000826 virtual void vibrate(int32_t, const VibrationElement&) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100828 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 }
830
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100831 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800832 return false;
833 }
834
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800835 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 }
837
838 virtual void monitor() {
839 }
840
841 virtual void requestReopenDevices() {
842 }
843
844 virtual void wake() {
845 }
846};
847
848
849// --- FakeInputReaderContext ---
850
851class FakeInputReaderContext : public InputReaderContext {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700852 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853 sp<InputReaderPolicyInterface> mPolicy;
854 sp<InputListenerInterface> mListener;
855 int32_t mGlobalMetaState;
856 bool mUpdateGlobalMetaStateWasCalled;
857 int32_t mGeneration;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800858 int32_t mNextId;
Michael Wright17db18e2020-06-26 20:51:44 +0100859 std::weak_ptr<PointerControllerInterface> mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
861public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700862 FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
863 const sp<InputReaderPolicyInterface>& policy,
864 const sp<InputListenerInterface>& listener)
865 : mEventHub(eventHub),
866 mPolicy(policy),
867 mListener(listener),
868 mGlobalMetaState(0),
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800869 mNextId(1) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870
871 virtual ~FakeInputReaderContext() { }
872
873 void assertUpdateGlobalMetaStateWasCalled() {
874 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
875 << "Expected updateGlobalMetaState() to have been called.";
876 mUpdateGlobalMetaStateWasCalled = false;
877 }
878
879 void setGlobalMetaState(int32_t state) {
880 mGlobalMetaState = state;
881 }
882
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800883 uint32_t getGeneration() {
884 return mGeneration;
885 }
886
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800887 void updatePointerDisplay() {
Michael Wright17db18e2020-06-26 20:51:44 +0100888 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800889 if (controller != nullptr) {
890 InputReaderConfiguration config;
891 mPolicy->getReaderConfiguration(&config);
892 auto viewport = config.getDisplayViewportById(config.defaultPointerDisplayId);
893 if (viewport) {
894 controller->setDisplayViewport(*viewport);
895 }
896 }
897 }
898
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899private:
900 virtual void updateGlobalMetaState() {
901 mUpdateGlobalMetaStateWasCalled = true;
902 }
903
904 virtual int32_t getGlobalMetaState() {
905 return mGlobalMetaState;
906 }
907
908 virtual EventHubInterface* getEventHub() {
909 return mEventHub.get();
910 }
911
912 virtual InputReaderPolicyInterface* getPolicy() {
913 return mPolicy.get();
914 }
915
916 virtual InputListenerInterface* getListener() {
917 return mListener.get();
918 }
919
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100920 virtual void disableVirtualKeysUntil(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 }
922
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800923 virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924
Michael Wright17db18e2020-06-26 20:51:44 +0100925 virtual std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) {
926 std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800927 if (controller == nullptr) {
928 controller = mPolicy->obtainPointerController(deviceId);
929 mPointerController = controller;
930 updatePointerDisplay();
931 }
932 return controller;
933 }
934
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 virtual void fadePointer() {
936 }
937
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100938 virtual void requestTimeoutAtTime(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 }
940
941 virtual int32_t bumpGeneration() {
942 return ++mGeneration;
943 }
Michael Wright842500e2015-03-13 17:32:02 -0700944
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800945 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
Michael Wright842500e2015-03-13 17:32:02 -0700946
947 }
948
949 virtual void dispatchExternalStylusState(const StylusState&) {
950
951 }
Prabir Pradhan42611e02018-11-27 14:04:02 -0800952
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800953 virtual int32_t getNextId() { return mNextId++; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954};
955
956
957// --- FakeInputMapper ---
958
959class FakeInputMapper : public InputMapper {
960 uint32_t mSources;
961 int32_t mKeyboardType;
962 int32_t mMetaState;
963 KeyedVector<int32_t, int32_t> mKeyCodeStates;
964 KeyedVector<int32_t, int32_t> mScanCodeStates;
965 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800966 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700968 std::mutex mLock;
969 std::condition_variable mStateChangedCondition;
970 bool mConfigureWasCalled GUARDED_BY(mLock);
971 bool mResetWasCalled GUARDED_BY(mLock);
972 bool mProcessWasCalled GUARDED_BY(mLock);
973 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974
Arthur Hungc23540e2018-11-29 20:42:11 +0800975 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800977 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
978 : InputMapper(deviceContext),
979 mSources(sources),
980 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800982 mConfigureWasCalled(false),
983 mResetWasCalled(false),
984 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985
986 virtual ~FakeInputMapper() { }
987
988 void setKeyboardType(int32_t keyboardType) {
989 mKeyboardType = keyboardType;
990 }
991
992 void setMetaState(int32_t metaState) {
993 mMetaState = metaState;
994 }
995
996 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700997 std::unique_lock<std::mutex> lock(mLock);
998 base::ScopedLockAssertion assumeLocked(mLock);
999 const bool configureCalled =
1000 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1001 return mConfigureWasCalled;
1002 });
1003 if (!configureCalled) {
1004 FAIL() << "Expected configure() to have been called.";
1005 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001006 mConfigureWasCalled = false;
1007 }
1008
1009 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001010 std::unique_lock<std::mutex> lock(mLock);
1011 base::ScopedLockAssertion assumeLocked(mLock);
1012 const bool resetCalled =
1013 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1014 return mResetWasCalled;
1015 });
1016 if (!resetCalled) {
1017 FAIL() << "Expected reset() to have been called.";
1018 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019 mResetWasCalled = false;
1020 }
1021
Yi Kong9b14ac62018-07-17 13:48:38 -07001022 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001023 std::unique_lock<std::mutex> lock(mLock);
1024 base::ScopedLockAssertion assumeLocked(mLock);
1025 const bool processCalled =
1026 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1027 return mProcessWasCalled;
1028 });
1029 if (!processCalled) {
1030 FAIL() << "Expected process() to have been called.";
1031 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032 if (outLastEvent) {
1033 *outLastEvent = mLastEvent;
1034 }
1035 mProcessWasCalled = false;
1036 }
1037
1038 void setKeyCodeState(int32_t keyCode, int32_t state) {
1039 mKeyCodeStates.replaceValueFor(keyCode, state);
1040 }
1041
1042 void setScanCodeState(int32_t scanCode, int32_t state) {
1043 mScanCodeStates.replaceValueFor(scanCode, state);
1044 }
1045
1046 void setSwitchState(int32_t switchCode, int32_t state) {
1047 mSwitchStates.replaceValueFor(switchCode, state);
1048 }
1049
1050 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001051 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 }
1053
1054private:
1055 virtual uint32_t getSources() {
1056 return mSources;
1057 }
1058
1059 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1060 InputMapper::populateDeviceInfo(deviceInfo);
1061
1062 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1063 deviceInfo->setKeyboardType(mKeyboardType);
1064 }
1065 }
1066
Arthur Hungc23540e2018-11-29 20:42:11 +08001067 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001068 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001070
1071 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001072 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001073 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1074 mViewport = config->getDisplayViewportByPort(*displayPort);
1075 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001076
1077 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078 }
1079
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001080 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001081 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001083 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084 }
1085
1086 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001087 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001088 mLastEvent = *rawEvent;
1089 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001090 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091 }
1092
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001093 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001094 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1095 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1096 }
1097
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001098 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1100 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1101 }
1102
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001103 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1105 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1106 }
1107
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001108 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109 const int32_t* keyCodes, uint8_t* outFlags) {
1110 bool result = false;
1111 for (size_t i = 0; i < numCodes; i++) {
1112 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1113 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1114 outFlags[i] = 1;
1115 result = true;
1116 }
1117 }
1118 }
1119 return result;
1120 }
1121
1122 virtual int32_t getMetaState() {
1123 return mMetaState;
1124 }
1125
1126 virtual void fadePointer() {
1127 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001128
1129 virtual std::optional<int32_t> getAssociatedDisplay() {
1130 if (mViewport) {
1131 return std::make_optional(mViewport->displayId);
1132 }
1133 return std::nullopt;
1134 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135};
1136
1137
1138// --- InstrumentedInputReader ---
1139
1140class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001141 std::shared_ptr<InputDevice> mNextDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001142
1143public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001144 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1145 const sp<InputReaderPolicyInterface>& policy,
1146 const sp<InputListenerInterface>& listener)
1147 : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001149 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001151 void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001152
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001153 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001154 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001155 InputDeviceIdentifier identifier;
1156 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001157 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158 int32_t generation = deviceId + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001159 return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160 }
1161
Prabir Pradhan28efc192019-11-05 01:10:04 +00001162 // Make the protected loopOnce method accessible to tests.
1163 using InputReader::loopOnce;
1164
Michael Wrightd02c5b62014-02-10 15:10:22 -08001165protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001166 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1167 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 if (mNextDevice) {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001169 std::shared_ptr<InputDevice> device(mNextDevice);
Yi Kong9b14ac62018-07-17 13:48:38 -07001170 mNextDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 return device;
1172 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001173 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174 }
1175
1176 friend class InputReaderTest;
1177};
1178
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001179// --- InputReaderPolicyTest ---
1180class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001181protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001182 sp<FakeInputReaderPolicy> mFakePolicy;
1183
Prabir Pradhan28efc192019-11-05 01:10:04 +00001184 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1185 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001186};
1187
1188/**
1189 * Check that empty set of viewports is an acceptable configuration.
1190 * Also try to get internal viewport two different ways - by type and by uniqueId.
1191 *
1192 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1193 * Such configuration is not currently allowed.
1194 */
1195TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001196 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001197
1198 // We didn't add any viewports yet, so there shouldn't be any.
1199 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001200 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001201 ASSERT_FALSE(internalViewport);
1202
1203 // Add an internal viewport, then clear it
1204 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001205 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT,
1206 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001207
1208 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001209 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001211 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001212
1213 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001214 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001215 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001216 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001217
1218 mFakePolicy->clearViewports();
1219 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001220 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001221 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001222 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001223 ASSERT_FALSE(internalViewport);
1224}
1225
1226TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1227 const std::string internalUniqueId = "local:0";
1228 const std::string externalUniqueId = "local:1";
1229 const std::string virtualUniqueId1 = "virtual:2";
1230 const std::string virtualUniqueId2 = "virtual:3";
1231 constexpr int32_t virtualDisplayId1 = 2;
1232 constexpr int32_t virtualDisplayId2 = 3;
1233
1234 // Add an internal viewport
1235 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001236 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT,
1237 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001238 // Add an external viewport
1239 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001240 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT,
1241 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001242 // Add an virtual viewport
1243 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001244 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT,
1245 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001246 // Add another virtual viewport
1247 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001248 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT,
1249 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001250
1251 // Check matching by type for internal
1252 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001253 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001254 ASSERT_TRUE(internalViewport);
1255 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1256
1257 // Check matching by type for external
1258 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001259 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001260 ASSERT_TRUE(externalViewport);
1261 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1262
1263 // Check matching by uniqueId for virtual viewport #1
1264 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001265 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001266 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001267 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001268 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1269 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1270
1271 // Check matching by uniqueId for virtual viewport #2
1272 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001273 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001274 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001275 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001276 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1277 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1278}
1279
1280
1281/**
1282 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1283 * that lookup works by checking display id.
1284 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1285 */
1286TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1287 const std::string uniqueId1 = "uniqueId1";
1288 const std::string uniqueId2 = "uniqueId2";
1289 constexpr int32_t displayId1 = 2;
1290 constexpr int32_t displayId2 = 3;
1291
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001292 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1293 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001294 for (const ViewportType& type : types) {
1295 mFakePolicy->clearViewports();
1296 // Add a viewport
1297 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001298 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001299 // Add another viewport
1300 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001301 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001302
1303 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001304 std::optional<DisplayViewport> viewport1 =
1305 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001306 ASSERT_TRUE(viewport1);
1307 ASSERT_EQ(displayId1, viewport1->displayId);
1308 ASSERT_EQ(type, viewport1->type);
1309
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001310 std::optional<DisplayViewport> viewport2 =
1311 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001312 ASSERT_TRUE(viewport2);
1313 ASSERT_EQ(displayId2, viewport2->displayId);
1314 ASSERT_EQ(type, viewport2->type);
1315
1316 // When there are multiple viewports of the same kind, and uniqueId is not specified
1317 // in the call to getDisplayViewport, then that situation is not supported.
1318 // The viewports can be stored in any order, so we cannot rely on the order, since that
1319 // is just implementation detail.
1320 // However, we can check that it still returns *a* viewport, we just cannot assert
1321 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001322 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001323 ASSERT_TRUE(someViewport);
1324 }
1325}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001326
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001327/**
1328 * Check getDisplayViewportByPort
1329 */
1330TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001331 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001332 const std::string uniqueId1 = "uniqueId1";
1333 const std::string uniqueId2 = "uniqueId2";
1334 constexpr int32_t displayId1 = 1;
1335 constexpr int32_t displayId2 = 2;
1336 const uint8_t hdmi1 = 0;
1337 const uint8_t hdmi2 = 1;
1338 const uint8_t hdmi3 = 2;
1339
1340 mFakePolicy->clearViewports();
1341 // Add a viewport that's associated with some display port that's not of interest.
1342 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1343 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1344 // Add another viewport, connected to HDMI1 port
1345 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1346 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1347
1348 // Check that correct display viewport was returned by comparing the display ports.
1349 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1350 ASSERT_TRUE(hdmi1Viewport);
1351 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1352 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1353
1354 // Check that we can still get the same viewport using the uniqueId
1355 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1356 ASSERT_TRUE(hdmi1Viewport);
1357 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1358 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1359 ASSERT_EQ(type, hdmi1Viewport->type);
1360
1361 // Check that we cannot find a port with "HDMI2", because we never added one
1362 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1363 ASSERT_FALSE(hdmi2Viewport);
1364}
1365
Michael Wrightd02c5b62014-02-10 15:10:22 -08001366// --- InputReaderTest ---
1367
1368class InputReaderTest : public testing::Test {
1369protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001370 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001371 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001372 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001373 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001374
Prabir Pradhan28efc192019-11-05 01:10:04 +00001375 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001376 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001377 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001378 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001379
Prabir Pradhan28efc192019-11-05 01:10:04 +00001380 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1381 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001382 }
1383
Prabir Pradhan28efc192019-11-05 01:10:04 +00001384 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001385 mFakeListener.clear();
1386 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001387 }
1388
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001389 void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1390 const PropertyMap* configuration) {
1391 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001392
1393 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001394 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001395 }
1396 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001397 mReader->loopOnce();
1398 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001399 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1400 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401 }
1402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001403 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001404 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001405 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001406 }
1407
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001408 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001409 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001410 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001411 }
1412
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001413 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001414 const std::string& name, uint32_t classes,
1415 uint32_t sources,
1416 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001417 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1418 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001419 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001420 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001421 return mapper;
1422 }
1423};
1424
1425TEST_F(InputReaderTest, GetInputDevices) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001426 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
Yi Kong9b14ac62018-07-17 13:48:38 -07001427 INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001428 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
Yi Kong9b14ac62018-07-17 13:48:38 -07001429 0, nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001430
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001431 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001432 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001433 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001434 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001435 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001436 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1437 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1438 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1439
1440 // Should also have received a notification describing the new input devices.
1441 inputDevices = mFakePolicy->getInputDevices();
1442 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001443 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001444 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001445 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1446 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1447 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1448}
1449
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001450TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001451 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001452 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001453 constexpr int32_t eventHubId = 1;
1454 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001455 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001456 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001457 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001458 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001459
Yi Kong9b14ac62018-07-17 13:48:38 -07001460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001461
1462 NotifyDeviceResetArgs resetArgs;
1463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001464 ASSERT_EQ(deviceId, resetArgs.deviceId);
1465
1466 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001467 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001468 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001469
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001471 ASSERT_EQ(deviceId, resetArgs.deviceId);
1472 ASSERT_EQ(device->isEnabled(), false);
1473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001474 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001475 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001478 ASSERT_EQ(device->isEnabled(), false);
1479
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001480 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001481 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001483 ASSERT_EQ(deviceId, resetArgs.deviceId);
1484 ASSERT_EQ(device->isEnabled(), true);
1485}
1486
Michael Wrightd02c5b62014-02-10 15:10:22 -08001487TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001488 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1489 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1490 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001491 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001492 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001493 AINPUT_SOURCE_KEYBOARD, nullptr);
1494 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001495
1496 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1497 AINPUT_SOURCE_ANY, AKEYCODE_A))
1498 << "Should return unknown when the device id is >= 0 but unknown.";
1499
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001500 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1501 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1502 << "Should return unknown when the device id is valid but the sources are not "
1503 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001504
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001505 ASSERT_EQ(AKEY_STATE_DOWN,
1506 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1507 AKEYCODE_A))
1508 << "Should return value provided by mapper when device id is valid and the device "
1509 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001510
1511 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1512 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1513 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1514
1515 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1516 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1517 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1518}
1519
1520TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001521 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1522 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1523 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001524 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001525 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001526 AINPUT_SOURCE_KEYBOARD, nullptr);
1527 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001528
1529 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1530 AINPUT_SOURCE_ANY, KEY_A))
1531 << "Should return unknown when the device id is >= 0 but unknown.";
1532
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001533 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1534 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1535 << "Should return unknown when the device id is valid but the sources are not "
1536 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001537
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001538 ASSERT_EQ(AKEY_STATE_DOWN,
1539 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1540 KEY_A))
1541 << "Should return value provided by mapper when device id is valid and the device "
1542 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001543
1544 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1545 AINPUT_SOURCE_TRACKBALL, KEY_A))
1546 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1547
1548 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1549 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1550 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1551}
1552
1553TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001554 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1555 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1556 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001557 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001558 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001559 AINPUT_SOURCE_KEYBOARD, nullptr);
1560 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
1562 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1563 AINPUT_SOURCE_ANY, SW_LID))
1564 << "Should return unknown when the device id is >= 0 but unknown.";
1565
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001566 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1567 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1568 << "Should return unknown when the device id is valid but the sources are not "
1569 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001571 ASSERT_EQ(AKEY_STATE_DOWN,
1572 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1573 SW_LID))
1574 << "Should return value provided by mapper when device id is valid and the device "
1575 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001576
1577 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1578 AINPUT_SOURCE_TRACKBALL, SW_LID))
1579 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1580
1581 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1582 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1583 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1584}
1585
1586TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1588 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1589 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001590 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001591 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001592 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001593
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001594 mapper.addSupportedKeyCode(AKEYCODE_A);
1595 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596
1597 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1598 uint8_t flags[4] = { 0, 0, 0, 1 };
1599
1600 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1601 << "Should return false when device id is >= 0 but unknown.";
1602 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1603
1604 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1606 << "Should return false when device id is valid but the sources are not supported by "
1607 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001608 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1609
1610 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001611 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1612 keyCodes, flags))
1613 << "Should return value provided by mapper when device id is valid and the device "
1614 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001615 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1616
1617 flags[3] = 1;
1618 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1619 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1620 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1621
1622 flags[3] = 1;
1623 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1624 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1625 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1626}
1627
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001628TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001629 constexpr int32_t eventHubId = 1;
1630 addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631
1632 NotifyConfigurationChangedArgs args;
1633
1634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1635 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1636}
1637
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001638TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1640 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1641 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001642 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001643 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001644 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001646 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001647 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1649
1650 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001651 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654 ASSERT_EQ(EV_KEY, event.type);
1655 ASSERT_EQ(KEY_A, event.code);
1656 ASSERT_EQ(1, event.value);
1657}
1658
Garfield Tan1c7bc862020-01-28 13:24:04 -08001659TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001660 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001661 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001662 constexpr int32_t eventHubId = 1;
1663 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001664 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001665 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhan42611e02018-11-27 14:04:02 -08001666 mReader->setNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001667 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001668
1669 NotifyDeviceResetArgs resetArgs;
1670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001671 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001673 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001674 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001676 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001677 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001678
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001679 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001680 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001682 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001683 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001684
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001685 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001686 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001688 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001689 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001690}
1691
Garfield Tan1c7bc862020-01-28 13:24:04 -08001692TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1693 constexpr int32_t deviceId = 1;
1694 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1695 constexpr int32_t eventHubId = 1;
1696 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1697 // Must add at least one mapper or the device will be ignored!
1698 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1699 mReader->setNextDevice(device);
1700 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1701
1702 NotifyDeviceResetArgs resetArgs;
1703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1704 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1705}
1706
Arthur Hungc23540e2018-11-29 20:42:11 +08001707TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Arthur Hungc23540e2018-11-29 20:42:11 +08001709 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001711 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1713 FakeInputMapper& mapper =
1714 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hungc23540e2018-11-29 20:42:11 +08001715 mReader->setNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001716
1717 const uint8_t hdmi1 = 1;
1718
1719 // Associated touch screen with second display.
1720 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1721
1722 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001723 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001724 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001725 DISPLAY_ORIENTATION_0, "local:0", NO_PORT,
1726 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001727 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001728 DISPLAY_ORIENTATION_0, "local:1", hdmi1,
1729 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001730 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001731 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001732
1733 // Add the device, and make sure all of the callbacks are triggered.
1734 // The device is added after the input port associations are processed since
1735 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001736 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001739 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001740
Arthur Hung2c9a3342019-07-23 14:18:59 +08001741 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001742 ASSERT_EQ(deviceId, device->getId());
1743 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1744 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001745
1746 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001747 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001748 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001749 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001750}
1751
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001752// --- InputReaderIntegrationTest ---
1753
1754// These tests create and interact with the InputReader only through its interface.
1755// The InputReader is started during SetUp(), which starts its processing in its own
1756// thread. The tests use linux uinput to emulate input devices.
1757// NOTE: Interacting with the physical device while these tests are running may cause
1758// the tests to fail.
1759class InputReaderIntegrationTest : public testing::Test {
1760protected:
1761 sp<TestInputListener> mTestListener;
1762 sp<FakeInputReaderPolicy> mFakePolicy;
1763 sp<InputReaderInterface> mReader;
1764
1765 virtual void SetUp() override {
1766 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001767 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1768 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001769
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001770 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001771 ASSERT_EQ(mReader->start(), OK);
1772
1773 // Since this test is run on a real device, all the input devices connected
1774 // to the test device will show up in mReader. We wait for those input devices to
1775 // show up before beginning the tests.
1776 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1777 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1778 }
1779
1780 virtual void TearDown() override {
1781 ASSERT_EQ(mReader->stop(), OK);
1782 mTestListener.clear();
1783 mFakePolicy.clear();
1784 }
1785};
1786
1787TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1788 // An invalid input device that is only used for this test.
1789 class InvalidUinputDevice : public UinputDevice {
1790 public:
1791 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1792
1793 private:
1794 void configureDevice(int fd, uinput_user_dev* device) override {}
1795 };
1796
1797 const size_t numDevices = mFakePolicy->getInputDevices().size();
1798
1799 // UinputDevice does not set any event or key bits, so InputReader should not
1800 // consider it as a valid device.
1801 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1802 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1803 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1804 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1805
1806 invalidDevice.reset();
1807 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1808 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1809 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1810}
1811
1812TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1813 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1814
1815 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1816 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1817 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1818 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1819
1820 // Find the test device by its name.
1821 std::vector<InputDeviceInfo> inputDevices;
1822 mReader->getInputDevices(inputDevices);
1823 InputDeviceInfo* keyboardInfo = nullptr;
1824 const char* keyboardName = keyboard->getName();
1825 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1826 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1827 keyboardInfo = &inputDevices[i];
1828 break;
1829 }
1830 }
1831 ASSERT_NE(keyboardInfo, nullptr);
1832 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1833 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1834 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1835
1836 keyboard.reset();
1837 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1838 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1839 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1840}
1841
1842TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1843 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1844 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1845
1846 NotifyConfigurationChangedArgs configChangedArgs;
1847 ASSERT_NO_FATAL_FAILURE(
1848 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001849 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001850 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1851
1852 NotifyKeyArgs keyArgs;
1853 keyboard->pressAndReleaseHomeKey();
1854 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1855 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001856 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001857 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001858 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1859 prevTimestamp = keyArgs.eventTime;
1860
1861 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1862 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001863 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001864 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1865}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001866
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001867/**
1868 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1869 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1870 * are passed to the listener.
1871 */
1872static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1873TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1874 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1875 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1876 NotifyKeyArgs keyArgs;
1877
1878 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1879 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1880 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1881 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1882
1883 controller->pressAndReleaseKey(BTN_GEAR_UP);
1884 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1885 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1886 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1887}
1888
Arthur Hungaab25622020-01-16 11:22:11 +08001889// --- TouchProcessTest ---
1890class TouchIntegrationTest : public InputReaderIntegrationTest {
1891protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001892 const std::string UNIQUE_ID = "local:0";
1893
1894 virtual void SetUp() override {
1895 InputReaderIntegrationTest::SetUp();
1896 // At least add an internal display.
1897 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1898 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001899 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001900
1901 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1902 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1903 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1904 }
1905
1906 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1907 int32_t orientation, const std::string& uniqueId,
1908 std::optional<uint8_t> physicalPort,
1909 ViewportType viewportType) {
1910 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1911 physicalPort, viewportType);
1912 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1913 }
1914
1915 std::unique_ptr<UinputTouchScreen> mDevice;
1916};
1917
1918TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1919 NotifyMotionArgs args;
1920 const Point centerPoint = mDevice->getCenterPoint();
1921
1922 // ACTION_DOWN
1923 mDevice->sendDown(centerPoint);
1924 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1925 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1926
1927 // ACTION_MOVE
1928 mDevice->sendMove(centerPoint + Point(1, 1));
1929 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1931
1932 // ACTION_UP
1933 mDevice->sendUp();
1934 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1935 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1936}
1937
1938TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1939 NotifyMotionArgs args;
1940 const Point centerPoint = mDevice->getCenterPoint();
1941
1942 // ACTION_DOWN
1943 mDevice->sendDown(centerPoint);
1944 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1945 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1946
1947 // ACTION_POINTER_DOWN (Second slot)
1948 const Point secondPoint = centerPoint + Point(100, 100);
1949 mDevice->sendSlot(SECOND_SLOT);
1950 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1951 mDevice->sendDown(secondPoint + Point(1, 1));
1952 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1953 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1954 args.action);
1955
1956 // ACTION_MOVE (Second slot)
1957 mDevice->sendMove(secondPoint);
1958 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1960
1961 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001962 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08001963 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08001964 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08001965 args.action);
1966
1967 // ACTION_UP
1968 mDevice->sendSlot(FIRST_SLOT);
1969 mDevice->sendUp();
1970 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1971 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1972}
1973
1974TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1975 NotifyMotionArgs args;
1976 const Point centerPoint = mDevice->getCenterPoint();
1977
1978 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001979 mDevice->sendSlot(FIRST_SLOT);
1980 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001981 mDevice->sendDown(centerPoint);
1982 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1983 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1984
arthurhungcc7f9802020-04-30 17:55:40 +08001985 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001986 const Point secondPoint = centerPoint + Point(100, 100);
1987 mDevice->sendSlot(SECOND_SLOT);
1988 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1989 mDevice->sendDown(secondPoint);
1990 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1991 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1992 args.action);
1993
arthurhungcc7f9802020-04-30 17:55:40 +08001994 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001995 mDevice->sendMove(secondPoint + Point(1, 1));
1996 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1998
arthurhungcc7f9802020-04-30 17:55:40 +08001999 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2000 // a palm event.
2001 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002002 mDevice->sendToolType(MT_TOOL_PALM);
2003 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002004 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2005 args.action);
2006 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002007
arthurhungcc7f9802020-04-30 17:55:40 +08002008 // Send up to second slot, expect first slot send moving.
2009 mDevice->sendPointerUp();
2010 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002012
arthurhungcc7f9802020-04-30 17:55:40 +08002013 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002014 mDevice->sendSlot(FIRST_SLOT);
2015 mDevice->sendUp();
2016
arthurhungcc7f9802020-04-30 17:55:40 +08002017 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2018 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002019}
2020
Michael Wrightd02c5b62014-02-10 15:10:22 -08002021// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002022class InputDeviceTest : public testing::Test {
2023protected:
2024 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002025 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002026 static const int32_t DEVICE_ID;
2027 static const int32_t DEVICE_GENERATION;
2028 static const int32_t DEVICE_CONTROLLER_NUMBER;
2029 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002030 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002031
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002032 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002033 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002034 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002035 FakeInputReaderContext* mFakeContext;
2036
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002037 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002038
Prabir Pradhan28efc192019-11-05 01:10:04 +00002039 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002040 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002041 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002042 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002043 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2044
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002045 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002046 InputDeviceIdentifier identifier;
2047 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002048 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002049 mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2050 identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002051 }
2052
Prabir Pradhan28efc192019-11-05 01:10:04 +00002053 virtual void TearDown() override {
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002054 mDevice = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002055 delete mFakeContext;
2056 mFakeListener.clear();
2057 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002058 }
2059};
2060
2061const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002062const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002063const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002064const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2065const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
2066const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
2067 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002068const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002069
2070TEST_F(InputDeviceTest, ImmutableProperties) {
2071 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002072 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002073 ASSERT_EQ(0U, mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002074}
2075
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002076TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2077 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002078}
2079
Michael Wrightd02c5b62014-02-10 15:10:22 -08002080TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2081 // Configuration.
2082 InputReaderConfiguration config;
2083 mDevice->configure(ARBITRARY_TIME, &config, 0);
2084
2085 // Reset.
2086 mDevice->reset(ARBITRARY_TIME);
2087
2088 NotifyDeviceResetArgs resetArgs;
2089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2090 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2091 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2092
2093 // Metadata.
2094 ASSERT_TRUE(mDevice->isIgnored());
2095 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2096
2097 InputDeviceInfo info;
2098 mDevice->getDeviceInfo(&info);
2099 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002100 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002101 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2102 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2103
2104 // State queries.
2105 ASSERT_EQ(0, mDevice->getMetaState());
2106
2107 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2108 << "Ignored device should return unknown key code state.";
2109 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2110 << "Ignored device should return unknown scan code state.";
2111 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2112 << "Ignored device should return unknown switch state.";
2113
2114 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2115 uint8_t flags[2] = { 0, 1 };
2116 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2117 << "Ignored device should never mark any key codes.";
2118 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2119 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2120}
2121
2122TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2123 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002124 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002125
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002126 FakeInputMapper& mapper1 =
2127 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002128 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2129 mapper1.setMetaState(AMETA_ALT_ON);
2130 mapper1.addSupportedKeyCode(AKEYCODE_A);
2131 mapper1.addSupportedKeyCode(AKEYCODE_B);
2132 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2133 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2134 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2135 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2136 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002137
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002138 FakeInputMapper& mapper2 =
2139 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002140 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002141
2142 InputReaderConfiguration config;
2143 mDevice->configure(ARBITRARY_TIME, &config, 0);
2144
2145 String8 propertyValue;
2146 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2147 << "Device should have read configuration during configuration phase.";
2148 ASSERT_STREQ("value", propertyValue.string());
2149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002150 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2151 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002152
2153 // Reset
2154 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002155 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2156 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002157
2158 NotifyDeviceResetArgs resetArgs;
2159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2160 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2161 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2162
2163 // Metadata.
2164 ASSERT_FALSE(mDevice->isIgnored());
2165 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2166
2167 InputDeviceInfo info;
2168 mDevice->getDeviceInfo(&info);
2169 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002170 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002171 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2172 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2173
2174 // State queries.
2175 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2176 << "Should query mappers and combine meta states.";
2177
2178 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2179 << "Should return unknown key code state when source not supported.";
2180 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2181 << "Should return unknown scan code state when source not supported.";
2182 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2183 << "Should return unknown switch state when source not supported.";
2184
2185 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2186 << "Should query mapper when source is supported.";
2187 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2188 << "Should query mapper when source is supported.";
2189 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2190 << "Should query mapper when source is supported.";
2191
2192 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2193 uint8_t flags[4] = { 0, 0, 0, 1 };
2194 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2195 << "Should do nothing when source is unsupported.";
2196 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2197 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2198 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2199 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2200
2201 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2202 << "Should query mapper when source is supported.";
2203 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2204 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2205 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2206 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2207
2208 // Event handling.
2209 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002210 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002211 mDevice->process(&event, 1);
2212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002213 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2214 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002215}
2216
Arthur Hung2c9a3342019-07-23 14:18:59 +08002217// A single input device is associated with a specific display. Check that:
2218// 1. Device is disabled if the viewport corresponding to the associated display is not found
2219// 2. Device is disabled when setEnabled API is called
2220TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002221 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002222
2223 // First Configuration.
2224 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2225
2226 // Device should be enabled by default.
2227 ASSERT_TRUE(mDevice->isEnabled());
2228
2229 // Prepare associated info.
2230 constexpr uint8_t hdmi = 1;
2231 const std::string UNIQUE_ID = "local:1";
2232
2233 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2234 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2235 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2236 // Device should be disabled because it is associated with a specific display via
2237 // input port <-> display port association, but the corresponding display is not found
2238 ASSERT_FALSE(mDevice->isEnabled());
2239
2240 // Prepare displays.
2241 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002242 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002243 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2244 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2245 ASSERT_TRUE(mDevice->isEnabled());
2246
2247 // Device should be disabled after set disable.
2248 mFakePolicy->addDisabledDevice(mDevice->getId());
2249 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2250 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2251 ASSERT_FALSE(mDevice->isEnabled());
2252
2253 // Device should still be disabled even found the associated display.
2254 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2255 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2256 ASSERT_FALSE(mDevice->isEnabled());
2257}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002258
2259// --- InputMapperTest ---
2260
2261class InputMapperTest : public testing::Test {
2262protected:
2263 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002264 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002265 static const int32_t DEVICE_ID;
2266 static const int32_t DEVICE_GENERATION;
2267 static const int32_t DEVICE_CONTROLLER_NUMBER;
2268 static const uint32_t DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002269 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002270
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002271 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002272 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002273 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274 FakeInputReaderContext* mFakeContext;
2275 InputDevice* mDevice;
2276
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002277 virtual void SetUp(uint32_t classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002278 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002280 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002281 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2282 InputDeviceIdentifier identifier;
2283 identifier.name = DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002284 identifier.location = DEVICE_LOCATION;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002285 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002287 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288 }
2289
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002290 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2291
Prabir Pradhan28efc192019-11-05 01:10:04 +00002292 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002293 delete mDevice;
2294 delete mFakeContext;
2295 mFakeListener.clear();
2296 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002297 }
2298
2299 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002300 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002301 }
2302
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002303 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002304 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2305 mFakeContext->updatePointerDisplay();
2306 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002307 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2308 }
2309
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002310 template <class T, typename... Args>
2311 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002312 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002313 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002314 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002315 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002316 }
2317
2318 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002319 int32_t orientation, const std::string& uniqueId,
2320 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002321 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002322 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002323 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2324 }
2325
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002326 void clearViewports() {
2327 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002328 }
2329
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002330 static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2331 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332 RawEvent event;
2333 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002334 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002335 event.type = type;
2336 event.code = code;
2337 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002338 mapper.process(&event);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002339 }
2340
2341 static void assertMotionRange(const InputDeviceInfo& info,
2342 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2343 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002344 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2346 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2347 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2348 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2349 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2350 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2351 }
2352
2353 static void assertPointerCoords(const PointerCoords& coords,
2354 float x, float y, float pressure, float size,
2355 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2356 float orientation, float distance) {
2357 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2358 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2359 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2360 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2361 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2362 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2363 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2364 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2365 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2366 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2367 }
2368
Michael Wright17db18e2020-06-26 20:51:44 +01002369 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002370 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002371 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002372 ASSERT_NEAR(x, actualX, 1);
2373 ASSERT_NEAR(y, actualY, 1);
2374 }
2375};
2376
2377const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002378const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002379const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002380const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2381const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2382const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002383const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384
2385// --- SwitchInputMapperTest ---
2386
2387class SwitchInputMapperTest : public InputMapperTest {
2388protected:
2389};
2390
2391TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002392 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002394 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395}
2396
2397TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002398 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002399
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002400 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002401 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002403 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002404 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405}
2406
2407TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002408 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002410 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2411 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2412 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2413 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414
2415 NotifySwitchArgs args;
2416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2417 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002418 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2419 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420 args.switchMask);
2421 ASSERT_EQ(uint32_t(0), args.policyFlags);
2422}
2423
2424
2425// --- KeyboardInputMapperTest ---
2426
2427class KeyboardInputMapperTest : public InputMapperTest {
2428protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002429 const std::string UNIQUE_ID = "local:0";
2430
2431 void prepareDisplay(int32_t orientation);
2432
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002433 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002434 int32_t originalKeyCode, int32_t rotatedKeyCode,
2435 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002436};
2437
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002438/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2439 * orientation.
2440 */
2441void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002442 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2443 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002444}
2445
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002446void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002447 int32_t originalScanCode, int32_t originalKeyCode,
2448 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449 NotifyKeyArgs args;
2450
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002451 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2453 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2454 ASSERT_EQ(originalScanCode, args.scanCode);
2455 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002456 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002458 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2460 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2461 ASSERT_EQ(originalScanCode, args.scanCode);
2462 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002463 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002464}
2465
Michael Wrightd02c5b62014-02-10 15:10:22 -08002466TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002467 KeyboardInputMapper& mapper =
2468 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2469 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002471 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472}
2473
2474TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2475 const int32_t USAGE_A = 0x070004;
2476 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002477 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2478 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002479
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002480 KeyboardInputMapper& mapper =
2481 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2482 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483
2484 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002485 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002486 NotifyKeyArgs args;
2487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2488 ASSERT_EQ(DEVICE_ID, args.deviceId);
2489 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2490 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2491 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2492 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2493 ASSERT_EQ(KEY_HOME, args.scanCode);
2494 ASSERT_EQ(AMETA_NONE, args.metaState);
2495 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2496 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2497 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2498
2499 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002500 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2502 ASSERT_EQ(DEVICE_ID, args.deviceId);
2503 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2504 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2505 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2506 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2507 ASSERT_EQ(KEY_HOME, args.scanCode);
2508 ASSERT_EQ(AMETA_NONE, args.metaState);
2509 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2510 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2511 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2512
2513 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002514 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2515 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2517 ASSERT_EQ(DEVICE_ID, args.deviceId);
2518 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2519 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2520 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2521 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2522 ASSERT_EQ(0, args.scanCode);
2523 ASSERT_EQ(AMETA_NONE, args.metaState);
2524 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2525 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2526 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2527
2528 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002529 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2530 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2532 ASSERT_EQ(DEVICE_ID, args.deviceId);
2533 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2534 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2535 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2536 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2537 ASSERT_EQ(0, args.scanCode);
2538 ASSERT_EQ(AMETA_NONE, args.metaState);
2539 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2540 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2541 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2542
2543 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002544 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2545 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2547 ASSERT_EQ(DEVICE_ID, args.deviceId);
2548 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2549 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2550 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2551 ASSERT_EQ(0, args.keyCode);
2552 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2553 ASSERT_EQ(AMETA_NONE, args.metaState);
2554 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2555 ASSERT_EQ(0U, args.policyFlags);
2556 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2557
2558 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002559 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2560 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2562 ASSERT_EQ(DEVICE_ID, args.deviceId);
2563 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2564 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2565 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2566 ASSERT_EQ(0, args.keyCode);
2567 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2568 ASSERT_EQ(AMETA_NONE, args.metaState);
2569 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2570 ASSERT_EQ(0U, args.policyFlags);
2571 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2572}
2573
2574TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002578 KeyboardInputMapper& mapper =
2579 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2580 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002581
2582 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002583 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002584
2585 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002586 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587 NotifyKeyArgs args;
2588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002590 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2592
2593 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002594 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2596 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598
2599 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002600 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2602 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002603 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604
2605 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002606 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2608 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002609 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2611}
2612
2613TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2615 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2616 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2617 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002619 KeyboardInputMapper& mapper =
2620 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2621 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002623 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2625 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2626 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2627 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2628 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2629 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2630 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2631 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2632}
2633
2634TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2636 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2637 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2638 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002641 KeyboardInputMapper& mapper =
2642 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2643 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002645 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002646 ASSERT_NO_FATAL_FAILURE(
2647 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2648 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2649 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2650 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2651 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2652 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2653 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002655 clearViewports();
2656 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002657 ASSERT_NO_FATAL_FAILURE(
2658 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2659 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2660 AKEYCODE_DPAD_UP, DISPLAY_ID));
2661 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2662 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2663 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2664 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002666 clearViewports();
2667 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002668 ASSERT_NO_FATAL_FAILURE(
2669 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2670 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2671 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2672 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2673 AKEYCODE_DPAD_UP, DISPLAY_ID));
2674 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2675 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002677 clearViewports();
2678 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002679 ASSERT_NO_FATAL_FAILURE(
2680 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2681 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2682 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2683 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2684 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2685 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2686 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687
2688 // Special case: if orientation changes while key is down, we still emit the same keycode
2689 // in the key up as we did in the key down.
2690 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002691 clearViewports();
2692 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002693 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2695 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2696 ASSERT_EQ(KEY_UP, args.scanCode);
2697 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2698
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002699 clearViewports();
2700 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002701 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2703 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2704 ASSERT_EQ(KEY_UP, args.scanCode);
2705 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2706}
2707
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002708TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2709 // If the keyboard is not orientation aware,
2710 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002711 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002712
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002713 KeyboardInputMapper& mapper =
2714 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2715 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002716 NotifyKeyArgs args;
2717
2718 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002719 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002721 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2723 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2724
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002725 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002726 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002728 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2730 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2731}
2732
2733TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2734 // If the keyboard is orientation aware,
2735 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002736 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002737
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002738 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002739 KeyboardInputMapper& mapper =
2740 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2741 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002742 NotifyKeyArgs args;
2743
2744 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2745 // ^--- already checked by the previous test
2746
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002747 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002748 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002749 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002751 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2753 ASSERT_EQ(DISPLAY_ID, args.displayId);
2754
2755 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002756 clearViewports();
2757 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002758 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002759 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002761 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2763 ASSERT_EQ(newDisplayId, args.displayId);
2764}
2765
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 KeyboardInputMapper& mapper =
2768 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2769 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002771 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002772 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002773
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002774 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002775 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776}
2777
2778TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002779 KeyboardInputMapper& mapper =
2780 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2781 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002783 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002784 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002786 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788}
2789
2790TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002791 KeyboardInputMapper& mapper =
2792 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2793 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002794
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002795 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796
2797 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2798 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002799 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800 ASSERT_TRUE(flags[0]);
2801 ASSERT_FALSE(flags[1]);
2802}
2803
2804TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002805 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2806 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2807 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2808 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2809 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2810 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002811
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002812 KeyboardInputMapper& mapper =
2813 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2814 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815
2816 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2818 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2819 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820
2821 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002822 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2823 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002824 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2826 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002827 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828
2829 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002830 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2831 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002832 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2833 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2834 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002835 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836
2837 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002838 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2839 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002840 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2841 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2842 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002843 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844
2845 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002846 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2847 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002848 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2849 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2850 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002851 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852
2853 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002854 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2855 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002856 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2857 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2858 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002859 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860
2861 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002862 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2863 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002864 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2865 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2866 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002867 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002868}
2869
Arthur Hung2c9a3342019-07-23 14:18:59 +08002870TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2871 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002872 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2873 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2874 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2875 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002876
2877 // keyboard 2.
2878 const std::string USB2 = "USB2";
2879 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002880 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002881 InputDeviceIdentifier identifier;
2882 identifier.name = "KEYBOARD2";
2883 identifier.location = USB2;
2884 std::unique_ptr<InputDevice> device2 =
2885 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002886 identifier);
2887 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2888 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2889 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2890 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2891 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002893 KeyboardInputMapper& mapper =
2894 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2895 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002896
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002897 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002898 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002899 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002900 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2901 device2->reset(ARBITRARY_TIME);
2902
2903 // Prepared displays and associated info.
2904 constexpr uint8_t hdmi1 = 0;
2905 constexpr uint8_t hdmi2 = 1;
2906 const std::string SECONDARY_UNIQUE_ID = "local:1";
2907
2908 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2909 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2910
2911 // No associated display viewport found, should disable the device.
2912 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2913 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2914 ASSERT_FALSE(device2->isEnabled());
2915
2916 // Prepare second display.
2917 constexpr int32_t newDisplayId = 2;
2918 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002919 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002920 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002921 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002922 // Default device will reconfigure above, need additional reconfiguration for another device.
2923 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2924 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2925
2926 // Device should be enabled after the associated display is found.
2927 ASSERT_TRUE(mDevice->isEnabled());
2928 ASSERT_TRUE(device2->isEnabled());
2929
2930 // Test pad key events
2931 ASSERT_NO_FATAL_FAILURE(
2932 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2933 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2934 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2935 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2936 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2937 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2938 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2939
2940 ASSERT_NO_FATAL_FAILURE(
2941 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2942 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2943 AKEYCODE_DPAD_RIGHT, newDisplayId));
2944 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2945 AKEYCODE_DPAD_DOWN, newDisplayId));
2946 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2947 AKEYCODE_DPAD_LEFT, newDisplayId));
2948}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002950// --- KeyboardInputMapperTest_ExternalDevice ---
2951
2952class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2953protected:
2954 virtual void SetUp() override {
2955 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2956 }
2957};
2958
2959TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002960 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2961 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002962
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002963 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2965 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2966 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002968 KeyboardInputMapper& mapper =
2969 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2970 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002971
2972 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2973 NotifyKeyArgs args;
2974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2975 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2976
2977 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2979 ASSERT_EQ(uint32_t(0), args.policyFlags);
2980
2981 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2983 ASSERT_EQ(uint32_t(0), args.policyFlags);
2984
2985 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2987 ASSERT_EQ(uint32_t(0), args.policyFlags);
2988
2989 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2991 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2992
2993 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2995 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2996}
2997
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002998TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002999 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003000
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003004
Powei Fengd041c5d2019-05-03 17:11:33 -07003005 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003006 KeyboardInputMapper& mapper =
3007 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3008 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003009
3010 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3011 NotifyKeyArgs args;
3012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3013 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3014
3015 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3017 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3018
3019 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3021 ASSERT_EQ(uint32_t(0), args.policyFlags);
3022
3023 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3025 ASSERT_EQ(uint32_t(0), args.policyFlags);
3026
3027 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3029 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3030
3031 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3033 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3034}
3035
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036// --- CursorInputMapperTest ---
3037
3038class CursorInputMapperTest : public InputMapperTest {
3039protected:
3040 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3041
Michael Wright17db18e2020-06-26 20:51:44 +01003042 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043
Prabir Pradhan28efc192019-11-05 01:10:04 +00003044 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045 InputMapperTest::SetUp();
3046
Michael Wright17db18e2020-06-26 20:51:44 +01003047 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003048 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 }
3050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003051 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3052 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003053
3054 void prepareDisplay(int32_t orientation) {
3055 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003056 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003057 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3058 orientation, uniqueId, NO_PORT, viewportType);
3059 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060};
3061
3062const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003064void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3065 int32_t originalY, int32_t rotatedX,
3066 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 NotifyMotionArgs args;
3068
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003069 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3070 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3071 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3075 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3076 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3077 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3078}
3079
3080TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003082 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085}
3086
3087TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092}
3093
3094TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003096 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097
3098 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
3101 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003102 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3103 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3105 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3106
3107 // When the bounds are set, then there should be a valid motion range.
3108 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3109
3110 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003111 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112
3113 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3114 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3115 1, 800 - 1, 0.0f, 0.0f));
3116 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3117 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3118 2, 480 - 1, 0.0f, 0.0f));
3119 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3120 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3121 0.0f, 1.0f, 0.0f, 0.0f));
3122}
3123
3124TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127
3128 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003129 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130
3131 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3132 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3133 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3134 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3135 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3136 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3137 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3138 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3139 0.0f, 1.0f, 0.0f, 0.0f));
3140}
3141
3142TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003144 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145
3146 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3147
3148 NotifyMotionArgs args;
3149
3150 // Button press.
3151 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003152 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3153 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3155 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3156 ASSERT_EQ(DEVICE_ID, args.deviceId);
3157 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3158 ASSERT_EQ(uint32_t(0), args.policyFlags);
3159 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3160 ASSERT_EQ(0, args.flags);
3161 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3162 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3163 ASSERT_EQ(0, args.edgeFlags);
3164 ASSERT_EQ(uint32_t(1), args.pointerCount);
3165 ASSERT_EQ(0, args.pointerProperties[0].id);
3166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3168 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3169 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3170 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3171 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3172
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3174 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3175 ASSERT_EQ(DEVICE_ID, args.deviceId);
3176 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3177 ASSERT_EQ(uint32_t(0), args.policyFlags);
3178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3179 ASSERT_EQ(0, args.flags);
3180 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3181 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3182 ASSERT_EQ(0, args.edgeFlags);
3183 ASSERT_EQ(uint32_t(1), args.pointerCount);
3184 ASSERT_EQ(0, args.pointerProperties[0].id);
3185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3187 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3188 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3189 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3190 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3191
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003193 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3194 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3196 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3197 ASSERT_EQ(DEVICE_ID, args.deviceId);
3198 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3199 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003200 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3201 ASSERT_EQ(0, args.flags);
3202 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3203 ASSERT_EQ(0, args.buttonState);
3204 ASSERT_EQ(0, args.edgeFlags);
3205 ASSERT_EQ(uint32_t(1), args.pointerCount);
3206 ASSERT_EQ(0, args.pointerProperties[0].id);
3207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3209 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3210 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3211 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3212 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3213
3214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3215 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3216 ASSERT_EQ(DEVICE_ID, args.deviceId);
3217 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3218 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3220 ASSERT_EQ(0, args.flags);
3221 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3222 ASSERT_EQ(0, args.buttonState);
3223 ASSERT_EQ(0, args.edgeFlags);
3224 ASSERT_EQ(uint32_t(1), args.pointerCount);
3225 ASSERT_EQ(0, args.pointerProperties[0].id);
3226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3228 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3229 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3230 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3231 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3232}
3233
3234TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003236 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237
3238 NotifyMotionArgs args;
3239
3240 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003241 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3242 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3246 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3247
3248 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003249 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3250 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3254 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3255}
3256
3257TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003259 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260
3261 NotifyMotionArgs args;
3262
3263 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003264 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3265 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3267 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3269 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3270
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3274 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3275
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003277 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3278 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003280 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3282 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3283
3284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3287 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3288}
3289
3290TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003292 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
3294 NotifyMotionArgs args;
3295
3296 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003297 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3298 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3299 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3300 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3302 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3304 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3305 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3306
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3308 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3310 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3311 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3312
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003314 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3315 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3316 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3320 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3321 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3322
3323 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003324 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3325 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3329 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3330
3331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3334 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3335}
3336
3337TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003339 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003341 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3345 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3346 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3347 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3348 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3349 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3350}
3351
3352TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 addConfigurationProperty("cursor.mode", "navigation");
3354 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003355 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003357 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3359 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3360 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3361 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3362 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3363 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3364 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3365 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3366
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003367 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3369 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3374 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3375 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3376
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003377 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3379 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3380 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3381 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3382 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3383 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3384 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3385 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3386
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003387 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3394 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3395 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3396}
3397
3398TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003400 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401
3402 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3403 mFakePointerController->setPosition(100, 200);
3404 mFakePointerController->setButtonState(0);
3405
3406 NotifyMotionArgs motionArgs;
3407 NotifyKeyArgs keyArgs;
3408
3409 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003410 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3411 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3413 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3414 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3415 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3417 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3418
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3421 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3422 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3424 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3425
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003426 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3427 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430 ASSERT_EQ(0, motionArgs.buttonState);
3431 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3433 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3434
3435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003436 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437 ASSERT_EQ(0, motionArgs.buttonState);
3438 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3440 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3441
3442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003444 ASSERT_EQ(0, motionArgs.buttonState);
3445 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3447 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3448
3449 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003450 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3451 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3452 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3455 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3456 motionArgs.buttonState);
3457 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3458 mFakePointerController->getButtonState());
3459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3460 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3461
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3464 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3465 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3466 mFakePointerController->getButtonState());
3467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3468 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3469
3470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3472 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3473 motionArgs.buttonState);
3474 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3475 mFakePointerController->getButtonState());
3476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3477 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3478
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003479 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3480 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003482 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3484 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3486 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3487
3488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003490 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3491 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3493 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3494
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003495 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3496 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3499 ASSERT_EQ(0, motionArgs.buttonState);
3500 ASSERT_EQ(0, mFakePointerController->getButtonState());
3501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3502 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 -08003503 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3504 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003505
3506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507 ASSERT_EQ(0, motionArgs.buttonState);
3508 ASSERT_EQ(0, mFakePointerController->getButtonState());
3509 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3511 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003512
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3514 ASSERT_EQ(0, motionArgs.buttonState);
3515 ASSERT_EQ(0, mFakePointerController->getButtonState());
3516 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3518 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3519
3520 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003521 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3522 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3524 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3525 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003526
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003528 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3530 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3532 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3533
3534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3535 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3536 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3537 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3539 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3540
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003541 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3542 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003544 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545 ASSERT_EQ(0, motionArgs.buttonState);
3546 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3548 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3549
3550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003552 ASSERT_EQ(0, motionArgs.buttonState);
3553 ASSERT_EQ(0, mFakePointerController->getButtonState());
3554
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3556 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3558 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3559 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3560
3561 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003562 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3563 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3565 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3566 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003567
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3571 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3573 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3574
3575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3576 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3577 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3578 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3580 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3581
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003582 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3583 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586 ASSERT_EQ(0, motionArgs.buttonState);
3587 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3589 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003590
3591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3593 ASSERT_EQ(0, motionArgs.buttonState);
3594 ASSERT_EQ(0, mFakePointerController->getButtonState());
3595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3596 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3597
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3599 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3600 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3601
3602 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003603 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3604 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3606 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3607 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003608
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003610 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3612 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3614 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3615
3616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3618 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3619 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3621 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3622
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003623 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3624 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003626 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 ASSERT_EQ(0, motionArgs.buttonState);
3628 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3630 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003631
3632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3634 ASSERT_EQ(0, motionArgs.buttonState);
3635 ASSERT_EQ(0, mFakePointerController->getButtonState());
3636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3637 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3638
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3640 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3641 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3642
3643 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003644 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3645 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3647 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3648 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003649
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3653 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3655 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3656
3657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3658 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3659 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3660 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3662 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3663
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003664 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3665 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 ASSERT_EQ(0, motionArgs.buttonState);
3669 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3671 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003672
3673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3675 ASSERT_EQ(0, motionArgs.buttonState);
3676 ASSERT_EQ(0, mFakePointerController->getButtonState());
3677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3678 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3679
Michael Wrightd02c5b62014-02-10 15:10:22 -08003680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3681 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3682 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3683}
3684
3685TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003687 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003688
3689 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3690 mFakePointerController->setPosition(100, 200);
3691 mFakePointerController->setButtonState(0);
3692
3693 NotifyMotionArgs args;
3694
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003695 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3696 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3697 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003699 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3702 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 +01003703 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003704}
3705
3706TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003707 addConfigurationProperty("cursor.mode", "pointer");
3708 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003709 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003710
3711 NotifyDeviceResetArgs resetArgs;
3712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3713 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3714 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3715
3716 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3717 mFakePointerController->setPosition(100, 200);
3718 mFakePointerController->setButtonState(0);
3719
3720 NotifyMotionArgs args;
3721
3722 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003723 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3724 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3725 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3727 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3730 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 +01003731 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003732
3733 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003734 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3735 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3737 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3738 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3740 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3742 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3743 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3745 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3746
3747 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003748 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3749 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3751 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3752 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3754 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3756 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3757 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3759 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3760
3761 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003762 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3763 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3764 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3766 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3769 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 +01003770 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003771
3772 // Disable pointer capture and check that the device generation got bumped
3773 // and events are generated the usual way.
3774 const uint32_t generation = mFakeContext->getGeneration();
3775 mFakePolicy->setPointerCapture(false);
3776 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3777 ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3778
3779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3780 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3781 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3782
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003783 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3784 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3785 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3787 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3790 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 +01003791 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792}
3793
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003794TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003796
Garfield Tan888a6a42020-01-09 11:39:16 -08003797 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003798 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003799 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3800 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003801 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003802 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3803 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3804
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003805 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3806 mFakePointerController->setPosition(100, 200);
3807 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003808
3809 NotifyMotionArgs args;
3810 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3811 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3812 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3814 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3815 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3817 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 +01003818 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003819 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3820}
3821
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822// --- TouchInputMapperTest ---
3823
3824class TouchInputMapperTest : public InputMapperTest {
3825protected:
3826 static const int32_t RAW_X_MIN;
3827 static const int32_t RAW_X_MAX;
3828 static const int32_t RAW_Y_MIN;
3829 static const int32_t RAW_Y_MAX;
3830 static const int32_t RAW_TOUCH_MIN;
3831 static const int32_t RAW_TOUCH_MAX;
3832 static const int32_t RAW_TOOL_MIN;
3833 static const int32_t RAW_TOOL_MAX;
3834 static const int32_t RAW_PRESSURE_MIN;
3835 static const int32_t RAW_PRESSURE_MAX;
3836 static const int32_t RAW_ORIENTATION_MIN;
3837 static const int32_t RAW_ORIENTATION_MAX;
3838 static const int32_t RAW_DISTANCE_MIN;
3839 static const int32_t RAW_DISTANCE_MAX;
3840 static const int32_t RAW_TILT_MIN;
3841 static const int32_t RAW_TILT_MAX;
3842 static const int32_t RAW_ID_MIN;
3843 static const int32_t RAW_ID_MAX;
3844 static const int32_t RAW_SLOT_MIN;
3845 static const int32_t RAW_SLOT_MAX;
3846 static const float X_PRECISION;
3847 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003848 static const float X_PRECISION_VIRTUAL;
3849 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850
3851 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003852 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853
3854 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3855
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003856 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003857 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003858
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859 enum Axes {
3860 POSITION = 1 << 0,
3861 TOUCH = 1 << 1,
3862 TOOL = 1 << 2,
3863 PRESSURE = 1 << 3,
3864 ORIENTATION = 1 << 4,
3865 MINOR = 1 << 5,
3866 ID = 1 << 6,
3867 DISTANCE = 1 << 7,
3868 TILT = 1 << 8,
3869 SLOT = 1 << 9,
3870 TOOL_TYPE = 1 << 10,
3871 };
3872
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003873 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3874 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003875 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003877 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878 int32_t toRawX(float displayX);
3879 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003880 float toCookedX(float rawX, float rawY);
3881 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003883 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003885 float toDisplayY(int32_t rawY, int32_t displayHeight);
3886
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887};
3888
3889const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3890const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3891const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3892const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3893const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3894const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3895const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3896const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003897const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3898const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003899const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3900const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3901const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3902const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3903const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3904const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3905const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3906const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3907const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3908const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3909const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3910const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003911const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3912 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3913const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3914 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003915const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3916 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917
3918const float TouchInputMapperTest::GEOMETRIC_SCALE =
3919 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3920 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3921
3922const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3923 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3924 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3925};
3926
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003927void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003928 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3929 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003930}
3931
3932void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3933 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3934 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935}
3936
Santos Cordonfa5cf462017-04-05 10:37:00 -07003937void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003938 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
3939 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
3940 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003941}
3942
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003944 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3945 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3946 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3947 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948}
3949
Jason Gerecke489fda82012-09-07 17:19:40 -07003950void TouchInputMapperTest::prepareLocationCalibration() {
3951 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3952}
3953
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954int32_t TouchInputMapperTest::toRawX(float displayX) {
3955 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3956}
3957
3958int32_t TouchInputMapperTest::toRawY(float displayY) {
3959 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3960}
3961
Jason Gerecke489fda82012-09-07 17:19:40 -07003962float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3963 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3964 return rawX;
3965}
3966
3967float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3968 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3969 return rawY;
3970}
3971
Michael Wrightd02c5b62014-02-10 15:10:22 -08003972float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003973 return toDisplayX(rawX, DISPLAY_WIDTH);
3974}
3975
3976float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3977 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978}
3979
3980float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003981 return toDisplayY(rawY, DISPLAY_HEIGHT);
3982}
3983
3984float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3985 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986}
3987
3988
3989// --- SingleTouchInputMapperTest ---
3990
3991class SingleTouchInputMapperTest : public TouchInputMapperTest {
3992protected:
3993 void prepareButtons();
3994 void prepareAxes(int axes);
3995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003996 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3997 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3998 void processUp(SingleTouchInputMapper& mappery);
3999 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4000 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4001 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4002 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4003 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4004 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005};
4006
4007void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004008 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004009}
4010
4011void SingleTouchInputMapperTest::prepareAxes(int axes) {
4012 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004013 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4014 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015 }
4016 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004017 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4018 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 }
4020 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004021 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4022 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 }
4024 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004025 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4026 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027 }
4028 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004029 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4030 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 }
4032}
4033
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004034void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004035 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4036 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4037 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038}
4039
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004040void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004041 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4042 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043}
4044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004045void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004046 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047}
4048
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004049void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004050 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051}
4052
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004053void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4054 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004055 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056}
4057
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004058void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004059 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060}
4061
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004062void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4063 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004064 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4065 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066}
4067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004068void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4069 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004070 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071}
4072
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004073void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004074 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075}
4076
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 prepareButtons();
4079 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004080 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004082 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083}
4084
4085TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004086 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4087 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088 prepareButtons();
4089 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004090 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004092 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093}
4094
4095TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096 prepareButtons();
4097 prepareAxes(POSITION);
4098 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004099 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004101 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102}
4103
4104TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105 prepareButtons();
4106 prepareAxes(POSITION);
4107 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004108 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004110 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111}
4112
4113TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114 addConfigurationProperty("touch.deviceType", "touchScreen");
4115 prepareDisplay(DISPLAY_ORIENTATION_0);
4116 prepareButtons();
4117 prepareAxes(POSITION);
4118 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004119 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120
4121 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004122 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123
4124 // Virtual key is down.
4125 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4126 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4127 processDown(mapper, x, y);
4128 processSync(mapper);
4129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004131 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132
4133 // Virtual key is up.
4134 processUp(mapper);
4135 processSync(mapper);
4136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004138 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139}
4140
4141TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 addConfigurationProperty("touch.deviceType", "touchScreen");
4143 prepareDisplay(DISPLAY_ORIENTATION_0);
4144 prepareButtons();
4145 prepareAxes(POSITION);
4146 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004147 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148
4149 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004150 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151
4152 // Virtual key is down.
4153 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4154 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4155 processDown(mapper, x, y);
4156 processSync(mapper);
4157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4158
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004159 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160
4161 // Virtual key is up.
4162 processUp(mapper);
4163 processSync(mapper);
4164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004166 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167}
4168
4169TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 addConfigurationProperty("touch.deviceType", "touchScreen");
4171 prepareDisplay(DISPLAY_ORIENTATION_0);
4172 prepareButtons();
4173 prepareAxes(POSITION);
4174 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004175 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176
4177 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4178 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004179 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 ASSERT_TRUE(flags[0]);
4181 ASSERT_FALSE(flags[1]);
4182}
4183
4184TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185 addConfigurationProperty("touch.deviceType", "touchScreen");
4186 prepareDisplay(DISPLAY_ORIENTATION_0);
4187 prepareButtons();
4188 prepareAxes(POSITION);
4189 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004190 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191
4192 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4193
4194 NotifyKeyArgs args;
4195
4196 // Press virtual key.
4197 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4198 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4199 processDown(mapper, x, y);
4200 processSync(mapper);
4201
4202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4203 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4204 ASSERT_EQ(DEVICE_ID, args.deviceId);
4205 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4206 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4207 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4208 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4209 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4210 ASSERT_EQ(KEY_HOME, args.scanCode);
4211 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4212 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4213
4214 // Release virtual key.
4215 processUp(mapper);
4216 processSync(mapper);
4217
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4219 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4220 ASSERT_EQ(DEVICE_ID, args.deviceId);
4221 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4222 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4223 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4224 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4225 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4226 ASSERT_EQ(KEY_HOME, args.scanCode);
4227 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4228 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4229
4230 // Should not have sent any motions.
4231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4232}
4233
4234TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 addConfigurationProperty("touch.deviceType", "touchScreen");
4236 prepareDisplay(DISPLAY_ORIENTATION_0);
4237 prepareButtons();
4238 prepareAxes(POSITION);
4239 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004240 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241
4242 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4243
4244 NotifyKeyArgs keyArgs;
4245
4246 // Press virtual key.
4247 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4248 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4249 processDown(mapper, x, y);
4250 processSync(mapper);
4251
4252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4253 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4254 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4255 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4256 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4257 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4258 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4259 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4260 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4261 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4262 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4263
4264 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4265 // into the display area.
4266 y -= 100;
4267 processMove(mapper, x, y);
4268 processSync(mapper);
4269
4270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4271 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4272 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4273 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4274 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4275 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4276 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4277 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4278 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4279 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4280 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4281 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4282
4283 NotifyMotionArgs motionArgs;
4284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4286 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4287 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4288 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4289 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4290 ASSERT_EQ(0, motionArgs.flags);
4291 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4292 ASSERT_EQ(0, motionArgs.buttonState);
4293 ASSERT_EQ(0, motionArgs.edgeFlags);
4294 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4295 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4298 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4299 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4300 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4301 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4302
4303 // Keep moving out of bounds. Should generate a pointer move.
4304 y -= 50;
4305 processMove(mapper, x, y);
4306 processSync(mapper);
4307
4308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4310 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4311 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4312 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4314 ASSERT_EQ(0, motionArgs.flags);
4315 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4316 ASSERT_EQ(0, motionArgs.buttonState);
4317 ASSERT_EQ(0, motionArgs.edgeFlags);
4318 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4319 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4322 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4323 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4324 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4325 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4326
4327 // Release out of bounds. Should generate a pointer up.
4328 processUp(mapper);
4329 processSync(mapper);
4330
4331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4333 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4334 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4335 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4336 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4337 ASSERT_EQ(0, motionArgs.flags);
4338 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4339 ASSERT_EQ(0, motionArgs.buttonState);
4340 ASSERT_EQ(0, motionArgs.edgeFlags);
4341 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4342 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4345 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4346 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4347 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4349
4350 // Should not have sent any more keys or motions.
4351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4353}
4354
4355TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 addConfigurationProperty("touch.deviceType", "touchScreen");
4357 prepareDisplay(DISPLAY_ORIENTATION_0);
4358 prepareButtons();
4359 prepareAxes(POSITION);
4360 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004361 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362
4363 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4364
4365 NotifyMotionArgs motionArgs;
4366
4367 // Initially go down out of bounds.
4368 int32_t x = -10;
4369 int32_t y = -10;
4370 processDown(mapper, x, y);
4371 processSync(mapper);
4372
4373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4374
4375 // Move into the display area. Should generate a pointer down.
4376 x = 50;
4377 y = 75;
4378 processMove(mapper, x, y);
4379 processSync(mapper);
4380
4381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4383 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4384 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4385 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4386 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4387 ASSERT_EQ(0, motionArgs.flags);
4388 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4389 ASSERT_EQ(0, motionArgs.buttonState);
4390 ASSERT_EQ(0, motionArgs.edgeFlags);
4391 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4392 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4395 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4396 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4397 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4399
4400 // Release. Should generate a pointer up.
4401 processUp(mapper);
4402 processSync(mapper);
4403
4404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4406 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4407 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4408 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4409 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4410 ASSERT_EQ(0, motionArgs.flags);
4411 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4412 ASSERT_EQ(0, motionArgs.buttonState);
4413 ASSERT_EQ(0, motionArgs.edgeFlags);
4414 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4415 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4418 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4419 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4420 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4421 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4422
4423 // Should not have sent any more keys or motions.
4424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4426}
4427
Santos Cordonfa5cf462017-04-05 10:37:00 -07004428TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004429 addConfigurationProperty("touch.deviceType", "touchScreen");
4430 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4431
4432 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4433 prepareButtons();
4434 prepareAxes(POSITION);
4435 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004436 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004437
4438 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4439
4440 NotifyMotionArgs motionArgs;
4441
4442 // Down.
4443 int32_t x = 100;
4444 int32_t y = 125;
4445 processDown(mapper, x, y);
4446 processSync(mapper);
4447
4448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4449 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4450 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4451 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4452 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4453 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4455 ASSERT_EQ(0, motionArgs.flags);
4456 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4457 ASSERT_EQ(0, motionArgs.buttonState);
4458 ASSERT_EQ(0, motionArgs.edgeFlags);
4459 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4460 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4463 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4464 1, 0, 0, 0, 0, 0, 0, 0));
4465 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4466 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4468
4469 // Move.
4470 x += 50;
4471 y += 75;
4472 processMove(mapper, x, y);
4473 processSync(mapper);
4474
4475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4476 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4477 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4478 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4479 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4480 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4482 ASSERT_EQ(0, motionArgs.flags);
4483 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4484 ASSERT_EQ(0, motionArgs.buttonState);
4485 ASSERT_EQ(0, motionArgs.edgeFlags);
4486 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4487 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4490 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4491 1, 0, 0, 0, 0, 0, 0, 0));
4492 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4493 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4494 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4495
4496 // Up.
4497 processUp(mapper);
4498 processSync(mapper);
4499
4500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4501 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4502 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4503 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4504 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4505 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4507 ASSERT_EQ(0, motionArgs.flags);
4508 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4509 ASSERT_EQ(0, motionArgs.buttonState);
4510 ASSERT_EQ(0, motionArgs.edgeFlags);
4511 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4512 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4513 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4515 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4516 1, 0, 0, 0, 0, 0, 0, 0));
4517 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4518 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4519 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4520
4521 // Should not have sent any more keys or motions.
4522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4524}
4525
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527 addConfigurationProperty("touch.deviceType", "touchScreen");
4528 prepareDisplay(DISPLAY_ORIENTATION_0);
4529 prepareButtons();
4530 prepareAxes(POSITION);
4531 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004532 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533
4534 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4535
4536 NotifyMotionArgs motionArgs;
4537
4538 // Down.
4539 int32_t x = 100;
4540 int32_t y = 125;
4541 processDown(mapper, x, y);
4542 processSync(mapper);
4543
4544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4545 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4546 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4547 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4548 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4550 ASSERT_EQ(0, motionArgs.flags);
4551 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4552 ASSERT_EQ(0, motionArgs.buttonState);
4553 ASSERT_EQ(0, motionArgs.edgeFlags);
4554 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4558 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4559 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4560 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4561 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4562
4563 // Move.
4564 x += 50;
4565 y += 75;
4566 processMove(mapper, x, y);
4567 processSync(mapper);
4568
4569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4570 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4571 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4572 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4573 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4575 ASSERT_EQ(0, motionArgs.flags);
4576 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4577 ASSERT_EQ(0, motionArgs.buttonState);
4578 ASSERT_EQ(0, motionArgs.edgeFlags);
4579 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4580 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4583 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4584 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4585 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4586 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4587
4588 // Up.
4589 processUp(mapper);
4590 processSync(mapper);
4591
4592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4593 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4594 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4595 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4596 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4597 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4598 ASSERT_EQ(0, motionArgs.flags);
4599 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4600 ASSERT_EQ(0, motionArgs.buttonState);
4601 ASSERT_EQ(0, motionArgs.edgeFlags);
4602 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4603 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4606 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4607 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4608 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4609 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4610
4611 // Should not have sent any more keys or motions.
4612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4614}
4615
4616TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617 addConfigurationProperty("touch.deviceType", "touchScreen");
4618 prepareButtons();
4619 prepareAxes(POSITION);
4620 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004621 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622
4623 NotifyMotionArgs args;
4624
4625 // Rotation 90.
4626 prepareDisplay(DISPLAY_ORIENTATION_90);
4627 processDown(mapper, toRawX(50), toRawY(75));
4628 processSync(mapper);
4629
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4631 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4632 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4633
4634 processUp(mapper);
4635 processSync(mapper);
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4637}
4638
4639TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640 addConfigurationProperty("touch.deviceType", "touchScreen");
4641 prepareButtons();
4642 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004643 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644
4645 NotifyMotionArgs args;
4646
4647 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004648 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 prepareDisplay(DISPLAY_ORIENTATION_0);
4650 processDown(mapper, toRawX(50), toRawY(75));
4651 processSync(mapper);
4652
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4654 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4655 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4656
4657 processUp(mapper);
4658 processSync(mapper);
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4660
4661 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004662 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 prepareDisplay(DISPLAY_ORIENTATION_90);
4664 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4665 processSync(mapper);
4666
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4668 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4669 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4670
4671 processUp(mapper);
4672 processSync(mapper);
4673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4674
4675 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004676 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677 prepareDisplay(DISPLAY_ORIENTATION_180);
4678 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4679 processSync(mapper);
4680
4681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4682 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4683 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4684
4685 processUp(mapper);
4686 processSync(mapper);
4687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4688
4689 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004690 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004691 prepareDisplay(DISPLAY_ORIENTATION_270);
4692 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4693 processSync(mapper);
4694
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4696 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4697 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4698
4699 processUp(mapper);
4700 processSync(mapper);
4701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4702}
4703
4704TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705 addConfigurationProperty("touch.deviceType", "touchScreen");
4706 prepareDisplay(DISPLAY_ORIENTATION_0);
4707 prepareButtons();
4708 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004709 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710
4711 // These calculations are based on the input device calibration documentation.
4712 int32_t rawX = 100;
4713 int32_t rawY = 200;
4714 int32_t rawPressure = 10;
4715 int32_t rawToolMajor = 12;
4716 int32_t rawDistance = 2;
4717 int32_t rawTiltX = 30;
4718 int32_t rawTiltY = 110;
4719
4720 float x = toDisplayX(rawX);
4721 float y = toDisplayY(rawY);
4722 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4723 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4724 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4725 float distance = float(rawDistance);
4726
4727 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4728 float tiltScale = M_PI / 180;
4729 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4730 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4731 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4732 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4733
4734 processDown(mapper, rawX, rawY);
4735 processPressure(mapper, rawPressure);
4736 processToolMajor(mapper, rawToolMajor);
4737 processDistance(mapper, rawDistance);
4738 processTilt(mapper, rawTiltX, rawTiltY);
4739 processSync(mapper);
4740
4741 NotifyMotionArgs args;
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4744 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4745 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4746}
4747
Jason Gerecke489fda82012-09-07 17:19:40 -07004748TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004749 addConfigurationProperty("touch.deviceType", "touchScreen");
4750 prepareDisplay(DISPLAY_ORIENTATION_0);
4751 prepareLocationCalibration();
4752 prepareButtons();
4753 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004754 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004755
4756 int32_t rawX = 100;
4757 int32_t rawY = 200;
4758
4759 float x = toDisplayX(toCookedX(rawX, rawY));
4760 float y = toDisplayY(toCookedY(rawX, rawY));
4761
4762 processDown(mapper, rawX, rawY);
4763 processSync(mapper);
4764
4765 NotifyMotionArgs args;
4766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4768 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4769}
4770
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 addConfigurationProperty("touch.deviceType", "touchScreen");
4773 prepareDisplay(DISPLAY_ORIENTATION_0);
4774 prepareButtons();
4775 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004776 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777
4778 NotifyMotionArgs motionArgs;
4779 NotifyKeyArgs keyArgs;
4780
4781 processDown(mapper, 100, 200);
4782 processSync(mapper);
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4784 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4785 ASSERT_EQ(0, motionArgs.buttonState);
4786
4787 // press BTN_LEFT, release BTN_LEFT
4788 processKey(mapper, BTN_LEFT, 1);
4789 processSync(mapper);
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4792 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4793
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4795 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4796 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4797
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 processKey(mapper, BTN_LEFT, 0);
4799 processSync(mapper);
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004803
4804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004806 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807
4808 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4809 processKey(mapper, BTN_RIGHT, 1);
4810 processKey(mapper, BTN_MIDDLE, 1);
4811 processSync(mapper);
4812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4814 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4815 motionArgs.buttonState);
4816
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4818 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4819 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4820
4821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4822 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4823 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4824 motionArgs.buttonState);
4825
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 processKey(mapper, BTN_RIGHT, 0);
4827 processSync(mapper);
4828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004829 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004831
4832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004834 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835
4836 processKey(mapper, BTN_MIDDLE, 0);
4837 processSync(mapper);
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004839 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004841
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004844 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845
4846 // press BTN_BACK, release BTN_BACK
4847 processKey(mapper, BTN_BACK, 1);
4848 processSync(mapper);
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4850 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4851 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004852
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004855 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4856
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4859 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860
4861 processKey(mapper, BTN_BACK, 0);
4862 processSync(mapper);
4863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004864 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004866
4867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004869 ASSERT_EQ(0, motionArgs.buttonState);
4870
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4872 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4873 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4874
4875 // press BTN_SIDE, release BTN_SIDE
4876 processKey(mapper, BTN_SIDE, 1);
4877 processSync(mapper);
4878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4879 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4880 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004881
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004884 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4885
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4888 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889
4890 processKey(mapper, BTN_SIDE, 0);
4891 processSync(mapper);
4892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004895
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004898 ASSERT_EQ(0, motionArgs.buttonState);
4899
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4901 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4902 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4903
4904 // press BTN_FORWARD, release BTN_FORWARD
4905 processKey(mapper, BTN_FORWARD, 1);
4906 processSync(mapper);
4907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4908 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4909 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004910
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004913 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4914
4915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4916 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4917 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918
4919 processKey(mapper, BTN_FORWARD, 0);
4920 processSync(mapper);
4921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004922 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004924
4925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004927 ASSERT_EQ(0, motionArgs.buttonState);
4928
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4930 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4931 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4932
4933 // press BTN_EXTRA, release BTN_EXTRA
4934 processKey(mapper, BTN_EXTRA, 1);
4935 processSync(mapper);
4936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4937 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4938 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004939
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004942 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4943
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4946 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947
4948 processKey(mapper, BTN_EXTRA, 0);
4949 processSync(mapper);
4950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004951 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004953
4954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004956 ASSERT_EQ(0, motionArgs.buttonState);
4957
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4959 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4960 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4961
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4963
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 // press BTN_STYLUS, release BTN_STYLUS
4965 processKey(mapper, BTN_STYLUS, 1);
4966 processSync(mapper);
4967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004969 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4970
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4972 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4973 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974
4975 processKey(mapper, BTN_STYLUS, 0);
4976 processSync(mapper);
4977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004980
4981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004983 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984
4985 // press BTN_STYLUS2, release BTN_STYLUS2
4986 processKey(mapper, BTN_STYLUS2, 1);
4987 processSync(mapper);
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004990 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4991
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4993 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4994 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995
4996 processKey(mapper, BTN_STYLUS2, 0);
4997 processSync(mapper);
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005001
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005004 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005
5006 // release touch
5007 processUp(mapper);
5008 processSync(mapper);
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5010 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5011 ASSERT_EQ(0, motionArgs.buttonState);
5012}
5013
5014TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 addConfigurationProperty("touch.deviceType", "touchScreen");
5016 prepareDisplay(DISPLAY_ORIENTATION_0);
5017 prepareButtons();
5018 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005019 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020
5021 NotifyMotionArgs motionArgs;
5022
5023 // default tool type is finger
5024 processDown(mapper, 100, 200);
5025 processSync(mapper);
5026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5027 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5029
5030 // eraser
5031 processKey(mapper, BTN_TOOL_RUBBER, 1);
5032 processSync(mapper);
5033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5036
5037 // stylus
5038 processKey(mapper, BTN_TOOL_RUBBER, 0);
5039 processKey(mapper, BTN_TOOL_PEN, 1);
5040 processSync(mapper);
5041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5044
5045 // brush
5046 processKey(mapper, BTN_TOOL_PEN, 0);
5047 processKey(mapper, BTN_TOOL_BRUSH, 1);
5048 processSync(mapper);
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5052
5053 // pencil
5054 processKey(mapper, BTN_TOOL_BRUSH, 0);
5055 processKey(mapper, BTN_TOOL_PENCIL, 1);
5056 processSync(mapper);
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5060
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005061 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062 processKey(mapper, BTN_TOOL_PENCIL, 0);
5063 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5064 processSync(mapper);
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5068
5069 // mouse
5070 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5071 processKey(mapper, BTN_TOOL_MOUSE, 1);
5072 processSync(mapper);
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5076
5077 // lens
5078 processKey(mapper, BTN_TOOL_MOUSE, 0);
5079 processKey(mapper, BTN_TOOL_LENS, 1);
5080 processSync(mapper);
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5084
5085 // double-tap
5086 processKey(mapper, BTN_TOOL_LENS, 0);
5087 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5088 processSync(mapper);
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5092
5093 // triple-tap
5094 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5095 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5096 processSync(mapper);
5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5100
5101 // quad-tap
5102 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5103 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5104 processSync(mapper);
5105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5108
5109 // finger
5110 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5111 processKey(mapper, BTN_TOOL_FINGER, 1);
5112 processSync(mapper);
5113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5116
5117 // stylus trumps finger
5118 processKey(mapper, BTN_TOOL_PEN, 1);
5119 processSync(mapper);
5120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5123
5124 // eraser trumps stylus
5125 processKey(mapper, BTN_TOOL_RUBBER, 1);
5126 processSync(mapper);
5127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5130
5131 // mouse trumps eraser
5132 processKey(mapper, BTN_TOOL_MOUSE, 1);
5133 processSync(mapper);
5134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5137
5138 // back to default tool type
5139 processKey(mapper, BTN_TOOL_MOUSE, 0);
5140 processKey(mapper, BTN_TOOL_RUBBER, 0);
5141 processKey(mapper, BTN_TOOL_PEN, 0);
5142 processKey(mapper, BTN_TOOL_FINGER, 0);
5143 processSync(mapper);
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5147}
5148
5149TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150 addConfigurationProperty("touch.deviceType", "touchScreen");
5151 prepareDisplay(DISPLAY_ORIENTATION_0);
5152 prepareButtons();
5153 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005154 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005155 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156
5157 NotifyMotionArgs motionArgs;
5158
5159 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5160 processKey(mapper, BTN_TOOL_FINGER, 1);
5161 processMove(mapper, 100, 200);
5162 processSync(mapper);
5163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5164 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5166 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5167
5168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5169 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5171 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5172
5173 // move a little
5174 processMove(mapper, 150, 250);
5175 processSync(mapper);
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5177 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5179 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5180
5181 // down when BTN_TOUCH is pressed, pressure defaults to 1
5182 processKey(mapper, BTN_TOUCH, 1);
5183 processSync(mapper);
5184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5185 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5187 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5188
5189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5190 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5192 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5193
5194 // up when BTN_TOUCH is released, hover restored
5195 processKey(mapper, BTN_TOUCH, 0);
5196 processSync(mapper);
5197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5198 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5200 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5201
5202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5203 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5205 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5206
5207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5208 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5210 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5211
5212 // exit hover when pointer goes away
5213 processKey(mapper, BTN_TOOL_FINGER, 0);
5214 processSync(mapper);
5215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5216 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5218 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5219}
5220
5221TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005222 addConfigurationProperty("touch.deviceType", "touchScreen");
5223 prepareDisplay(DISPLAY_ORIENTATION_0);
5224 prepareButtons();
5225 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005226 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227
5228 NotifyMotionArgs motionArgs;
5229
5230 // initially hovering because pressure is 0
5231 processDown(mapper, 100, 200);
5232 processPressure(mapper, 0);
5233 processSync(mapper);
5234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5235 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5237 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5238
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5242 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5243
5244 // move a little
5245 processMove(mapper, 150, 250);
5246 processSync(mapper);
5247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5250 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5251
5252 // down when pressure is non-zero
5253 processPressure(mapper, RAW_PRESSURE_MAX);
5254 processSync(mapper);
5255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5256 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5258 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5259
5260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5263 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5264
5265 // up when pressure becomes 0, hover restored
5266 processPressure(mapper, 0);
5267 processSync(mapper);
5268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5269 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5271 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5272
5273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5274 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5276 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5277
5278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5279 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5281 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5282
5283 // exit hover when pointer goes away
5284 processUp(mapper);
5285 processSync(mapper);
5286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5287 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5289 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5290}
5291
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292// --- MultiTouchInputMapperTest ---
5293
5294class MultiTouchInputMapperTest : public TouchInputMapperTest {
5295protected:
5296 void prepareAxes(int axes);
5297
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005298 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5299 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5300 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5301 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5302 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5303 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5304 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5305 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5306 void processId(MultiTouchInputMapper& mapper, int32_t id);
5307 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5308 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5309 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5310 void processMTSync(MultiTouchInputMapper& mapper);
5311 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312};
5313
5314void MultiTouchInputMapperTest::prepareAxes(int axes) {
5315 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005316 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5317 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318 }
5319 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005320 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5321 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005323 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5324 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325 }
5326 }
5327 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005328 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5329 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5332 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 }
5334 }
5335 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005336 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5337 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338 }
5339 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005340 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5341 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342 }
5343 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005344 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5345 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346 }
5347 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005348 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5349 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350 }
5351 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005352 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5353 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354 }
5355 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005356 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357 }
5358}
5359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005360void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5361 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005362 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5363 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364}
5365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5367 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005368 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369}
5370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005371void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5372 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005373 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374}
5375
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005376void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005377 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005378}
5379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005380void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005381 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382}
5383
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5385 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005386 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387}
5388
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005389void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005390 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391}
5392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005393void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005394 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395}
5396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005397void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005398 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399}
5400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005401void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005402 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403}
5404
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005405void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005406 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407}
5408
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005409void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5410 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005411 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412}
5413
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005414void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005415 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416}
5417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005418void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005419 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420}
5421
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 addConfigurationProperty("touch.deviceType", "touchScreen");
5424 prepareDisplay(DISPLAY_ORIENTATION_0);
5425 prepareAxes(POSITION);
5426 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428
5429 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5430
5431 NotifyMotionArgs motionArgs;
5432
5433 // Two fingers down at once.
5434 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5435 processPosition(mapper, x1, y1);
5436 processMTSync(mapper);
5437 processPosition(mapper, x2, y2);
5438 processMTSync(mapper);
5439 processSync(mapper);
5440
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5442 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5443 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5444 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5445 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5447 ASSERT_EQ(0, motionArgs.flags);
5448 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5449 ASSERT_EQ(0, motionArgs.buttonState);
5450 ASSERT_EQ(0, motionArgs.edgeFlags);
5451 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5452 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5455 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5456 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5457 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5459
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5462 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5463 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5464 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5465 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5466 motionArgs.action);
5467 ASSERT_EQ(0, motionArgs.flags);
5468 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5469 ASSERT_EQ(0, motionArgs.buttonState);
5470 ASSERT_EQ(0, motionArgs.edgeFlags);
5471 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5472 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5474 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5477 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5479 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5480 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5481 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5483
5484 // Move.
5485 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5486 processPosition(mapper, x1, y1);
5487 processMTSync(mapper);
5488 processPosition(mapper, x2, y2);
5489 processMTSync(mapper);
5490 processSync(mapper);
5491
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5493 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5494 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5495 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5496 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5498 ASSERT_EQ(0, motionArgs.flags);
5499 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5500 ASSERT_EQ(0, motionArgs.buttonState);
5501 ASSERT_EQ(0, motionArgs.edgeFlags);
5502 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5503 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5505 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5508 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5510 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5511 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5512 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5513 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5514
5515 // First finger up.
5516 x2 += 15; y2 -= 20;
5517 processPosition(mapper, x2, y2);
5518 processMTSync(mapper);
5519 processSync(mapper);
5520
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5523 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5524 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5525 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5526 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5527 motionArgs.action);
5528 ASSERT_EQ(0, motionArgs.flags);
5529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5530 ASSERT_EQ(0, motionArgs.buttonState);
5531 ASSERT_EQ(0, motionArgs.edgeFlags);
5532 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5535 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5538 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5540 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5541 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5542 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5544
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5547 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5548 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5549 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5551 ASSERT_EQ(0, motionArgs.flags);
5552 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5553 ASSERT_EQ(0, motionArgs.buttonState);
5554 ASSERT_EQ(0, motionArgs.edgeFlags);
5555 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5556 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5559 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5560 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5561 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5563
5564 // Move.
5565 x2 += 20; y2 -= 25;
5566 processPosition(mapper, x2, y2);
5567 processMTSync(mapper);
5568 processSync(mapper);
5569
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5571 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5572 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5573 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5574 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5576 ASSERT_EQ(0, motionArgs.flags);
5577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5578 ASSERT_EQ(0, motionArgs.buttonState);
5579 ASSERT_EQ(0, motionArgs.edgeFlags);
5580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5581 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5584 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5585 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5586 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5587 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5588
5589 // New finger down.
5590 int32_t x3 = 700, y3 = 300;
5591 processPosition(mapper, x2, y2);
5592 processMTSync(mapper);
5593 processPosition(mapper, x3, y3);
5594 processMTSync(mapper);
5595 processSync(mapper);
5596
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5598 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5599 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5600 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5601 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5602 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5603 motionArgs.action);
5604 ASSERT_EQ(0, motionArgs.flags);
5605 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5606 ASSERT_EQ(0, motionArgs.buttonState);
5607 ASSERT_EQ(0, motionArgs.edgeFlags);
5608 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5609 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5611 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5614 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5616 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5617 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5618 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5619 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5620
5621 // Second finger up.
5622 x3 += 30; y3 -= 20;
5623 processPosition(mapper, x3, y3);
5624 processMTSync(mapper);
5625 processSync(mapper);
5626
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5628 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5629 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5630 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5631 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5632 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5633 motionArgs.action);
5634 ASSERT_EQ(0, motionArgs.flags);
5635 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5636 ASSERT_EQ(0, motionArgs.buttonState);
5637 ASSERT_EQ(0, motionArgs.edgeFlags);
5638 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5639 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5641 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5644 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5646 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5647 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5648 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5649 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5650
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5652 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5653 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5654 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5655 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5657 ASSERT_EQ(0, motionArgs.flags);
5658 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5659 ASSERT_EQ(0, motionArgs.buttonState);
5660 ASSERT_EQ(0, motionArgs.edgeFlags);
5661 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5665 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5666 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5667 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5668 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5669
5670 // Last finger up.
5671 processMTSync(mapper);
5672 processSync(mapper);
5673
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5675 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5676 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5677 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5678 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5679 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5680 ASSERT_EQ(0, motionArgs.flags);
5681 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5682 ASSERT_EQ(0, motionArgs.buttonState);
5683 ASSERT_EQ(0, motionArgs.edgeFlags);
5684 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5685 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5686 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5688 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5689 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5690 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5691 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5692
5693 // Should not have sent any more keys or motions.
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5696}
5697
5698TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699 addConfigurationProperty("touch.deviceType", "touchScreen");
5700 prepareDisplay(DISPLAY_ORIENTATION_0);
5701 prepareAxes(POSITION | ID);
5702 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005703 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704
5705 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5706
5707 NotifyMotionArgs motionArgs;
5708
5709 // Two fingers down at once.
5710 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5711 processPosition(mapper, x1, y1);
5712 processId(mapper, 1);
5713 processMTSync(mapper);
5714 processPosition(mapper, x2, y2);
5715 processId(mapper, 2);
5716 processMTSync(mapper);
5717 processSync(mapper);
5718
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5720 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5721 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5722 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5725 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5726
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5728 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5729 motionArgs.action);
5730 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5731 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5732 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5733 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5736 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5738 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5739
5740 // Move.
5741 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5742 processPosition(mapper, x1, y1);
5743 processId(mapper, 1);
5744 processMTSync(mapper);
5745 processPosition(mapper, x2, y2);
5746 processId(mapper, 2);
5747 processMTSync(mapper);
5748 processSync(mapper);
5749
5750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5752 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5755 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5758 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5760 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5761
5762 // First finger up.
5763 x2 += 15; y2 -= 20;
5764 processPosition(mapper, x2, y2);
5765 processId(mapper, 2);
5766 processMTSync(mapper);
5767 processSync(mapper);
5768
5769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5770 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5771 motionArgs.action);
5772 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5775 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5778 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5780 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5781
5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5784 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5785 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5788 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5789
5790 // Move.
5791 x2 += 20; y2 -= 25;
5792 processPosition(mapper, x2, y2);
5793 processId(mapper, 2);
5794 processMTSync(mapper);
5795 processSync(mapper);
5796
5797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5798 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5799 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5800 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5803 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5804
5805 // New finger down.
5806 int32_t x3 = 700, y3 = 300;
5807 processPosition(mapper, x2, y2);
5808 processId(mapper, 2);
5809 processMTSync(mapper);
5810 processPosition(mapper, x3, y3);
5811 processId(mapper, 3);
5812 processMTSync(mapper);
5813 processSync(mapper);
5814
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5817 motionArgs.action);
5818 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5819 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5821 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5824 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5826 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5827
5828 // Second finger up.
5829 x3 += 30; y3 -= 20;
5830 processPosition(mapper, x3, y3);
5831 processId(mapper, 3);
5832 processMTSync(mapper);
5833 processSync(mapper);
5834
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5836 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5837 motionArgs.action);
5838 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5841 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5844 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5846 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5847
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5850 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5851 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5854 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5855
5856 // Last finger up.
5857 processMTSync(mapper);
5858 processSync(mapper);
5859
5860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5861 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5862 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5863 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5866 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5867
5868 // Should not have sent any more keys or motions.
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5871}
5872
5873TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874 addConfigurationProperty("touch.deviceType", "touchScreen");
5875 prepareDisplay(DISPLAY_ORIENTATION_0);
5876 prepareAxes(POSITION | ID | SLOT);
5877 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005878 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879
5880 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5881
5882 NotifyMotionArgs motionArgs;
5883
5884 // Two fingers down at once.
5885 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5886 processPosition(mapper, x1, y1);
5887 processId(mapper, 1);
5888 processSlot(mapper, 1);
5889 processPosition(mapper, x2, y2);
5890 processId(mapper, 2);
5891 processSync(mapper);
5892
5893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5894 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5895 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5896 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5899 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5900
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5902 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5903 motionArgs.action);
5904 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5905 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5907 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5910 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5912 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5913
5914 // Move.
5915 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5916 processSlot(mapper, 0);
5917 processPosition(mapper, x1, y1);
5918 processSlot(mapper, 1);
5919 processPosition(mapper, x2, y2);
5920 processSync(mapper);
5921
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5923 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5924 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5925 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5927 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5930 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5932 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5933
5934 // First finger up.
5935 x2 += 15; y2 -= 20;
5936 processSlot(mapper, 0);
5937 processId(mapper, -1);
5938 processSlot(mapper, 1);
5939 processPosition(mapper, x2, y2);
5940 processSync(mapper);
5941
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5944 motionArgs.action);
5945 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5946 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5948 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5949 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5951 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5953 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5954
5955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5957 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5958 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5961 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5962
5963 // Move.
5964 x2 += 20; y2 -= 25;
5965 processPosition(mapper, x2, y2);
5966 processSync(mapper);
5967
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5970 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5971 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5972 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5974 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5975
5976 // New finger down.
5977 int32_t x3 = 700, y3 = 300;
5978 processPosition(mapper, x2, y2);
5979 processSlot(mapper, 0);
5980 processId(mapper, 3);
5981 processPosition(mapper, x3, y3);
5982 processSync(mapper);
5983
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5985 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5986 motionArgs.action);
5987 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5988 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5990 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5993 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5995 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5996
5997 // Second finger up.
5998 x3 += 30; y3 -= 20;
5999 processSlot(mapper, 1);
6000 processId(mapper, -1);
6001 processSlot(mapper, 0);
6002 processPosition(mapper, x3, y3);
6003 processSync(mapper);
6004
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6007 motionArgs.action);
6008 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6009 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6011 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6014 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6016 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6017
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6020 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6021 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6024 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6025
6026 // Last finger up.
6027 processId(mapper, -1);
6028 processSync(mapper);
6029
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6031 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6032 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6033 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6036 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6037
6038 // Should not have sent any more keys or motions.
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6041}
6042
6043TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 addConfigurationProperty("touch.deviceType", "touchScreen");
6045 prepareDisplay(DISPLAY_ORIENTATION_0);
6046 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006047 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048
6049 // These calculations are based on the input device calibration documentation.
6050 int32_t rawX = 100;
6051 int32_t rawY = 200;
6052 int32_t rawTouchMajor = 7;
6053 int32_t rawTouchMinor = 6;
6054 int32_t rawToolMajor = 9;
6055 int32_t rawToolMinor = 8;
6056 int32_t rawPressure = 11;
6057 int32_t rawDistance = 0;
6058 int32_t rawOrientation = 3;
6059 int32_t id = 5;
6060
6061 float x = toDisplayX(rawX);
6062 float y = toDisplayY(rawY);
6063 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6064 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6065 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6066 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6067 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6068 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6069 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6070 float distance = float(rawDistance);
6071
6072 processPosition(mapper, rawX, rawY);
6073 processTouchMajor(mapper, rawTouchMajor);
6074 processTouchMinor(mapper, rawTouchMinor);
6075 processToolMajor(mapper, rawToolMajor);
6076 processToolMinor(mapper, rawToolMinor);
6077 processPressure(mapper, rawPressure);
6078 processOrientation(mapper, rawOrientation);
6079 processDistance(mapper, rawDistance);
6080 processId(mapper, id);
6081 processMTSync(mapper);
6082 processSync(mapper);
6083
6084 NotifyMotionArgs args;
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6086 ASSERT_EQ(0, args.pointerProperties[0].id);
6087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6088 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6089 orientation, distance));
6090}
6091
6092TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006093 addConfigurationProperty("touch.deviceType", "touchScreen");
6094 prepareDisplay(DISPLAY_ORIENTATION_0);
6095 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6096 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006097 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098
6099 // These calculations are based on the input device calibration documentation.
6100 int32_t rawX = 100;
6101 int32_t rawY = 200;
6102 int32_t rawTouchMajor = 140;
6103 int32_t rawTouchMinor = 120;
6104 int32_t rawToolMajor = 180;
6105 int32_t rawToolMinor = 160;
6106
6107 float x = toDisplayX(rawX);
6108 float y = toDisplayY(rawY);
6109 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6110 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6111 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6112 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6113 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6114
6115 processPosition(mapper, rawX, rawY);
6116 processTouchMajor(mapper, rawTouchMajor);
6117 processTouchMinor(mapper, rawTouchMinor);
6118 processToolMajor(mapper, rawToolMajor);
6119 processToolMinor(mapper, rawToolMinor);
6120 processMTSync(mapper);
6121 processSync(mapper);
6122
6123 NotifyMotionArgs args;
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6126 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6127}
6128
6129TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130 addConfigurationProperty("touch.deviceType", "touchScreen");
6131 prepareDisplay(DISPLAY_ORIENTATION_0);
6132 prepareAxes(POSITION | TOUCH | TOOL);
6133 addConfigurationProperty("touch.size.calibration", "diameter");
6134 addConfigurationProperty("touch.size.scale", "10");
6135 addConfigurationProperty("touch.size.bias", "160");
6136 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006137 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138
6139 // These calculations are based on the input device calibration documentation.
6140 // Note: We only provide a single common touch/tool value because the device is assumed
6141 // not to emit separate values for each pointer (isSummed = 1).
6142 int32_t rawX = 100;
6143 int32_t rawY = 200;
6144 int32_t rawX2 = 150;
6145 int32_t rawY2 = 250;
6146 int32_t rawTouchMajor = 5;
6147 int32_t rawToolMajor = 8;
6148
6149 float x = toDisplayX(rawX);
6150 float y = toDisplayY(rawY);
6151 float x2 = toDisplayX(rawX2);
6152 float y2 = toDisplayY(rawY2);
6153 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6154 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6155 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6156
6157 processPosition(mapper, rawX, rawY);
6158 processTouchMajor(mapper, rawTouchMajor);
6159 processToolMajor(mapper, rawToolMajor);
6160 processMTSync(mapper);
6161 processPosition(mapper, rawX2, rawY2);
6162 processTouchMajor(mapper, rawTouchMajor);
6163 processToolMajor(mapper, rawToolMajor);
6164 processMTSync(mapper);
6165 processSync(mapper);
6166
6167 NotifyMotionArgs args;
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6170
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6173 args.action);
6174 ASSERT_EQ(size_t(2), args.pointerCount);
6175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6176 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6178 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6179}
6180
6181TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 addConfigurationProperty("touch.deviceType", "touchScreen");
6183 prepareDisplay(DISPLAY_ORIENTATION_0);
6184 prepareAxes(POSITION | TOUCH | TOOL);
6185 addConfigurationProperty("touch.size.calibration", "area");
6186 addConfigurationProperty("touch.size.scale", "43");
6187 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006188 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189
6190 // These calculations are based on the input device calibration documentation.
6191 int32_t rawX = 100;
6192 int32_t rawY = 200;
6193 int32_t rawTouchMajor = 5;
6194 int32_t rawToolMajor = 8;
6195
6196 float x = toDisplayX(rawX);
6197 float y = toDisplayY(rawY);
6198 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6199 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6200 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6201
6202 processPosition(mapper, rawX, rawY);
6203 processTouchMajor(mapper, rawTouchMajor);
6204 processToolMajor(mapper, rawToolMajor);
6205 processMTSync(mapper);
6206 processSync(mapper);
6207
6208 NotifyMotionArgs args;
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6211 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6212}
6213
6214TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215 addConfigurationProperty("touch.deviceType", "touchScreen");
6216 prepareDisplay(DISPLAY_ORIENTATION_0);
6217 prepareAxes(POSITION | PRESSURE);
6218 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6219 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006220 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221
Michael Wrightaa449c92017-12-13 21:21:43 +00006222 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006223 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006224 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6225 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6226 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6227
Michael Wrightd02c5b62014-02-10 15:10:22 -08006228 // These calculations are based on the input device calibration documentation.
6229 int32_t rawX = 100;
6230 int32_t rawY = 200;
6231 int32_t rawPressure = 60;
6232
6233 float x = toDisplayX(rawX);
6234 float y = toDisplayY(rawY);
6235 float pressure = float(rawPressure) * 0.01f;
6236
6237 processPosition(mapper, rawX, rawY);
6238 processPressure(mapper, rawPressure);
6239 processMTSync(mapper);
6240 processSync(mapper);
6241
6242 NotifyMotionArgs args;
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6245 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6246}
6247
6248TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249 addConfigurationProperty("touch.deviceType", "touchScreen");
6250 prepareDisplay(DISPLAY_ORIENTATION_0);
6251 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006252 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253
6254 NotifyMotionArgs motionArgs;
6255 NotifyKeyArgs keyArgs;
6256
6257 processId(mapper, 1);
6258 processPosition(mapper, 100, 200);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6262 ASSERT_EQ(0, motionArgs.buttonState);
6263
6264 // press BTN_LEFT, release BTN_LEFT
6265 processKey(mapper, BTN_LEFT, 1);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6269 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6270
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6273 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6274
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 processKey(mapper, BTN_LEFT, 0);
6276 processSync(mapper);
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006279 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006280
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006283 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284
6285 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6286 processKey(mapper, BTN_RIGHT, 1);
6287 processKey(mapper, BTN_MIDDLE, 1);
6288 processSync(mapper);
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6291 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6292 motionArgs.buttonState);
6293
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6296 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6297
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6300 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6301 motionArgs.buttonState);
6302
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 processKey(mapper, BTN_RIGHT, 0);
6304 processSync(mapper);
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006308
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006311 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312
6313 processKey(mapper, BTN_MIDDLE, 0);
6314 processSync(mapper);
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006317 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006318
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006321 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322
6323 // press BTN_BACK, release BTN_BACK
6324 processKey(mapper, BTN_BACK, 1);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6327 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6328 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006332 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6333
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6336 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337
6338 processKey(mapper, BTN_BACK, 0);
6339 processSync(mapper);
6340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006343
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346 ASSERT_EQ(0, motionArgs.buttonState);
6347
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6349 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6350 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6351
6352 // press BTN_SIDE, release BTN_SIDE
6353 processKey(mapper, BTN_SIDE, 1);
6354 processSync(mapper);
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6356 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6357 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006361 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6362
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6365 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366
6367 processKey(mapper, BTN_SIDE, 0);
6368 processSync(mapper);
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006372
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375 ASSERT_EQ(0, motionArgs.buttonState);
6376
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6378 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6379 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6380
6381 // press BTN_FORWARD, release BTN_FORWARD
6382 processKey(mapper, BTN_FORWARD, 1);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6386 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006390 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6391
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6394 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395
6396 processKey(mapper, BTN_FORWARD, 0);
6397 processSync(mapper);
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006401
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006404 ASSERT_EQ(0, motionArgs.buttonState);
6405
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6407 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6408 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6409
6410 // press BTN_EXTRA, release BTN_EXTRA
6411 processKey(mapper, BTN_EXTRA, 1);
6412 processSync(mapper);
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6414 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6415 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006416
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006419 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6420
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6423 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424
6425 processKey(mapper, BTN_EXTRA, 0);
6426 processSync(mapper);
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006430
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006433 ASSERT_EQ(0, motionArgs.buttonState);
6434
Michael Wrightd02c5b62014-02-10 15:10:22 -08006435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6436 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6437 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6438
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6440
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441 // press BTN_STYLUS, release BTN_STYLUS
6442 processKey(mapper, BTN_STYLUS, 1);
6443 processSync(mapper);
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006446 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6447
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6450 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006451
6452 processKey(mapper, BTN_STYLUS, 0);
6453 processSync(mapper);
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006457
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006460 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006461
6462 // press BTN_STYLUS2, release BTN_STYLUS2
6463 processKey(mapper, BTN_STYLUS2, 1);
6464 processSync(mapper);
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006467 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6468
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6471 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472
6473 processKey(mapper, BTN_STYLUS2, 0);
6474 processSync(mapper);
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006477 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006478
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006481 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482
6483 // release touch
6484 processId(mapper, -1);
6485 processSync(mapper);
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6488 ASSERT_EQ(0, motionArgs.buttonState);
6489}
6490
6491TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492 addConfigurationProperty("touch.deviceType", "touchScreen");
6493 prepareDisplay(DISPLAY_ORIENTATION_0);
6494 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006495 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496
6497 NotifyMotionArgs motionArgs;
6498
6499 // default tool type is finger
6500 processId(mapper, 1);
6501 processPosition(mapper, 100, 200);
6502 processSync(mapper);
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6504 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6506
6507 // eraser
6508 processKey(mapper, BTN_TOOL_RUBBER, 1);
6509 processSync(mapper);
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6513
6514 // stylus
6515 processKey(mapper, BTN_TOOL_RUBBER, 0);
6516 processKey(mapper, BTN_TOOL_PEN, 1);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6521
6522 // brush
6523 processKey(mapper, BTN_TOOL_PEN, 0);
6524 processKey(mapper, BTN_TOOL_BRUSH, 1);
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6529
6530 // pencil
6531 processKey(mapper, BTN_TOOL_BRUSH, 0);
6532 processKey(mapper, BTN_TOOL_PENCIL, 1);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6537
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006538 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539 processKey(mapper, BTN_TOOL_PENCIL, 0);
6540 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6541 processSync(mapper);
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6545
6546 // mouse
6547 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6548 processKey(mapper, BTN_TOOL_MOUSE, 1);
6549 processSync(mapper);
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6553
6554 // lens
6555 processKey(mapper, BTN_TOOL_MOUSE, 0);
6556 processKey(mapper, BTN_TOOL_LENS, 1);
6557 processSync(mapper);
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6561
6562 // double-tap
6563 processKey(mapper, BTN_TOOL_LENS, 0);
6564 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6565 processSync(mapper);
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6569
6570 // triple-tap
6571 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6572 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6573 processSync(mapper);
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6577
6578 // quad-tap
6579 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6580 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6581 processSync(mapper);
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6585
6586 // finger
6587 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6588 processKey(mapper, BTN_TOOL_FINGER, 1);
6589 processSync(mapper);
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6593
6594 // stylus trumps finger
6595 processKey(mapper, BTN_TOOL_PEN, 1);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6600
6601 // eraser trumps stylus
6602 processKey(mapper, BTN_TOOL_RUBBER, 1);
6603 processSync(mapper);
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6607
6608 // mouse trumps eraser
6609 processKey(mapper, BTN_TOOL_MOUSE, 1);
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6614
6615 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6616 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6617 processSync(mapper);
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6620 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6621
6622 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6623 processToolType(mapper, MT_TOOL_PEN);
6624 processSync(mapper);
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6628
6629 // back to default tool type
6630 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6631 processKey(mapper, BTN_TOOL_MOUSE, 0);
6632 processKey(mapper, BTN_TOOL_RUBBER, 0);
6633 processKey(mapper, BTN_TOOL_PEN, 0);
6634 processKey(mapper, BTN_TOOL_FINGER, 0);
6635 processSync(mapper);
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6639}
6640
6641TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006642 addConfigurationProperty("touch.deviceType", "touchScreen");
6643 prepareDisplay(DISPLAY_ORIENTATION_0);
6644 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006645 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006646 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006647
6648 NotifyMotionArgs motionArgs;
6649
6650 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6651 processId(mapper, 1);
6652 processPosition(mapper, 100, 200);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6657 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6658
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6662 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6663
6664 // move a little
6665 processPosition(mapper, 150, 250);
6666 processSync(mapper);
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6670 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6671
6672 // down when BTN_TOUCH is pressed, pressure defaults to 1
6673 processKey(mapper, BTN_TOUCH, 1);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6678 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6679
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6683 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6684
6685 // up when BTN_TOUCH is released, hover restored
6686 processKey(mapper, BTN_TOUCH, 0);
6687 processSync(mapper);
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6689 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6691 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6692
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6697
6698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6701 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6702
6703 // exit hover when pointer goes away
6704 processId(mapper, -1);
6705 processSync(mapper);
6706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6707 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6709 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6710}
6711
6712TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006713 addConfigurationProperty("touch.deviceType", "touchScreen");
6714 prepareDisplay(DISPLAY_ORIENTATION_0);
6715 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006716 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006717
6718 NotifyMotionArgs motionArgs;
6719
6720 // initially hovering because pressure is 0
6721 processId(mapper, 1);
6722 processPosition(mapper, 100, 200);
6723 processPressure(mapper, 0);
6724 processSync(mapper);
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6726 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6728 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6729
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6733 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6734
6735 // move a little
6736 processPosition(mapper, 150, 250);
6737 processSync(mapper);
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6741 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6742
6743 // down when pressure becomes non-zero
6744 processPressure(mapper, RAW_PRESSURE_MAX);
6745 processSync(mapper);
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6747 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6749 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6750
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6752 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6754 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6755
6756 // up when pressure becomes 0, hover restored
6757 processPressure(mapper, 0);
6758 processSync(mapper);
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6760 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6762 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6763
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6765 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6767 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6768
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6772 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6773
6774 // exit hover when pointer goes away
6775 processId(mapper, -1);
6776 processSync(mapper);
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6778 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6780 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6781}
6782
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006783/**
6784 * Set the input device port <--> display port associations, and check that the
6785 * events are routed to the display that matches the display port.
6786 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6787 */
6788TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006789 const std::string usb2 = "USB2";
6790 const uint8_t hdmi1 = 0;
6791 const uint8_t hdmi2 = 1;
6792 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006793 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006794
6795 addConfigurationProperty("touch.deviceType", "touchScreen");
6796 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006797 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006798
6799 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6800 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6801
6802 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6803 // for this input device is specified, and the matching viewport is not present,
6804 // the input device should be disabled (at the mapper level).
6805
6806 // Add viewport for display 2 on hdmi2
6807 prepareSecondaryDisplay(type, hdmi2);
6808 // Send a touch event
6809 processPosition(mapper, 100, 100);
6810 processSync(mapper);
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6812
6813 // Add viewport for display 1 on hdmi1
6814 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6815 // Send a touch event again
6816 processPosition(mapper, 100, 100);
6817 processSync(mapper);
6818
6819 NotifyMotionArgs args;
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6821 ASSERT_EQ(DISPLAY_ID, args.displayId);
6822}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006823
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006824TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006825 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006826 std::shared_ptr<FakePointerController> fakePointerController =
6827 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006828 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006829 fakePointerController->setPosition(100, 200);
6830 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006831 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6832
Garfield Tan888a6a42020-01-09 11:39:16 -08006833 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006834 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006835
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006836 prepareDisplay(DISPLAY_ORIENTATION_0);
6837 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006838 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006839
6840 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006841 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006842
6843 NotifyMotionArgs motionArgs;
6844 processPosition(mapper, 100, 100);
6845 processSync(mapper);
6846
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6848 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6849 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6850}
6851
Arthur Hung7c645402019-01-25 17:45:42 +08006852TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6853 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006854 prepareAxes(POSITION | ID | SLOT);
6855 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006856 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006857
6858 // Create the second touch screen device, and enable multi fingers.
6859 const std::string USB2 = "USB2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006860 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006861 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
Arthur Hung7c645402019-01-25 17:45:42 +08006862 InputDeviceIdentifier identifier;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006863 identifier.name = "TOUCHSCREEN2";
Arthur Hung7c645402019-01-25 17:45:42 +08006864 identifier.location = USB2;
Arthur Hung2c9a3342019-07-23 14:18:59 +08006865 std::unique_ptr<InputDevice> device2 =
6866 std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006867 identifier);
6868 mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6869 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6870 0 /*flat*/, 0 /*fuzz*/);
6871 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6872 0 /*flat*/, 0 /*fuzz*/);
6873 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6874 0 /*flat*/, 0 /*fuzz*/);
6875 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6876 0 /*flat*/, 0 /*fuzz*/);
6877 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6878 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6879 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006880
6881 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006882 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006883 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6884 device2->reset(ARBITRARY_TIME);
6885
6886 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006887 std::shared_ptr<FakePointerController> fakePointerController =
6888 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006889 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6890 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6891
6892 // Setup policy for associated displays and show touches.
6893 const uint8_t hdmi1 = 0;
6894 const uint8_t hdmi2 = 1;
6895 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6896 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6897 mFakePolicy->setShowTouches(true);
6898
6899 // Create displays.
6900 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006901 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006902
6903 // Default device will reconfigure above, need additional reconfiguration for another device.
6904 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006905 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08006906
6907 // Two fingers down at default display.
6908 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6909 processPosition(mapper, x1, y1);
6910 processId(mapper, 1);
6911 processSlot(mapper, 1);
6912 processPosition(mapper, x2, y2);
6913 processId(mapper, 2);
6914 processSync(mapper);
6915
6916 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6917 fakePointerController->getSpots().find(DISPLAY_ID);
6918 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6919 ASSERT_EQ(size_t(2), iter->second.size());
6920
6921 // Two fingers down at second display.
6922 processPosition(mapper2, x1, y1);
6923 processId(mapper2, 1);
6924 processSlot(mapper2, 1);
6925 processPosition(mapper2, x2, y2);
6926 processId(mapper2, 2);
6927 processSync(mapper2);
6928
6929 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6930 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6931 ASSERT_EQ(size_t(2), iter->second.size());
6932}
6933
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006934TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006935 prepareAxes(POSITION);
6936 addConfigurationProperty("touch.deviceType", "touchScreen");
6937 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006938 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006939
6940 NotifyMotionArgs motionArgs;
6941 // Unrotated video frame
6942 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6943 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006944 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006945 processPosition(mapper, 100, 200);
6946 processSync(mapper);
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6948 ASSERT_EQ(frames, motionArgs.videoFrames);
6949
6950 // Subsequent touch events should not have any videoframes
6951 // This is implemented separately in FakeEventHub,
6952 // but that should match the behaviour of TouchVideoDevice.
6953 processPosition(mapper, 200, 200);
6954 processSync(mapper);
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6956 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6957}
6958
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006959TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006960 prepareAxes(POSITION);
6961 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006962 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006963 // Unrotated video frame
6964 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6965 NotifyMotionArgs motionArgs;
6966
6967 // Test all 4 orientations
6968 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6969 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6970 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6971 clearViewports();
6972 prepareDisplay(orientation);
6973 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006974 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006975 processPosition(mapper, 100, 200);
6976 processSync(mapper);
6977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6978 frames[0].rotate(orientation);
6979 ASSERT_EQ(frames, motionArgs.videoFrames);
6980 }
6981}
6982
6983TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006984 prepareAxes(POSITION);
6985 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006986 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006987 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6988 // so mix these.
6989 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6990 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6991 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6992 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6993 NotifyMotionArgs motionArgs;
6994
6995 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006996 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006997 processPosition(mapper, 100, 200);
6998 processSync(mapper);
6999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7000 std::for_each(frames.begin(), frames.end(),
7001 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7002 ASSERT_EQ(frames, motionArgs.videoFrames);
7003}
7004
Arthur Hung9da14732019-09-02 16:16:58 +08007005/**
7006 * If we had defined port associations, but the viewport is not ready, the touch device would be
7007 * expected to be disabled, and it should be enabled after the viewport has found.
7008 */
7009TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007010 constexpr uint8_t hdmi2 = 1;
7011 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007012 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007013
7014 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7015
7016 addConfigurationProperty("touch.deviceType", "touchScreen");
7017 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007018 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007019
7020 ASSERT_EQ(mDevice->isEnabled(), false);
7021
7022 // Add display on hdmi2, the device should be enabled and can receive touch event.
7023 prepareSecondaryDisplay(type, hdmi2);
7024 ASSERT_EQ(mDevice->isEnabled(), true);
7025
7026 // Send a touch event.
7027 processPosition(mapper, 100, 100);
7028 processSync(mapper);
7029
7030 NotifyMotionArgs args;
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7032 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7033}
7034
Arthur Hung6cd19a42019-08-30 19:04:12 +08007035
Arthur Hung6cd19a42019-08-30 19:04:12 +08007036
Arthur Hung421eb1c2020-01-16 00:09:42 +08007037TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007038 addConfigurationProperty("touch.deviceType", "touchScreen");
7039 prepareDisplay(DISPLAY_ORIENTATION_0);
7040 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007041 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007042
7043 NotifyMotionArgs motionArgs;
7044
7045 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7046 // finger down
7047 processId(mapper, 1);
7048 processPosition(mapper, x1, y1);
7049 processSync(mapper);
7050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7051 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7053
7054 // finger move
7055 processId(mapper, 1);
7056 processPosition(mapper, x2, y2);
7057 processSync(mapper);
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7061
7062 // finger up.
7063 processId(mapper, -1);
7064 processSync(mapper);
7065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7066 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7068
7069 // new finger down
7070 processId(mapper, 1);
7071 processPosition(mapper, x3, y3);
7072 processSync(mapper);
7073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7074 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7076}
7077
7078/**
arthurhungcc7f9802020-04-30 17:55:40 +08007079 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7080 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007081 */
arthurhungcc7f9802020-04-30 17:55:40 +08007082TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007083 addConfigurationProperty("touch.deviceType", "touchScreen");
7084 prepareDisplay(DISPLAY_ORIENTATION_0);
7085 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007086 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007087
7088 NotifyMotionArgs motionArgs;
7089
7090 // default tool type is finger
7091 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007092 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007093 processPosition(mapper, x1, y1);
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7098
7099 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7100 processToolType(mapper, MT_TOOL_PALM);
7101 processSync(mapper);
7102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7103 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7104
7105 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007106 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007107 processPosition(mapper, x2, y2);
7108 processSync(mapper);
7109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7110
7111 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007112 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007113 processSync(mapper);
7114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7115
7116 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007117 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007118 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007119 processPosition(mapper, x3, y3);
7120 processSync(mapper);
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7122 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7124}
7125
arthurhungbf89a482020-04-17 17:37:55 +08007126/**
arthurhungcc7f9802020-04-30 17:55:40 +08007127 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7128 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007129 */
arthurhungcc7f9802020-04-30 17:55:40 +08007130TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007131 addConfigurationProperty("touch.deviceType", "touchScreen");
7132 prepareDisplay(DISPLAY_ORIENTATION_0);
7133 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7134 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7135
7136 NotifyMotionArgs motionArgs;
7137
7138 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007139 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7140 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007141 processPosition(mapper, x1, y1);
7142 processSync(mapper);
7143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7144 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7146
7147 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007148 processSlot(mapper, SECOND_SLOT);
7149 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007150 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007151 processSync(mapper);
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7153 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7154 motionArgs.action);
7155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7156
7157 // If the tool type of the first finger changes to MT_TOOL_PALM,
7158 // we expect to receive ACTION_POINTER_UP with cancel flag.
7159 processSlot(mapper, FIRST_SLOT);
7160 processId(mapper, FIRST_TRACKING_ID);
7161 processToolType(mapper, MT_TOOL_PALM);
7162 processSync(mapper);
7163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7164 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7165 motionArgs.action);
7166 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7167
7168 // The following MOVE events of second finger should be processed.
7169 processSlot(mapper, SECOND_SLOT);
7170 processId(mapper, SECOND_TRACKING_ID);
7171 processPosition(mapper, x2 + 1, y2 + 1);
7172 processSync(mapper);
7173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7175 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7176
7177 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7178 // it. Second finger receive move.
7179 processSlot(mapper, FIRST_SLOT);
7180 processId(mapper, INVALID_TRACKING_ID);
7181 processSync(mapper);
7182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7184 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7185
7186 // Second finger keeps moving.
7187 processSlot(mapper, SECOND_SLOT);
7188 processId(mapper, SECOND_TRACKING_ID);
7189 processPosition(mapper, x2 + 2, y2 + 2);
7190 processSync(mapper);
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7193 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7194
7195 // Second finger up.
7196 processId(mapper, INVALID_TRACKING_ID);
7197 processSync(mapper);
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7199 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7200 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7201}
7202
7203/**
7204 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7205 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7206 */
7207TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7208 addConfigurationProperty("touch.deviceType", "touchScreen");
7209 prepareDisplay(DISPLAY_ORIENTATION_0);
7210 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7211 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7212
7213 NotifyMotionArgs motionArgs;
7214
7215 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7216 // First finger down.
7217 processId(mapper, FIRST_TRACKING_ID);
7218 processPosition(mapper, x1, y1);
7219 processSync(mapper);
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7221 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7223
7224 // Second finger down.
7225 processSlot(mapper, SECOND_SLOT);
7226 processId(mapper, SECOND_TRACKING_ID);
7227 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007228 processSync(mapper);
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7231 motionArgs.action);
7232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7233
arthurhungcc7f9802020-04-30 17:55:40 +08007234 // If the tool type of the first finger changes to MT_TOOL_PALM,
7235 // we expect to receive ACTION_POINTER_UP with cancel flag.
7236 processSlot(mapper, FIRST_SLOT);
7237 processId(mapper, FIRST_TRACKING_ID);
7238 processToolType(mapper, MT_TOOL_PALM);
7239 processSync(mapper);
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7241 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7242 motionArgs.action);
7243 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7244
7245 // Second finger keeps moving.
7246 processSlot(mapper, SECOND_SLOT);
7247 processId(mapper, SECOND_TRACKING_ID);
7248 processPosition(mapper, x2 + 1, y2 + 1);
7249 processSync(mapper);
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7252
7253 // second finger becomes palm, receive cancel due to only 1 finger is active.
7254 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007255 processToolType(mapper, MT_TOOL_PALM);
7256 processSync(mapper);
7257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7258 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7259
arthurhungcc7f9802020-04-30 17:55:40 +08007260 // third finger down.
7261 processSlot(mapper, THIRD_SLOT);
7262 processId(mapper, THIRD_TRACKING_ID);
7263 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007264 processPosition(mapper, x3, y3);
7265 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7267 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007269 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7270
7271 // third finger move
7272 processId(mapper, THIRD_TRACKING_ID);
7273 processPosition(mapper, x3 + 1, y3 + 1);
7274 processSync(mapper);
7275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7277
7278 // first finger up, third finger receive move.
7279 processSlot(mapper, FIRST_SLOT);
7280 processId(mapper, INVALID_TRACKING_ID);
7281 processSync(mapper);
7282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7284 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7285
7286 // second finger up, third finger receive move.
7287 processSlot(mapper, SECOND_SLOT);
7288 processId(mapper, INVALID_TRACKING_ID);
7289 processSync(mapper);
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7292 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7293
7294 // third finger up.
7295 processSlot(mapper, THIRD_SLOT);
7296 processId(mapper, INVALID_TRACKING_ID);
7297 processSync(mapper);
7298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7299 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7300 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7301}
7302
7303/**
7304 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7305 * and the active finger could still be allowed to receive the events
7306 */
7307TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7308 addConfigurationProperty("touch.deviceType", "touchScreen");
7309 prepareDisplay(DISPLAY_ORIENTATION_0);
7310 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7311 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7312
7313 NotifyMotionArgs motionArgs;
7314
7315 // default tool type is finger
7316 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7317 processId(mapper, FIRST_TRACKING_ID);
7318 processPosition(mapper, x1, y1);
7319 processSync(mapper);
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7321 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7323
7324 // Second finger down.
7325 processSlot(mapper, SECOND_SLOT);
7326 processId(mapper, SECOND_TRACKING_ID);
7327 processPosition(mapper, x2, y2);
7328 processSync(mapper);
7329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7330 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7331 motionArgs.action);
7332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7333
7334 // If the tool type of the second finger changes to MT_TOOL_PALM,
7335 // we expect to receive ACTION_POINTER_UP with cancel flag.
7336 processId(mapper, SECOND_TRACKING_ID);
7337 processToolType(mapper, MT_TOOL_PALM);
7338 processSync(mapper);
7339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7340 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7341 motionArgs.action);
7342 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7343
7344 // The following MOVE event should be processed.
7345 processSlot(mapper, FIRST_SLOT);
7346 processId(mapper, FIRST_TRACKING_ID);
7347 processPosition(mapper, x1 + 1, y1 + 1);
7348 processSync(mapper);
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7351 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7352
7353 // second finger up.
7354 processSlot(mapper, SECOND_SLOT);
7355 processId(mapper, INVALID_TRACKING_ID);
7356 processSync(mapper);
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7359
7360 // first finger keep moving
7361 processSlot(mapper, FIRST_SLOT);
7362 processId(mapper, FIRST_TRACKING_ID);
7363 processPosition(mapper, x1 + 2, y1 + 2);
7364 processSync(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7367
7368 // first finger up.
7369 processId(mapper, INVALID_TRACKING_ID);
7370 processSync(mapper);
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7372 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7373 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007374}
7375
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007376// --- MultiTouchInputMapperTest_ExternalDevice ---
7377
7378class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7379protected:
7380 virtual void SetUp() override {
7381 InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
7382 }
7383};
7384
7385/**
7386 * Expect fallback to internal viewport if device is external and external viewport is not present.
7387 */
7388TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7389 prepareAxes(POSITION);
7390 addConfigurationProperty("touch.deviceType", "touchScreen");
7391 prepareDisplay(DISPLAY_ORIENTATION_0);
7392 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7393
7394 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7395
7396 NotifyMotionArgs motionArgs;
7397
7398 // Expect the event to be sent to the internal viewport,
7399 // because an external viewport is not present.
7400 processPosition(mapper, 100, 100);
7401 processSync(mapper);
7402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7403 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7404
7405 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007406 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007407 processPosition(mapper, 100, 100);
7408 processSync(mapper);
7409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7410 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7411}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007412
7413/**
7414 * Test touch should not work if outside of surface.
7415 */
7416class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7417protected:
7418 void halfDisplayToCenterHorizontal(int32_t orientation) {
7419 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007420 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007421
7422 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7423 internalViewport->orientation = orientation;
7424 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7425 internalViewport->logicalLeft = 0;
7426 internalViewport->logicalTop = 0;
7427 internalViewport->logicalRight = DISPLAY_HEIGHT;
7428 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7429
7430 internalViewport->physicalLeft = 0;
7431 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7432 internalViewport->physicalRight = DISPLAY_HEIGHT;
7433 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7434
7435 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7436 internalViewport->deviceHeight = DISPLAY_WIDTH;
7437 } else {
7438 internalViewport->logicalLeft = 0;
7439 internalViewport->logicalTop = 0;
7440 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7441 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7442
7443 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7444 internalViewport->physicalTop = 0;
7445 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7446 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7447
7448 internalViewport->deviceWidth = DISPLAY_WIDTH;
7449 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7450 }
7451
7452 mFakePolicy->updateViewport(internalViewport.value());
7453 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7454 }
7455
7456 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7457 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7458 int32_t yExpected) {
7459 // touch on outside area should not work.
7460 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7461 processSync(mapper);
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7463
7464 // touch on inside area should receive the event.
7465 NotifyMotionArgs args;
7466 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7467 processSync(mapper);
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7469 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7470 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7471
7472 // Reset.
7473 mapper.reset(ARBITRARY_TIME);
7474 }
7475};
7476
7477TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7478 addConfigurationProperty("touch.deviceType", "touchScreen");
7479 prepareDisplay(DISPLAY_ORIENTATION_0);
7480 prepareAxes(POSITION);
7481 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7482
7483 // Touch on center of normal display should work.
7484 const int32_t x = DISPLAY_WIDTH / 4;
7485 const int32_t y = DISPLAY_HEIGHT / 2;
7486 processPosition(mapper, toRawX(x), toRawY(y));
7487 processSync(mapper);
7488 NotifyMotionArgs args;
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7491 0.0f, 0.0f, 0.0f, 0.0f));
7492 // Reset.
7493 mapper.reset(ARBITRARY_TIME);
7494
7495 // Let physical display be different to device, and make surface and physical could be 1:1.
7496 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7497
7498 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7499 const int32_t yExpected = y;
7500 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7501}
7502
7503TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7504 addConfigurationProperty("touch.deviceType", "touchScreen");
7505 prepareDisplay(DISPLAY_ORIENTATION_0);
7506 prepareAxes(POSITION);
7507 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7508
7509 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7510 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7511
7512 const int32_t x = DISPLAY_WIDTH / 4;
7513 const int32_t y = DISPLAY_HEIGHT / 2;
7514
7515 // expect x/y = swap x/y then reverse y.
7516 const int32_t xExpected = y;
7517 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7518 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7519}
7520
7521TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7522 addConfigurationProperty("touch.deviceType", "touchScreen");
7523 prepareDisplay(DISPLAY_ORIENTATION_0);
7524 prepareAxes(POSITION);
7525 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7526
7527 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7528 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7529
7530 const int32_t x = DISPLAY_WIDTH / 4;
7531 const int32_t y = DISPLAY_HEIGHT / 2;
7532
7533 // expect x/y = swap x/y then reverse x.
7534 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7535 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7536 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7537}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007538
7539TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7540 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7541 std::shared_ptr<FakePointerController> fakePointerController =
7542 std::make_shared<FakePointerController>();
7543 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7544 fakePointerController->setPosition(0, 0);
7545 fakePointerController->setButtonState(0);
7546
7547 // prepare device and capture
7548 prepareDisplay(DISPLAY_ORIENTATION_0);
7549 prepareAxes(POSITION | ID | SLOT);
7550 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7551 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7552 mFakePolicy->setPointerCapture(true);
7553 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7554 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7555
7556 // captured touchpad should be a touchpad source
7557 NotifyDeviceResetArgs resetArgs;
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7559 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7560
7561 // run captured pointer tests - note that this is unscaled, so input listener events should be
7562 // identical to what the hardware sends (accounting for any
7563 // calibration).
7564 // FINGER 0 DOWN
7565 processId(mapper, 1);
7566 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7567 processKey(mapper, BTN_TOUCH, 1);
7568 processSync(mapper);
7569
7570 // expect coord[0] to contain initial location of touch 0
7571 NotifyMotionArgs args;
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7573 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7574 ASSERT_EQ(1U, args.pointerCount);
7575 ASSERT_EQ(0, args.pointerProperties[0].id);
7576 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7577 ASSERT_NO_FATAL_FAILURE(
7578 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7579
7580 // FINGER 1 DOWN
7581 processSlot(mapper, 1);
7582 processId(mapper, 2);
7583 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7584 processSync(mapper);
7585
7586 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7588 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | 0x0100, args.action);
7589 ASSERT_EQ(2U, args.pointerCount);
7590 ASSERT_EQ(0, args.pointerProperties[0].id);
7591 ASSERT_EQ(1, args.pointerProperties[1].id);
7592 ASSERT_NO_FATAL_FAILURE(
7593 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7594 ASSERT_NO_FATAL_FAILURE(
7595 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7596
7597 // FINGER 1 MOVE
7598 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7599 processSync(mapper);
7600
7601 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7602 // from move
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7605 ASSERT_NO_FATAL_FAILURE(
7606 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7607 ASSERT_NO_FATAL_FAILURE(
7608 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7609
7610 // FINGER 0 MOVE
7611 processSlot(mapper, 0);
7612 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7613 processSync(mapper);
7614
7615 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7618 ASSERT_NO_FATAL_FAILURE(
7619 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7620 ASSERT_NO_FATAL_FAILURE(
7621 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7622
7623 // BUTTON DOWN
7624 processKey(mapper, BTN_LEFT, 1);
7625 processSync(mapper);
7626
7627 // touchinputmapper design sends a move before button press
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7631 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7632
7633 // BUTTON UP
7634 processKey(mapper, BTN_LEFT, 0);
7635 processSync(mapper);
7636
7637 // touchinputmapper design sends a move after button release
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7642
7643 // FINGER 0 UP
7644 processId(mapper, -1);
7645 processSync(mapper);
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7647 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7648
7649 // FINGER 1 MOVE
7650 processSlot(mapper, 1);
7651 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7652 processSync(mapper);
7653
7654 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7657 ASSERT_EQ(1U, args.pointerCount);
7658 ASSERT_EQ(1, args.pointerProperties[0].id);
7659 ASSERT_NO_FATAL_FAILURE(
7660 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7661
7662 // FINGER 1 UP
7663 processId(mapper, -1);
7664 processKey(mapper, BTN_TOUCH, 0);
7665 processSync(mapper);
7666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7667 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7668
7669 // non captured touchpad should be a mouse source
7670 mFakePolicy->setPointerCapture(false);
7671 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7673 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7674}
7675
7676TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7677 std::shared_ptr<FakePointerController> fakePointerController =
7678 std::make_shared<FakePointerController>();
7679 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7680 fakePointerController->setPosition(0, 0);
7681 fakePointerController->setButtonState(0);
7682
7683 // prepare device and capture
7684 prepareDisplay(DISPLAY_ORIENTATION_0);
7685 prepareAxes(POSITION | ID | SLOT);
7686 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7687 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7688 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7689 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7690 // run uncaptured pointer tests - pushes out generic events
7691 // FINGER 0 DOWN
7692 processId(mapper, 3);
7693 processPosition(mapper, 100, 100);
7694 processKey(mapper, BTN_TOUCH, 1);
7695 processSync(mapper);
7696
7697 // start at (100,100), cursor should be at (0,0) * scale
7698 NotifyMotionArgs args;
7699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7701 ASSERT_NO_FATAL_FAILURE(
7702 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7703
7704 // FINGER 0 MOVE
7705 processPosition(mapper, 200, 200);
7706 processSync(mapper);
7707
7708 // compute scaling to help with touch position checking
7709 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7710 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7711 float scale =
7712 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7713
7714 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7718 0, 0, 0, 0, 0, 0, 0));
7719}
7720
7721TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7722 std::shared_ptr<FakePointerController> fakePointerController =
7723 std::make_shared<FakePointerController>();
7724
7725 prepareDisplay(DISPLAY_ORIENTATION_0);
7726 prepareAxes(POSITION | ID | SLOT);
7727 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7728 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7729 mFakePolicy->setPointerCapture(false);
7730 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7731
7732 // uncaptured touchpad should be a pointer device
7733 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7734
7735 // captured touchpad should be a touchpad device
7736 mFakePolicy->setPointerCapture(true);
7737 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7738 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7739}
7740
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741} // namespace android