blob: b28b6ea4e208a0328aeae4aaa069fcb086aa07fc [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;
Chris Ye1b0c7342020-07-28 21:57:03 -070040using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070041
42// Timeout for waiting for an expected event
43static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
44
Michael Wrightd02c5b62014-02-10 15:10:22 -080045// An arbitrary time value.
46static const nsecs_t ARBITRARY_TIME = 1234;
47
48// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080049static constexpr int32_t DISPLAY_ID = 0;
50static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
51static constexpr int32_t DISPLAY_WIDTH = 480;
52static constexpr int32_t DISPLAY_HEIGHT = 800;
53static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
54static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
55static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070056static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070057static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080058
arthurhungcc7f9802020-04-30 17:55:40 +080059static constexpr int32_t FIRST_SLOT = 0;
60static constexpr int32_t SECOND_SLOT = 1;
61static constexpr int32_t THIRD_SLOT = 2;
62static constexpr int32_t INVALID_TRACKING_ID = -1;
63static constexpr int32_t FIRST_TRACKING_ID = 0;
64static constexpr int32_t SECOND_TRACKING_ID = 1;
65static constexpr int32_t THIRD_TRACKING_ID = 2;
66
Michael Wrightd02c5b62014-02-10 15:10:22 -080067// Error tolerance for floating point assertions.
68static const float EPSILON = 0.001f;
69
70template<typename T>
71static inline T min(T a, T b) {
72 return a < b ? a : b;
73}
74
75static inline float avg(float x, float y) {
76 return (x + y) / 2;
77}
78
79
80// --- FakePointerController ---
81
82class FakePointerController : public PointerControllerInterface {
83 bool mHaveBounds;
84 float mMinX, mMinY, mMaxX, mMaxY;
85 float mX, mY;
86 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080087 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080088
Michael Wrightd02c5b62014-02-10 15:10:22 -080089public:
90 FakePointerController() :
91 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080092 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080093 }
94
Michael Wright17db18e2020-06-26 20:51:44 +010095 virtual ~FakePointerController() {}
96
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 void setBounds(float minX, float minY, float maxX, float maxY) {
98 mHaveBounds = true;
99 mMinX = minX;
100 mMinY = minY;
101 mMaxX = maxX;
102 mMaxY = maxY;
103 }
104
105 virtual void setPosition(float x, float y) {
106 mX = x;
107 mY = y;
108 }
109
110 virtual void setButtonState(int32_t buttonState) {
111 mButtonState = buttonState;
112 }
113
114 virtual int32_t getButtonState() const {
115 return mButtonState;
116 }
117
118 virtual void getPosition(float* outX, float* outY) const {
119 *outX = mX;
120 *outY = mY;
121 }
122
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800123 virtual int32_t getDisplayId() const {
124 return mDisplayId;
125 }
126
Garfield Tan888a6a42020-01-09 11:39:16 -0800127 virtual void setDisplayViewport(const DisplayViewport& viewport) {
128 mDisplayId = viewport.displayId;
129 }
130
Arthur Hung7c645402019-01-25 17:45:42 +0800131 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
132 return mSpotsByDisplay;
133 }
134
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135private:
136 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
137 *outMinX = mMinX;
138 *outMinY = mMinY;
139 *outMaxX = mMaxX;
140 *outMaxY = mMaxY;
141 return mHaveBounds;
142 }
143
144 virtual void move(float deltaX, float deltaY) {
145 mX += deltaX;
146 if (mX < mMinX) mX = mMinX;
147 if (mX > mMaxX) mX = mMaxX;
148 mY += deltaY;
149 if (mY < mMinY) mY = mMinY;
150 if (mY > mMaxY) mY = mMaxY;
151 }
152
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100153 virtual void fade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 }
155
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100156 virtual void unfade(Transition) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157 }
158
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100159 virtual void setPresentation(Presentation) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160 }
161
Arthur Hung7c645402019-01-25 17:45:42 +0800162 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
163 int32_t displayId) {
164 std::vector<int32_t> newSpots;
165 // Add spots for fingers that are down.
166 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
167 uint32_t id = idBits.clearFirstMarkedBit();
168 newSpots.push_back(id);
169 }
170
171 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 }
173
174 virtual void clearSpots() {
175 }
Arthur Hung7c645402019-01-25 17:45:42 +0800176
177 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178};
179
180
181// --- FakeInputReaderPolicy ---
182
183class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700184 std::mutex mLock;
185 std::condition_variable mDevicesChangedCondition;
186
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100188 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700189 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
190 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100191 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700192 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193
194protected:
195 virtual ~FakeInputReaderPolicy() { }
196
197public:
198 FakeInputReaderPolicy() {
199 }
200
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700201 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800202 waitForInputDevices([](bool devicesChanged) {
203 if (!devicesChanged) {
204 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
205 }
206 });
207 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800209 void assertInputDevicesNotChanged() {
210 waitForInputDevices([](bool devicesChanged) {
211 if (devicesChanged) {
212 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
213 }
214 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700215 }
216
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700217 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100218 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100219 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700220 }
221
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700222 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
223 return mConfig.getDisplayViewportByUniqueId(uniqueId);
224 }
225 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
226 return mConfig.getDisplayViewportByType(type);
227 }
228
229 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
230 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700231 }
232
233 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700234 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
235 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700236 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700237 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700238 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100239 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240 }
241
Arthur Hung6cd19a42019-08-30 19:04:12 +0800242 bool updateViewport(const DisplayViewport& viewport) {
243 size_t count = mViewports.size();
244 for (size_t i = 0; i < count; i++) {
245 const DisplayViewport& currentViewport = mViewports[i];
246 if (currentViewport.displayId == viewport.displayId) {
247 mViewports[i] = viewport;
248 mConfig.setDisplayViewports(mViewports);
249 return true;
250 }
251 }
252 // no viewport found.
253 return false;
254 }
255
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100256 void addExcludedDeviceName(const std::string& deviceName) {
257 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258 }
259
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700260 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
261 mConfig.portAssociations.insert({inputPort, displayPort});
262 }
263
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000264 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700265
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000266 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700267
Michael Wright17db18e2020-06-26 20:51:44 +0100268 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
269 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 }
271
272 const InputReaderConfiguration* getReaderConfiguration() const {
273 return &mConfig;
274 }
275
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800276 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277 return mInputDevices;
278 }
279
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100280 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700281 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700282 return transform;
283 }
284
285 void setTouchAffineTransformation(const TouchAffineTransformation t) {
286 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800287 }
288
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800289 void setPointerCapture(bool enabled) {
290 mConfig.pointerCapture = enabled;
291 }
292
Arthur Hung7c645402019-01-25 17:45:42 +0800293 void setShowTouches(bool enabled) {
294 mConfig.showTouches = enabled;
295 }
296
Garfield Tan888a6a42020-01-09 11:39:16 -0800297 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
298 mConfig.defaultPointerDisplayId = pointerDisplayId;
299 }
300
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800301 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
302
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700304 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700305 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
306 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700307 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
308 || orientation == DISPLAY_ORIENTATION_270);
309 DisplayViewport v;
310 v.displayId = displayId;
311 v.orientation = orientation;
312 v.logicalLeft = 0;
313 v.logicalTop = 0;
314 v.logicalRight = isRotated ? height : width;
315 v.logicalBottom = isRotated ? width : height;
316 v.physicalLeft = 0;
317 v.physicalTop = 0;
318 v.physicalRight = isRotated ? height : width;
319 v.physicalBottom = isRotated ? width : height;
320 v.deviceWidth = isRotated ? height : width;
321 v.deviceHeight = isRotated ? width : height;
322 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700323 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100324 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700325 return v;
326 }
327
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
329 *outConfig = mConfig;
330 }
331
Michael Wright17db18e2020-06-26 20:51:44 +0100332 virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
333 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800336 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700337 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700339 mInputDevicesChanged = true;
340 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 }
342
Chris Ye3a1e4462020-08-12 10:13:15 -0700343 virtual std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
344 const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700345 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346 }
347
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100348 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
349 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800351
352 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
353 std::unique_lock<std::mutex> lock(mLock);
354 base::ScopedLockAssertion assumeLocked(mLock);
355
356 const bool devicesChanged =
357 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
358 return mInputDevicesChanged;
359 });
360 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
361 mInputDevicesChanged = false;
362 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363};
364
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365// --- FakeEventHub ---
366
367class FakeEventHub : public EventHubInterface {
368 struct KeyInfo {
369 int32_t keyCode;
370 uint32_t flags;
371 };
372
373 struct Device {
374 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700375 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376 PropertyMap configuration;
377 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
378 KeyedVector<int, bool> relativeAxes;
379 KeyedVector<int32_t, int32_t> keyCodeStates;
380 KeyedVector<int32_t, int32_t> scanCodeStates;
381 KeyedVector<int32_t, int32_t> switchStates;
382 KeyedVector<int32_t, int32_t> absoluteAxisValue;
383 KeyedVector<int32_t, KeyInfo> keysByScanCode;
384 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
385 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800386 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700387 bool enabled;
388
389 status_t enable() {
390 enabled = true;
391 return OK;
392 }
393
394 status_t disable() {
395 enabled = false;
396 return OK;
397 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800398
Chris Ye1b0c7342020-07-28 21:57:03 -0700399 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 };
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
Chris Ye1b0c7342020-07-28 21:57:03 -0700419 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> 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
Chris Ye1b0c7342020-07-28 21:57:03 -0700595 virtual Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700597 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598 }
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
Chris Ye3a1e4462020-08-12 10:13:15 -0700818 virtual const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700819 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 }
821
Chris Ye3a1e4462020-08-12 10:13:15 -0700822 virtual bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<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
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848// --- FakeInputMapper ---
849
850class FakeInputMapper : public InputMapper {
851 uint32_t mSources;
852 int32_t mKeyboardType;
853 int32_t mMetaState;
854 KeyedVector<int32_t, int32_t> mKeyCodeStates;
855 KeyedVector<int32_t, int32_t> mScanCodeStates;
856 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800857 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800858
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700859 std::mutex mLock;
860 std::condition_variable mStateChangedCondition;
861 bool mConfigureWasCalled GUARDED_BY(mLock);
862 bool mResetWasCalled GUARDED_BY(mLock);
863 bool mProcessWasCalled GUARDED_BY(mLock);
864 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865
Arthur Hungc23540e2018-11-29 20:42:11 +0800866 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800867public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800868 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
869 : InputMapper(deviceContext),
870 mSources(sources),
871 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800873 mConfigureWasCalled(false),
874 mResetWasCalled(false),
875 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876
877 virtual ~FakeInputMapper() { }
878
879 void setKeyboardType(int32_t keyboardType) {
880 mKeyboardType = keyboardType;
881 }
882
883 void setMetaState(int32_t metaState) {
884 mMetaState = metaState;
885 }
886
887 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700888 std::unique_lock<std::mutex> lock(mLock);
889 base::ScopedLockAssertion assumeLocked(mLock);
890 const bool configureCalled =
891 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
892 return mConfigureWasCalled;
893 });
894 if (!configureCalled) {
895 FAIL() << "Expected configure() to have been called.";
896 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897 mConfigureWasCalled = false;
898 }
899
900 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700901 std::unique_lock<std::mutex> lock(mLock);
902 base::ScopedLockAssertion assumeLocked(mLock);
903 const bool resetCalled =
904 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
905 return mResetWasCalled;
906 });
907 if (!resetCalled) {
908 FAIL() << "Expected reset() to have been called.";
909 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800910 mResetWasCalled = false;
911 }
912
Yi Kong9b14ac62018-07-17 13:48:38 -0700913 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700914 std::unique_lock<std::mutex> lock(mLock);
915 base::ScopedLockAssertion assumeLocked(mLock);
916 const bool processCalled =
917 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
918 return mProcessWasCalled;
919 });
920 if (!processCalled) {
921 FAIL() << "Expected process() to have been called.";
922 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 if (outLastEvent) {
924 *outLastEvent = mLastEvent;
925 }
926 mProcessWasCalled = false;
927 }
928
929 void setKeyCodeState(int32_t keyCode, int32_t state) {
930 mKeyCodeStates.replaceValueFor(keyCode, state);
931 }
932
933 void setScanCodeState(int32_t scanCode, int32_t state) {
934 mScanCodeStates.replaceValueFor(scanCode, state);
935 }
936
937 void setSwitchState(int32_t switchCode, int32_t state) {
938 mSwitchStates.replaceValueFor(switchCode, state);
939 }
940
941 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800942 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943 }
944
945private:
946 virtual uint32_t getSources() {
947 return mSources;
948 }
949
950 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
951 InputMapper::populateDeviceInfo(deviceInfo);
952
953 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
954 deviceInfo->setKeyboardType(mKeyboardType);
955 }
956 }
957
Arthur Hungc23540e2018-11-29 20:42:11 +0800958 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700959 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800961
962 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800963 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800964 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
965 mViewport = config->getDisplayViewportByPort(*displayPort);
966 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700967
968 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 }
970
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100971 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700972 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700974 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800975 }
976
977 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700978 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979 mLastEvent = *rawEvent;
980 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700981 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 }
983
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100984 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
986 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
987 }
988
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100989 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
991 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
992 }
993
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100994 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995 ssize_t index = mSwitchStates.indexOfKey(switchCode);
996 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
997 }
998
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100999 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000 const int32_t* keyCodes, uint8_t* outFlags) {
1001 bool result = false;
1002 for (size_t i = 0; i < numCodes; i++) {
1003 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1004 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1005 outFlags[i] = 1;
1006 result = true;
1007 }
1008 }
1009 }
1010 return result;
1011 }
1012
1013 virtual int32_t getMetaState() {
1014 return mMetaState;
1015 }
1016
1017 virtual void fadePointer() {
1018 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001019
1020 virtual std::optional<int32_t> getAssociatedDisplay() {
1021 if (mViewport) {
1022 return std::make_optional(mViewport->displayId);
1023 }
1024 return std::nullopt;
1025 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026};
1027
1028
1029// --- InstrumentedInputReader ---
1030
1031class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001032 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001033
1034public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001035 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1036 const sp<InputReaderPolicyInterface>& policy,
1037 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001038 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001040 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001041
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001042 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001044 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001045 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046 InputDeviceIdentifier identifier;
1047 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001048 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001050 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051 }
1052
Prabir Pradhan28efc192019-11-05 01:10:04 +00001053 // Make the protected loopOnce method accessible to tests.
1054 using InputReader::loopOnce;
1055
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001057 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1058 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001059 if (!mNextDevices.empty()) {
1060 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1061 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062 return device;
1063 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001064 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065 }
1066
arthurhungdcef2dc2020-08-11 14:47:50 +08001067 // --- FakeInputReaderContext ---
1068 class FakeInputReaderContext : public ContextImpl {
1069 int32_t mGlobalMetaState;
1070 bool mUpdateGlobalMetaStateWasCalled;
1071 int32_t mGeneration;
1072
1073 public:
1074 FakeInputReaderContext(InputReader* reader)
1075 : ContextImpl(reader),
1076 mGlobalMetaState(0),
1077 mUpdateGlobalMetaStateWasCalled(false),
1078 mGeneration(1) {}
1079
1080 virtual ~FakeInputReaderContext() {}
1081
1082 void assertUpdateGlobalMetaStateWasCalled() {
1083 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1084 << "Expected updateGlobalMetaState() to have been called.";
1085 mUpdateGlobalMetaStateWasCalled = false;
1086 }
1087
1088 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1089
1090 uint32_t getGeneration() { return mGeneration; }
1091
1092 void updateGlobalMetaState() override {
1093 mUpdateGlobalMetaStateWasCalled = true;
1094 ContextImpl::updateGlobalMetaState();
1095 }
1096
1097 int32_t getGlobalMetaState() override {
1098 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1099 }
1100
1101 int32_t bumpGeneration() override {
1102 mGeneration = ContextImpl::bumpGeneration();
1103 return mGeneration;
1104 }
1105 } mFakeContext;
1106
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001108
1109public:
1110 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111};
1112
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001113// --- InputReaderPolicyTest ---
1114class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001115protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001116 sp<FakeInputReaderPolicy> mFakePolicy;
1117
Prabir Pradhan28efc192019-11-05 01:10:04 +00001118 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1119 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001120};
1121
1122/**
1123 * Check that empty set of viewports is an acceptable configuration.
1124 * Also try to get internal viewport two different ways - by type and by uniqueId.
1125 *
1126 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1127 * Such configuration is not currently allowed.
1128 */
1129TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001130 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001131
1132 // We didn't add any viewports yet, so there shouldn't be any.
1133 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001134 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001135 ASSERT_FALSE(internalViewport);
1136
1137 // Add an internal viewport, then clear it
1138 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001139 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT,
1140 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001141
1142 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001143 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001144 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001145 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001146
1147 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001148 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001149 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001150 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001151
1152 mFakePolicy->clearViewports();
1153 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001154 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001155 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001156 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001157 ASSERT_FALSE(internalViewport);
1158}
1159
1160TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1161 const std::string internalUniqueId = "local:0";
1162 const std::string externalUniqueId = "local:1";
1163 const std::string virtualUniqueId1 = "virtual:2";
1164 const std::string virtualUniqueId2 = "virtual:3";
1165 constexpr int32_t virtualDisplayId1 = 2;
1166 constexpr int32_t virtualDisplayId2 = 3;
1167
1168 // Add an internal viewport
1169 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001170 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT,
1171 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001172 // Add an external viewport
1173 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001174 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT,
1175 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001176 // Add an virtual viewport
1177 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001178 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT,
1179 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001180 // Add another virtual viewport
1181 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001182 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT,
1183 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001184
1185 // Check matching by type for internal
1186 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001187 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001188 ASSERT_TRUE(internalViewport);
1189 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1190
1191 // Check matching by type for external
1192 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001193 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001194 ASSERT_TRUE(externalViewport);
1195 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1196
1197 // Check matching by uniqueId for virtual viewport #1
1198 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001199 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001200 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001201 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001202 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1203 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1204
1205 // Check matching by uniqueId for virtual viewport #2
1206 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001207 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001208 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001209 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1211 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1212}
1213
1214
1215/**
1216 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1217 * that lookup works by checking display id.
1218 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1219 */
1220TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1221 const std::string uniqueId1 = "uniqueId1";
1222 const std::string uniqueId2 = "uniqueId2";
1223 constexpr int32_t displayId1 = 2;
1224 constexpr int32_t displayId2 = 3;
1225
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001226 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1227 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001228 for (const ViewportType& type : types) {
1229 mFakePolicy->clearViewports();
1230 // Add a viewport
1231 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001232 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001233 // Add another viewport
1234 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001235 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001236
1237 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001238 std::optional<DisplayViewport> viewport1 =
1239 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001240 ASSERT_TRUE(viewport1);
1241 ASSERT_EQ(displayId1, viewport1->displayId);
1242 ASSERT_EQ(type, viewport1->type);
1243
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001244 std::optional<DisplayViewport> viewport2 =
1245 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001246 ASSERT_TRUE(viewport2);
1247 ASSERT_EQ(displayId2, viewport2->displayId);
1248 ASSERT_EQ(type, viewport2->type);
1249
1250 // When there are multiple viewports of the same kind, and uniqueId is not specified
1251 // in the call to getDisplayViewport, then that situation is not supported.
1252 // The viewports can be stored in any order, so we cannot rely on the order, since that
1253 // is just implementation detail.
1254 // However, we can check that it still returns *a* viewport, we just cannot assert
1255 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001256 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001257 ASSERT_TRUE(someViewport);
1258 }
1259}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001260
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001261/**
1262 * Check getDisplayViewportByPort
1263 */
1264TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001265 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001266 const std::string uniqueId1 = "uniqueId1";
1267 const std::string uniqueId2 = "uniqueId2";
1268 constexpr int32_t displayId1 = 1;
1269 constexpr int32_t displayId2 = 2;
1270 const uint8_t hdmi1 = 0;
1271 const uint8_t hdmi2 = 1;
1272 const uint8_t hdmi3 = 2;
1273
1274 mFakePolicy->clearViewports();
1275 // Add a viewport that's associated with some display port that's not of interest.
1276 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1277 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1278 // Add another viewport, connected to HDMI1 port
1279 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1280 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1281
1282 // Check that correct display viewport was returned by comparing the display ports.
1283 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1284 ASSERT_TRUE(hdmi1Viewport);
1285 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1286 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1287
1288 // Check that we can still get the same viewport using the uniqueId
1289 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1290 ASSERT_TRUE(hdmi1Viewport);
1291 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1292 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1293 ASSERT_EQ(type, hdmi1Viewport->type);
1294
1295 // Check that we cannot find a port with "HDMI2", because we never added one
1296 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1297 ASSERT_FALSE(hdmi2Viewport);
1298}
1299
Michael Wrightd02c5b62014-02-10 15:10:22 -08001300// --- InputReaderTest ---
1301
1302class InputReaderTest : public testing::Test {
1303protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001304 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001305 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001306 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001307 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001308
Prabir Pradhan28efc192019-11-05 01:10:04 +00001309 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001310 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001311 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001312 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001313
Prabir Pradhan28efc192019-11-05 01:10:04 +00001314 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1315 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001316 }
1317
Prabir Pradhan28efc192019-11-05 01:10:04 +00001318 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001319 mFakeListener.clear();
1320 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321 }
1322
Chris Ye1b0c7342020-07-28 21:57:03 -07001323 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001324 const PropertyMap* configuration) {
1325 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001326
1327 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001328 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001329 }
1330 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001331 mReader->loopOnce();
1332 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001333 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1334 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001335 }
1336
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001337 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001338 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001339 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001340 }
1341
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001342 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001343 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001344 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001345 }
1346
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001347 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001348 const std::string& name,
1349 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001350 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001351 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1352 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001353 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001354 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001355 return mapper;
1356 }
1357};
1358
1359TEST_F(InputReaderTest, GetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001360 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1361 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1362 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001363
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001364 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001366 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001367 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001368 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001369 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1370 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1371 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1372
1373 // Should also have received a notification describing the new input devices.
1374 inputDevices = mFakePolicy->getInputDevices();
1375 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001376 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001377 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001378 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1379 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1380 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1381}
1382
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001383TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001384 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001385 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001386 constexpr int32_t eventHubId = 1;
1387 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001388 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001389 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001390 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001391 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001392
Yi Kong9b14ac62018-07-17 13:48:38 -07001393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001394
1395 NotifyDeviceResetArgs resetArgs;
1396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001397 ASSERT_EQ(deviceId, resetArgs.deviceId);
1398
1399 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001400 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001401 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001402
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001404 ASSERT_EQ(deviceId, resetArgs.deviceId);
1405 ASSERT_EQ(device->isEnabled(), false);
1406
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001407 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001408 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001411 ASSERT_EQ(device->isEnabled(), false);
1412
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001413 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001414 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001416 ASSERT_EQ(deviceId, resetArgs.deviceId);
1417 ASSERT_EQ(device->isEnabled(), true);
1418}
1419
Michael Wrightd02c5b62014-02-10 15:10:22 -08001420TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001421 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001422 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001423 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001424 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001425 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001426 AINPUT_SOURCE_KEYBOARD, nullptr);
1427 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001428
1429 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1430 AINPUT_SOURCE_ANY, AKEYCODE_A))
1431 << "Should return unknown when the device id is >= 0 but unknown.";
1432
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001433 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1434 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1435 << "Should return unknown when the device id is valid but the sources are not "
1436 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001437
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001438 ASSERT_EQ(AKEY_STATE_DOWN,
1439 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1440 AKEYCODE_A))
1441 << "Should return value provided by mapper when device id is valid and the device "
1442 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443
1444 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1445 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1446 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1447
1448 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1449 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1450 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1451}
1452
1453TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001454 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001455 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001456 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001457 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001458 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001459 AINPUT_SOURCE_KEYBOARD, nullptr);
1460 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001461
1462 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1463 AINPUT_SOURCE_ANY, KEY_A))
1464 << "Should return unknown when the device id is >= 0 but unknown.";
1465
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001466 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1467 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1468 << "Should return unknown when the device id is valid but the sources are not "
1469 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001470
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001471 ASSERT_EQ(AKEY_STATE_DOWN,
1472 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1473 KEY_A))
1474 << "Should return value provided by mapper when device id is valid and the device "
1475 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476
1477 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1478 AINPUT_SOURCE_TRACKBALL, KEY_A))
1479 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1480
1481 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1482 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1483 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1484}
1485
1486TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001487 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001488 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001489 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001490 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001491 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001492 AINPUT_SOURCE_KEYBOARD, nullptr);
1493 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001494
1495 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1496 AINPUT_SOURCE_ANY, SW_LID))
1497 << "Should return unknown when the device id is >= 0 but unknown.";
1498
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001499 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1500 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1501 << "Should return unknown when the device id is valid but the sources are not "
1502 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001504 ASSERT_EQ(AKEY_STATE_DOWN,
1505 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1506 SW_LID))
1507 << "Should return value provided by mapper when device id is valid and the device "
1508 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001509
1510 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1511 AINPUT_SOURCE_TRACKBALL, SW_LID))
1512 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1513
1514 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1515 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1516 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1517}
1518
1519TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001520 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001521 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001522 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001523 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001524 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001525 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001526
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001527 mapper.addSupportedKeyCode(AKEYCODE_A);
1528 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529
1530 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1531 uint8_t flags[4] = { 0, 0, 0, 1 };
1532
1533 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1534 << "Should return false when device id is >= 0 but unknown.";
1535 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1536
1537 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001538 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1539 << "Should return false when device id is valid but the sources are not supported by "
1540 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001541 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1542
1543 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001544 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1545 keyCodes, flags))
1546 << "Should return value provided by mapper when device id is valid and the device "
1547 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001548 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1549
1550 flags[3] = 1;
1551 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1552 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1553 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1554
1555 flags[3] = 1;
1556 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1557 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1558 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1559}
1560
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001561TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001562 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001563 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564
1565 NotifyConfigurationChangedArgs args;
1566
1567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1568 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1569}
1570
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001571TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001572 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001573 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001574 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001575 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001576 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001577 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001579 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001580 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001581 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1582
1583 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001584 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001586 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587 ASSERT_EQ(EV_KEY, event.type);
1588 ASSERT_EQ(KEY_A, event.code);
1589 ASSERT_EQ(1, event.value);
1590}
1591
Garfield Tan1c7bc862020-01-28 13:24:04 -08001592TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001593 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001594 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001595 constexpr int32_t eventHubId = 1;
1596 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001597 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001598 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001599 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001600 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001601
1602 NotifyDeviceResetArgs resetArgs;
1603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001604 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001605
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001606 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001607 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001609 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001610 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001611
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001612 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001613 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001615 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001616 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001617
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001618 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001619 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001621 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001622 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001623}
1624
Garfield Tan1c7bc862020-01-28 13:24:04 -08001625TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1626 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001627 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001628 constexpr int32_t eventHubId = 1;
1629 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1630 // Must add at least one mapper or the device will be ignored!
1631 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001632 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001633 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1634
1635 NotifyDeviceResetArgs resetArgs;
1636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1637 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1638}
1639
Arthur Hungc23540e2018-11-29 20:42:11 +08001640TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001642 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001643 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001644 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001645 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1646 FakeInputMapper& mapper =
1647 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001648 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001649
1650 const uint8_t hdmi1 = 1;
1651
1652 // Associated touch screen with second display.
1653 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1654
1655 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001656 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001657 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001658 DISPLAY_ORIENTATION_0, "local:0", NO_PORT,
1659 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001660 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001661 DISPLAY_ORIENTATION_0, "local:1", hdmi1,
1662 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001663 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001664 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001665
1666 // Add the device, and make sure all of the callbacks are triggered.
1667 // The device is added after the input port associations are processed since
1668 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001669 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001672 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001673
Arthur Hung2c9a3342019-07-23 14:18:59 +08001674 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001675 ASSERT_EQ(deviceId, device->getId());
1676 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1677 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001678
1679 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001680 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001681 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001682 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001683}
1684
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001685TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1686 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1687 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1688 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1689 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1690 // Must add at least one mapper or the device will be ignored!
1691 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1692 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1693 mReader->pushNextDevice(device);
1694 mReader->pushNextDevice(device);
1695 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1696 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1697
1698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1699
1700 NotifyDeviceResetArgs resetArgs;
1701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1702 ASSERT_EQ(deviceId, resetArgs.deviceId);
1703 ASSERT_TRUE(device->isEnabled());
1704 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1705 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1706
1707 disableDevice(deviceId);
1708 mReader->loopOnce();
1709
1710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1711 ASSERT_EQ(deviceId, resetArgs.deviceId);
1712 ASSERT_FALSE(device->isEnabled());
1713 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1714 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1715
1716 enableDevice(deviceId);
1717 mReader->loopOnce();
1718
1719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1720 ASSERT_EQ(deviceId, resetArgs.deviceId);
1721 ASSERT_TRUE(device->isEnabled());
1722 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1723 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1724}
1725
1726TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1727 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1728 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1729 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1730 // Add two subdevices to device
1731 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1732 FakeInputMapper& mapperDevice1 =
1733 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1734 FakeInputMapper& mapperDevice2 =
1735 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1736 mReader->pushNextDevice(device);
1737 mReader->pushNextDevice(device);
1738 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1739 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1740
1741 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1742 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1743
1744 ASSERT_EQ(AKEY_STATE_DOWN,
1745 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1746 ASSERT_EQ(AKEY_STATE_DOWN,
1747 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1748 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1749 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1750}
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;
Chris Ye1b0c7342020-07-28 21:57:03 -07002029 static const Flags<InputDeviceClass> 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;
arthurhungdcef2dc2020-08-11 14:47:50 +08002035 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002036 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002037
Prabir Pradhan28efc192019-11-05 01:10:04 +00002038 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002039 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002040 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002041 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002042 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2043 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002044 InputDeviceIdentifier identifier;
2045 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002046 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002047 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002048 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002049 mReader->pushNextDevice(mDevice);
2050 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2051 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002052 }
2053
Prabir Pradhan28efc192019-11-05 01:10:04 +00002054 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002055 mFakeListener.clear();
2056 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002057 }
2058};
2059
2060const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002061const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002062const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002063const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2064const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002065const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2066 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002067const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002068
2069TEST_F(InputDeviceTest, ImmutableProperties) {
2070 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002071 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002072 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002073}
2074
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002075TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2076 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002077}
2078
Michael Wrightd02c5b62014-02-10 15:10:22 -08002079TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2080 // Configuration.
2081 InputReaderConfiguration config;
2082 mDevice->configure(ARBITRARY_TIME, &config, 0);
2083
2084 // Reset.
2085 mDevice->reset(ARBITRARY_TIME);
2086
2087 NotifyDeviceResetArgs resetArgs;
2088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2089 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2090 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2091
2092 // Metadata.
2093 ASSERT_TRUE(mDevice->isIgnored());
2094 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2095
2096 InputDeviceInfo info;
2097 mDevice->getDeviceInfo(&info);
2098 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002099 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2101 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2102
2103 // State queries.
2104 ASSERT_EQ(0, mDevice->getMetaState());
2105
2106 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2107 << "Ignored device should return unknown key code state.";
2108 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2109 << "Ignored device should return unknown scan code state.";
2110 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2111 << "Ignored device should return unknown switch state.";
2112
2113 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2114 uint8_t flags[2] = { 0, 1 };
2115 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2116 << "Ignored device should never mark any key codes.";
2117 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2118 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2119}
2120
2121TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2122 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002123 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002124
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002125 FakeInputMapper& mapper1 =
2126 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002127 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2128 mapper1.setMetaState(AMETA_ALT_ON);
2129 mapper1.addSupportedKeyCode(AKEYCODE_A);
2130 mapper1.addSupportedKeyCode(AKEYCODE_B);
2131 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2132 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2133 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2134 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2135 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002136
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002137 FakeInputMapper& mapper2 =
2138 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002139 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002140
2141 InputReaderConfiguration config;
2142 mDevice->configure(ARBITRARY_TIME, &config, 0);
2143
2144 String8 propertyValue;
2145 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2146 << "Device should have read configuration during configuration phase.";
2147 ASSERT_STREQ("value", propertyValue.string());
2148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002149 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2150 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002151
2152 // Reset
2153 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002154 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2155 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002156
2157 NotifyDeviceResetArgs resetArgs;
2158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2159 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2160 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2161
2162 // Metadata.
2163 ASSERT_FALSE(mDevice->isIgnored());
2164 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2165
2166 InputDeviceInfo info;
2167 mDevice->getDeviceInfo(&info);
2168 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002169 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002170 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2171 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2172
2173 // State queries.
2174 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2175 << "Should query mappers and combine meta states.";
2176
2177 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2178 << "Should return unknown key code state when source not supported.";
2179 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2180 << "Should return unknown scan code state when source not supported.";
2181 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2182 << "Should return unknown switch state when source not supported.";
2183
2184 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2185 << "Should query mapper when source is supported.";
2186 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2187 << "Should query mapper when source is supported.";
2188 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2189 << "Should query mapper when source is supported.";
2190
2191 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2192 uint8_t flags[4] = { 0, 0, 0, 1 };
2193 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2194 << "Should do nothing when source is unsupported.";
2195 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2196 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2197 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2198 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2199
2200 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2201 << "Should query mapper when source is supported.";
2202 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2203 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2204 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2205 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2206
2207 // Event handling.
2208 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002209 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002210 mDevice->process(&event, 1);
2211
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002212 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2213 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002214}
2215
Arthur Hung2c9a3342019-07-23 14:18:59 +08002216// A single input device is associated with a specific display. Check that:
2217// 1. Device is disabled if the viewport corresponding to the associated display is not found
2218// 2. Device is disabled when setEnabled API is called
2219TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002220 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002221
2222 // First Configuration.
2223 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2224
2225 // Device should be enabled by default.
2226 ASSERT_TRUE(mDevice->isEnabled());
2227
2228 // Prepare associated info.
2229 constexpr uint8_t hdmi = 1;
2230 const std::string UNIQUE_ID = "local:1";
2231
2232 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2233 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2234 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2235 // Device should be disabled because it is associated with a specific display via
2236 // input port <-> display port association, but the corresponding display is not found
2237 ASSERT_FALSE(mDevice->isEnabled());
2238
2239 // Prepare displays.
2240 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002241 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002242 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2243 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2244 ASSERT_TRUE(mDevice->isEnabled());
2245
2246 // Device should be disabled after set disable.
2247 mFakePolicy->addDisabledDevice(mDevice->getId());
2248 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2249 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2250 ASSERT_FALSE(mDevice->isEnabled());
2251
2252 // Device should still be disabled even found the associated display.
2253 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2254 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2255 ASSERT_FALSE(mDevice->isEnabled());
2256}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257
2258// --- InputMapperTest ---
2259
2260class InputMapperTest : public testing::Test {
2261protected:
2262 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002263 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002264 static const int32_t DEVICE_ID;
2265 static const int32_t DEVICE_GENERATION;
2266 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002267 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002268 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002269
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002270 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002271 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002272 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002273 std::unique_ptr<InstrumentedInputReader> mReader;
2274 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002275
Chris Ye1b0c7342020-07-28 21:57:03 -07002276 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002277 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002278 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002279 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002280 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2281 mFakeListener);
2282 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002283 }
2284
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002285 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2286
Prabir Pradhan28efc192019-11-05 01:10:04 +00002287 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288 mFakeListener.clear();
2289 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002290 }
2291
2292 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002293 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002294 }
2295
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002296 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002297 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002298 mReader->requestRefreshConfiguration(changes);
2299 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002300 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002301 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2302 }
2303
arthurhungdcef2dc2020-08-11 14:47:50 +08002304 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2305 const std::string& location, int32_t eventHubId,
2306 Flags<InputDeviceClass> classes) {
2307 InputDeviceIdentifier identifier;
2308 identifier.name = name;
2309 identifier.location = location;
2310 std::shared_ptr<InputDevice> device =
2311 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2312 identifier);
2313 mReader->pushNextDevice(device);
2314 mFakeEventHub->addDevice(eventHubId, name, classes);
2315 mReader->loopOnce();
2316 return device;
2317 }
2318
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002319 template <class T, typename... Args>
2320 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002321 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002322 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002324 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002325 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002326 }
2327
2328 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002329 int32_t orientation, const std::string& uniqueId,
2330 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002331 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002332 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002333 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2334 }
2335
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002336 void clearViewports() {
2337 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002338 }
2339
arthurhungdcef2dc2020-08-11 14:47:50 +08002340 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002341 RawEvent event;
2342 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002343 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002344 event.type = type;
2345 event.code = code;
2346 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002347 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002348 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002349 }
2350
2351 static void assertMotionRange(const InputDeviceInfo& info,
2352 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2353 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002354 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002355 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2356 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2357 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2358 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2359 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2360 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2361 }
2362
2363 static void assertPointerCoords(const PointerCoords& coords,
2364 float x, float y, float pressure, float size,
2365 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2366 float orientation, float distance) {
2367 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2368 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2369 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2370 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2371 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2372 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2373 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2374 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2375 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2376 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2377 }
2378
Michael Wright17db18e2020-06-26 20:51:44 +01002379 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002380 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002381 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382 ASSERT_NEAR(x, actualX, 1);
2383 ASSERT_NEAR(y, actualY, 1);
2384 }
2385};
2386
2387const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002388const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002389const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2391const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002392const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2393 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002394const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395
2396// --- SwitchInputMapperTest ---
2397
2398class SwitchInputMapperTest : public InputMapperTest {
2399protected:
2400};
2401
2402TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002403 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002405 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406}
2407
2408TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002409 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002410
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002411 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002412 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002413
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002414 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002415 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416}
2417
2418TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002419 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002421 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2422 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2423 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2424 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425
2426 NotifySwitchArgs args;
2427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2428 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002429 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2430 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002431 args.switchMask);
2432 ASSERT_EQ(uint32_t(0), args.policyFlags);
2433}
2434
2435
2436// --- KeyboardInputMapperTest ---
2437
2438class KeyboardInputMapperTest : public InputMapperTest {
2439protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002440 const std::string UNIQUE_ID = "local:0";
2441
2442 void prepareDisplay(int32_t orientation);
2443
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002444 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002445 int32_t originalKeyCode, int32_t rotatedKeyCode,
2446 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002447};
2448
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002449/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2450 * orientation.
2451 */
2452void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002453 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2454 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002455}
2456
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002457void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002458 int32_t originalScanCode, int32_t originalKeyCode,
2459 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002460 NotifyKeyArgs args;
2461
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002462 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2464 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2465 ASSERT_EQ(originalScanCode, args.scanCode);
2466 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002467 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002469 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2471 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2472 ASSERT_EQ(originalScanCode, args.scanCode);
2473 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002474 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002475}
2476
Michael Wrightd02c5b62014-02-10 15:10:22 -08002477TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002478 KeyboardInputMapper& mapper =
2479 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2480 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002481
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002482 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483}
2484
2485TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2486 const int32_t USAGE_A = 0x070004;
2487 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002488 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2489 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002490
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002491 KeyboardInputMapper& mapper =
2492 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2493 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002494 // Initial metastate to AMETA_NONE.
2495 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2496 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497
2498 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002499 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002500 NotifyKeyArgs args;
2501 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, args.eventTime);
2505 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, 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 up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002514 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2516 ASSERT_EQ(DEVICE_ID, args.deviceId);
2517 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2518 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2519 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2520 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2521 ASSERT_EQ(KEY_HOME, args.scanCode);
2522 ASSERT_EQ(AMETA_NONE, args.metaState);
2523 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2524 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2525 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2526
2527 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002528 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2529 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2531 ASSERT_EQ(DEVICE_ID, args.deviceId);
2532 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2533 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2534 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2535 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2536 ASSERT_EQ(0, args.scanCode);
2537 ASSERT_EQ(AMETA_NONE, args.metaState);
2538 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2539 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2540 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2541
2542 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002543 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2544 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2546 ASSERT_EQ(DEVICE_ID, args.deviceId);
2547 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2548 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2549 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2550 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2551 ASSERT_EQ(0, args.scanCode);
2552 ASSERT_EQ(AMETA_NONE, args.metaState);
2553 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2554 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2555 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2556
2557 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002558 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2559 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2561 ASSERT_EQ(DEVICE_ID, args.deviceId);
2562 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2563 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2564 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2565 ASSERT_EQ(0, args.keyCode);
2566 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2567 ASSERT_EQ(AMETA_NONE, args.metaState);
2568 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2569 ASSERT_EQ(0U, args.policyFlags);
2570 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2571
2572 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002573 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2574 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2576 ASSERT_EQ(DEVICE_ID, args.deviceId);
2577 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2578 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2579 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2580 ASSERT_EQ(0, args.keyCode);
2581 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2582 ASSERT_EQ(AMETA_NONE, args.metaState);
2583 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2584 ASSERT_EQ(0U, args.policyFlags);
2585 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2586}
2587
2588TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002589 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2590 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002592 KeyboardInputMapper& mapper =
2593 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2594 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002595
arthurhungc903df12020-08-11 15:08:42 +08002596 // Initial metastate to AMETA_NONE.
2597 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2598 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599
2600 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002601 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602 NotifyKeyArgs args;
2603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2604 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002605 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002606 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607
2608 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002609 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2611 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002612 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
2614 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002615 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2617 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002618 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619
2620 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002621 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2623 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002624 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002625 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626}
2627
2628TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002629 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2630 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2631 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2632 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002634 KeyboardInputMapper& mapper =
2635 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2636 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002638 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2640 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2641 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2642 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2644 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2646 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2647}
2648
2649TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002650 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2651 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2652 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2653 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002656 KeyboardInputMapper& mapper =
2657 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2658 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002659
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002660 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002661 ASSERT_NO_FATAL_FAILURE(
2662 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2663 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2664 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2665 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2666 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2667 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2668 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002670 clearViewports();
2671 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002672 ASSERT_NO_FATAL_FAILURE(
2673 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2674 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2675 AKEYCODE_DPAD_UP, DISPLAY_ID));
2676 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2677 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2678 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2679 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002681 clearViewports();
2682 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002683 ASSERT_NO_FATAL_FAILURE(
2684 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2685 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2686 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2687 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2688 AKEYCODE_DPAD_UP, DISPLAY_ID));
2689 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2690 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002692 clearViewports();
2693 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002694 ASSERT_NO_FATAL_FAILURE(
2695 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2696 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2697 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2698 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2699 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2700 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2701 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702
2703 // Special case: if orientation changes while key is down, we still emit the same keycode
2704 // in the key up as we did in the key down.
2705 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002706 clearViewports();
2707 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002708 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2710 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2711 ASSERT_EQ(KEY_UP, args.scanCode);
2712 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2713
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002714 clearViewports();
2715 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002716 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2718 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2719 ASSERT_EQ(KEY_UP, args.scanCode);
2720 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2721}
2722
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002723TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2724 // If the keyboard is not orientation aware,
2725 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002726 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002727
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002728 KeyboardInputMapper& mapper =
2729 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2730 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002731 NotifyKeyArgs args;
2732
2733 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002734 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002736 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2738 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2739
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002740 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002741 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002743 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2745 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2746}
2747
2748TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2749 // If the keyboard is orientation aware,
2750 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002751 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002752
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002753 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002754 KeyboardInputMapper& mapper =
2755 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2756 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002757 NotifyKeyArgs args;
2758
2759 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2760 // ^--- already checked by the previous test
2761
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002762 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002763 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002764 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002766 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2768 ASSERT_EQ(DISPLAY_ID, args.displayId);
2769
2770 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002771 clearViewports();
2772 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002773 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002774 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002776 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2778 ASSERT_EQ(newDisplayId, args.displayId);
2779}
2780
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782 KeyboardInputMapper& mapper =
2783 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2784 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002786 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002789 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002790 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791}
2792
2793TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002794 KeyboardInputMapper& mapper =
2795 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2796 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002797
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002798 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002799 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002801 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002802 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803}
2804
2805TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002806 KeyboardInputMapper& mapper =
2807 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2808 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002810 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002811
2812 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2813 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002814 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815 ASSERT_TRUE(flags[0]);
2816 ASSERT_FALSE(flags[1]);
2817}
2818
2819TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002820 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2821 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2822 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2823 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2824 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2825 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002827 KeyboardInputMapper& mapper =
2828 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2829 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002830 // Initial metastate to AMETA_NONE.
2831 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2832 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833
2834 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002835 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2836 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2837 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838
2839 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002840 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2841 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002842 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2843 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2844 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002845 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846
2847 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002848 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2849 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002850 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2851 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2852 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002853 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
2855 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002856 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2857 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002858 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2859 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2860 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002861 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862
2863 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002864 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2865 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002866 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2867 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2868 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002869 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002870
2871 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002872 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2873 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002874 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2875 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2876 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002877 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878
2879 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002880 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2881 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002882 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2883 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2884 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002885 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002886}
2887
Arthur Hung2c9a3342019-07-23 14:18:59 +08002888TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2889 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002890 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2891 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2892 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2893 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002894
2895 // keyboard 2.
2896 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08002897 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002898 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002899 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08002900 std::shared_ptr<InputDevice> device2 =
2901 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
2902 Flags<InputDeviceClass>(0));
2903
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002904 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2905 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2906 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2907 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002908
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002909 KeyboardInputMapper& mapper =
2910 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2911 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002912
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002913 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002914 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002915 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002916 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2917 device2->reset(ARBITRARY_TIME);
2918
2919 // Prepared displays and associated info.
2920 constexpr uint8_t hdmi1 = 0;
2921 constexpr uint8_t hdmi2 = 1;
2922 const std::string SECONDARY_UNIQUE_ID = "local:1";
2923
2924 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2925 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2926
2927 // No associated display viewport found, should disable the device.
2928 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2929 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2930 ASSERT_FALSE(device2->isEnabled());
2931
2932 // Prepare second display.
2933 constexpr int32_t newDisplayId = 2;
2934 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002935 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002936 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002937 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002938 // Default device will reconfigure above, need additional reconfiguration for another device.
2939 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2940 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2941
2942 // Device should be enabled after the associated display is found.
2943 ASSERT_TRUE(mDevice->isEnabled());
2944 ASSERT_TRUE(device2->isEnabled());
2945
2946 // Test pad key events
2947 ASSERT_NO_FATAL_FAILURE(
2948 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2949 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2950 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2951 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2952 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2953 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2954 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2955
2956 ASSERT_NO_FATAL_FAILURE(
2957 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2958 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2959 AKEYCODE_DPAD_RIGHT, newDisplayId));
2960 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2961 AKEYCODE_DPAD_DOWN, newDisplayId));
2962 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2963 AKEYCODE_DPAD_LEFT, newDisplayId));
2964}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965
arthurhungc903df12020-08-11 15:08:42 +08002966TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
2967 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2968 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2969 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2970 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2971 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2972 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2973
2974 KeyboardInputMapper& mapper =
2975 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2976 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2977 // Initial metastate to AMETA_NONE.
2978 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2979 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
2980
2981 // Initialization should have turned all of the lights off.
2982 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2983 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2984 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
2985
2986 // Toggle caps lock on.
2987 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2988 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
2989 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2990 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
2991
2992 // Toggle num lock on.
2993 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2994 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
2995 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2996 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
2997
2998 // Toggle scroll lock on.
2999 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3000 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3001 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3002 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3003
3004 mFakeEventHub->removeDevice(EVENTHUB_ID);
3005 mReader->loopOnce();
3006
3007 // keyboard 2 should default toggle keys.
3008 const std::string USB2 = "USB2";
3009 const std::string DEVICE_NAME2 = "KEYBOARD2";
3010 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3011 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3012 std::shared_ptr<InputDevice> device2 =
3013 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3014 Flags<InputDeviceClass>(0));
3015 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3016 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3017 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3018 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3019 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3020 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3021
3022 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3023 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3024 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3025 device2->reset(ARBITRARY_TIME);
3026
3027 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3028 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3029 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
3030 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3031}
3032
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003033// --- KeyboardInputMapperTest_ExternalDevice ---
3034
3035class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3036protected:
3037 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07003038 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003039 }
3040};
3041
3042TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003043 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3044 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003045
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003046 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3047 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3048 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3049 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003051 KeyboardInputMapper& mapper =
3052 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3053 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003054
3055 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3056 NotifyKeyArgs args;
3057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3058 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3059
3060 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3062 ASSERT_EQ(uint32_t(0), args.policyFlags);
3063
3064 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3066 ASSERT_EQ(uint32_t(0), args.policyFlags);
3067
3068 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3070 ASSERT_EQ(uint32_t(0), args.policyFlags);
3071
3072 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3074 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3075
3076 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3078 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3079}
3080
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003081TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003082 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003083
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003084 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3085 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3086 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003087
Powei Fengd041c5d2019-05-03 17:11:33 -07003088 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 KeyboardInputMapper& mapper =
3090 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3091 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003092
3093 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3094 NotifyKeyArgs args;
3095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3096 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3097
3098 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3100 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3101
3102 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3104 ASSERT_EQ(uint32_t(0), args.policyFlags);
3105
3106 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3108 ASSERT_EQ(uint32_t(0), args.policyFlags);
3109
3110 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3112 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3113
3114 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3116 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3117}
3118
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119// --- CursorInputMapperTest ---
3120
3121class CursorInputMapperTest : public InputMapperTest {
3122protected:
3123 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3124
Michael Wright17db18e2020-06-26 20:51:44 +01003125 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126
Prabir Pradhan28efc192019-11-05 01:10:04 +00003127 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 InputMapperTest::SetUp();
3129
Michael Wright17db18e2020-06-26 20:51:44 +01003130 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003131 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132 }
3133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003134 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3135 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003136
3137 void prepareDisplay(int32_t orientation) {
3138 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003139 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003140 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3141 orientation, uniqueId, NO_PORT, viewportType);
3142 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143};
3144
3145const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003147void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3148 int32_t originalY, int32_t rotatedX,
3149 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150 NotifyMotionArgs args;
3151
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003152 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3153 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3154 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3158 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3159 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3160 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3161}
3162
3163TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003164 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003165 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003167 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168}
3169
3170TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003171 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003172 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003174 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175}
3176
3177TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003179 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180
3181 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003182 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183
3184 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003185 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3186 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3188 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3189
3190 // When the bounds are set, then there should be a valid motion range.
3191 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3192
3193 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003194 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195
3196 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3197 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3198 1, 800 - 1, 0.0f, 0.0f));
3199 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3200 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3201 2, 480 - 1, 0.0f, 0.0f));
3202 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3203 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3204 0.0f, 1.0f, 0.0f, 0.0f));
3205}
3206
3207TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003208 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003209 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210
3211 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003212 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003213
3214 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3215 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3216 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3217 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3218 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3219 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3220 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3221 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3222 0.0f, 1.0f, 0.0f, 0.0f));
3223}
3224
3225TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003227 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228
arthurhungdcef2dc2020-08-11 14:47:50 +08003229 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230
3231 NotifyMotionArgs args;
3232
3233 // Button press.
3234 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003235 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3236 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3238 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3239 ASSERT_EQ(DEVICE_ID, args.deviceId);
3240 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3241 ASSERT_EQ(uint32_t(0), args.policyFlags);
3242 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3243 ASSERT_EQ(0, args.flags);
3244 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3245 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3246 ASSERT_EQ(0, args.edgeFlags);
3247 ASSERT_EQ(uint32_t(1), args.pointerCount);
3248 ASSERT_EQ(0, args.pointerProperties[0].id);
3249 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3251 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3252 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3253 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3254 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3255
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3257 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3258 ASSERT_EQ(DEVICE_ID, args.deviceId);
3259 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3260 ASSERT_EQ(uint32_t(0), args.policyFlags);
3261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3262 ASSERT_EQ(0, args.flags);
3263 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3264 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3265 ASSERT_EQ(0, args.edgeFlags);
3266 ASSERT_EQ(uint32_t(1), args.pointerCount);
3267 ASSERT_EQ(0, args.pointerProperties[0].id);
3268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3270 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3271 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3272 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3273 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3274
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003276 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3277 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3279 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3280 ASSERT_EQ(DEVICE_ID, args.deviceId);
3281 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3282 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003283 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3284 ASSERT_EQ(0, args.flags);
3285 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3286 ASSERT_EQ(0, args.buttonState);
3287 ASSERT_EQ(0, args.edgeFlags);
3288 ASSERT_EQ(uint32_t(1), args.pointerCount);
3289 ASSERT_EQ(0, args.pointerProperties[0].id);
3290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3292 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3293 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3294 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3295 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3296
3297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3298 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3299 ASSERT_EQ(DEVICE_ID, args.deviceId);
3300 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3301 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3303 ASSERT_EQ(0, args.flags);
3304 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3305 ASSERT_EQ(0, args.buttonState);
3306 ASSERT_EQ(0, args.edgeFlags);
3307 ASSERT_EQ(uint32_t(1), args.pointerCount);
3308 ASSERT_EQ(0, args.pointerProperties[0].id);
3309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3311 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3312 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3313 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3314 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3315}
3316
3317TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003319 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320
3321 NotifyMotionArgs args;
3322
3323 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003324 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
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));
3327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3329 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3330
3331 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003332 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3333 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3337 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3338}
3339
3340TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003342 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343
3344 NotifyMotionArgs args;
3345
3346 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003347 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3348 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3350 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3352 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3353
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3355 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3357 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3358
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003360 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3361 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3365 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3366
3367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3370 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3371}
3372
3373TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003375 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376
3377 NotifyMotionArgs args;
3378
3379 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003380 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3381 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3382 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3383 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3385 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3387 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3388 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3389
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3393 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3394 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3395
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003397 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3398 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3399 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3403 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3404 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3405
3406 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003407 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3408 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3412 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3413
3414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3417 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3418}
3419
3420TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003422 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003424 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3426 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3427 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3428 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3429 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3431 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3432 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3433}
3434
3435TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436 addConfigurationProperty("cursor.mode", "navigation");
3437 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003438 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003440 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3442 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3443 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3444 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3445 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3446 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3447 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3448 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3449
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003450 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3452 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3453 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3454 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3455 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3456 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3457 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3458 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3459
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003460 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3462 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3463 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3464 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3465 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3466 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3467 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3468 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3469
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003470 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3472 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3473 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3474 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3475 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3476 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3479}
3480
3481TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003483 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484
3485 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3486 mFakePointerController->setPosition(100, 200);
3487 mFakePointerController->setButtonState(0);
3488
3489 NotifyMotionArgs motionArgs;
3490 NotifyKeyArgs keyArgs;
3491
3492 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003493 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3494 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3496 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3497 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3498 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3500 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3501
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3504 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3505 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3507 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3508
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003509 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3510 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003512 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513 ASSERT_EQ(0, motionArgs.buttonState);
3514 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3516 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3517
3518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003519 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 ASSERT_EQ(0, motionArgs.buttonState);
3521 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3523 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3524
3525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003527 ASSERT_EQ(0, motionArgs.buttonState);
3528 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3530 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3531
3532 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003533 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3534 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3535 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3537 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3538 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3539 motionArgs.buttonState);
3540 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3541 mFakePointerController->getButtonState());
3542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3543 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3544
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3546 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3547 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3548 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3549 mFakePointerController->getButtonState());
3550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3551 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3552
3553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3554 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3555 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3556 motionArgs.buttonState);
3557 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3558 mFakePointerController->getButtonState());
3559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3560 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3561
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003562 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3563 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3567 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3569 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3570
3571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003573 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3574 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3576 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3577
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003578 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3579 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003581 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3582 ASSERT_EQ(0, motionArgs.buttonState);
3583 ASSERT_EQ(0, mFakePointerController->getButtonState());
3584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3585 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 -08003586 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3587 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003588
3589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003590 ASSERT_EQ(0, motionArgs.buttonState);
3591 ASSERT_EQ(0, mFakePointerController->getButtonState());
3592 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3594 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 -08003595
Michael Wrightd02c5b62014-02-10 15:10:22 -08003596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3597 ASSERT_EQ(0, motionArgs.buttonState);
3598 ASSERT_EQ(0, mFakePointerController->getButtonState());
3599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3601 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3602
3603 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003604 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3605 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3607 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3608 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003609
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3613 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3615 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3616
3617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3619 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3620 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3622 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3623
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003624 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3625 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628 ASSERT_EQ(0, motionArgs.buttonState);
3629 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3631 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3632
3633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003635 ASSERT_EQ(0, motionArgs.buttonState);
3636 ASSERT_EQ(0, mFakePointerController->getButtonState());
3637
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3639 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3641 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3642 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3643
3644 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003645 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3646 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3648 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3649 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003650
Michael Wrightd02c5b62014-02-10 15:10:22 -08003651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003652 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3654 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3656 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3657
3658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3659 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3660 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3661 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3663 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3664
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003665 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3666 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003668 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669 ASSERT_EQ(0, motionArgs.buttonState);
3670 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3672 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 -08003673
3674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3675 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3676 ASSERT_EQ(0, motionArgs.buttonState);
3677 ASSERT_EQ(0, mFakePointerController->getButtonState());
3678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3679 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3680
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3682 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3683 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3684
3685 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003686 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3687 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3689 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3690 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003691
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003693 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3695 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3697 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3698
3699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3700 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3701 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3702 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3704 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3705
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003706 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3707 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003709 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710 ASSERT_EQ(0, motionArgs.buttonState);
3711 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3713 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 -08003714
3715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3717 ASSERT_EQ(0, motionArgs.buttonState);
3718 ASSERT_EQ(0, mFakePointerController->getButtonState());
3719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3720 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3721
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3723 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3724 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3725
3726 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003727 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3728 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3730 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3731 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003732
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3736 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3738 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3739
3740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3741 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3742 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3743 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3745 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3746
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003747 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3748 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751 ASSERT_EQ(0, motionArgs.buttonState);
3752 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3754 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 -08003755
3756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3758 ASSERT_EQ(0, motionArgs.buttonState);
3759 ASSERT_EQ(0, mFakePointerController->getButtonState());
3760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3761 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3762
Michael Wrightd02c5b62014-02-10 15:10:22 -08003763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3764 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3765 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3766}
3767
3768TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003770 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771
3772 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3773 mFakePointerController->setPosition(100, 200);
3774 mFakePointerController->setButtonState(0);
3775
3776 NotifyMotionArgs args;
3777
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003778 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3779 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3780 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003782 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3783 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3785 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 +01003786 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003787}
3788
3789TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003790 addConfigurationProperty("cursor.mode", "pointer");
3791 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003792 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003793
3794 NotifyDeviceResetArgs resetArgs;
3795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3796 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3797 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3798
3799 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3800 mFakePointerController->setPosition(100, 200);
3801 mFakePointerController->setButtonState(0);
3802
3803 NotifyMotionArgs args;
3804
3805 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003806 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3807 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3808 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3810 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3813 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 +01003814 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003815
3816 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003817 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3818 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3820 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3821 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3823 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3825 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3826 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3828 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3829
3830 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003831 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3832 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3834 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3837 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3839 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3842 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3843
3844 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003845 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3846 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3847 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3849 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3852 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 +01003853 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003854
3855 // Disable pointer capture and check that the device generation got bumped
3856 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08003857 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003858 mFakePolicy->setPointerCapture(false);
3859 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08003860 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003861
3862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3863 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3864 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3865
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003866 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3867 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3868 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3870 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3873 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 +01003874 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003875}
3876
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003877TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003878 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003879
Garfield Tan888a6a42020-01-09 11:39:16 -08003880 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003881 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003882 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3883 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003884 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003885 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3886 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3887
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003888 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3889 mFakePointerController->setPosition(100, 200);
3890 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003891
3892 NotifyMotionArgs args;
3893 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3894 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3895 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3897 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3898 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3900 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 +01003901 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003902 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3903}
3904
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905// --- TouchInputMapperTest ---
3906
3907class TouchInputMapperTest : public InputMapperTest {
3908protected:
3909 static const int32_t RAW_X_MIN;
3910 static const int32_t RAW_X_MAX;
3911 static const int32_t RAW_Y_MIN;
3912 static const int32_t RAW_Y_MAX;
3913 static const int32_t RAW_TOUCH_MIN;
3914 static const int32_t RAW_TOUCH_MAX;
3915 static const int32_t RAW_TOOL_MIN;
3916 static const int32_t RAW_TOOL_MAX;
3917 static const int32_t RAW_PRESSURE_MIN;
3918 static const int32_t RAW_PRESSURE_MAX;
3919 static const int32_t RAW_ORIENTATION_MIN;
3920 static const int32_t RAW_ORIENTATION_MAX;
3921 static const int32_t RAW_DISTANCE_MIN;
3922 static const int32_t RAW_DISTANCE_MAX;
3923 static const int32_t RAW_TILT_MIN;
3924 static const int32_t RAW_TILT_MAX;
3925 static const int32_t RAW_ID_MIN;
3926 static const int32_t RAW_ID_MAX;
3927 static const int32_t RAW_SLOT_MIN;
3928 static const int32_t RAW_SLOT_MAX;
3929 static const float X_PRECISION;
3930 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003931 static const float X_PRECISION_VIRTUAL;
3932 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933
3934 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003935 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936
3937 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3938
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003939 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003940 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003941
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942 enum Axes {
3943 POSITION = 1 << 0,
3944 TOUCH = 1 << 1,
3945 TOOL = 1 << 2,
3946 PRESSURE = 1 << 3,
3947 ORIENTATION = 1 << 4,
3948 MINOR = 1 << 5,
3949 ID = 1 << 6,
3950 DISTANCE = 1 << 7,
3951 TILT = 1 << 8,
3952 SLOT = 1 << 9,
3953 TOOL_TYPE = 1 << 10,
3954 };
3955
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003956 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3957 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003958 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003960 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961 int32_t toRawX(float displayX);
3962 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003963 float toCookedX(float rawX, float rawY);
3964 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003966 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003968 float toDisplayY(int32_t rawY, int32_t displayHeight);
3969
Michael Wrightd02c5b62014-02-10 15:10:22 -08003970};
3971
3972const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3973const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3974const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3975const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3976const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3977const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3978const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3979const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003980const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3981const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3983const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3984const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3985const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3986const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3987const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3988const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3989const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3990const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3991const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3992const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3993const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003994const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3995 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3996const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3997 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003998const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3999 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004000
4001const float TouchInputMapperTest::GEOMETRIC_SCALE =
4002 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4003 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4004
4005const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4006 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4007 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4008};
4009
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004010void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004011 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4012 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004013}
4014
4015void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4016 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4017 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018}
4019
Santos Cordonfa5cf462017-04-05 10:37:00 -07004020void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004021 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4022 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4023 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004024}
4025
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004027 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4028 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4029 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4030 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031}
4032
Jason Gerecke489fda82012-09-07 17:19:40 -07004033void TouchInputMapperTest::prepareLocationCalibration() {
4034 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4035}
4036
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037int32_t TouchInputMapperTest::toRawX(float displayX) {
4038 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4039}
4040
4041int32_t TouchInputMapperTest::toRawY(float displayY) {
4042 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4043}
4044
Jason Gerecke489fda82012-09-07 17:19:40 -07004045float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4046 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4047 return rawX;
4048}
4049
4050float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4051 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4052 return rawY;
4053}
4054
Michael Wrightd02c5b62014-02-10 15:10:22 -08004055float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004056 return toDisplayX(rawX, DISPLAY_WIDTH);
4057}
4058
4059float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4060 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061}
4062
4063float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004064 return toDisplayY(rawY, DISPLAY_HEIGHT);
4065}
4066
4067float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4068 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069}
4070
4071
4072// --- SingleTouchInputMapperTest ---
4073
4074class SingleTouchInputMapperTest : public TouchInputMapperTest {
4075protected:
4076 void prepareButtons();
4077 void prepareAxes(int axes);
4078
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004079 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4080 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4081 void processUp(SingleTouchInputMapper& mappery);
4082 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4083 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4084 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4085 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4086 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4087 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088};
4089
4090void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004091 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092}
4093
4094void SingleTouchInputMapperTest::prepareAxes(int axes) {
4095 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004096 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098 }
4099 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4101 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 }
4103 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004104 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4105 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106 }
4107 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004108 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4109 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 }
4111 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4113 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114 }
4115}
4116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004117void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004118 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4119 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4120 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121}
4122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004123void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004124 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4125 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004126}
4127
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004128void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004129 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130}
4131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004132void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004133 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134}
4135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004136void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4137 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004138 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139}
4140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004141void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004142 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143}
4144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004145void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4146 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004147 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4148 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149}
4150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004151void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4152 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004153 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154}
4155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004156void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004157 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004158}
4159
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161 prepareButtons();
4162 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004163 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004165 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166}
4167
4168TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004169 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4170 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171 prepareButtons();
4172 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004173 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004175 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176}
4177
4178TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 prepareButtons();
4180 prepareAxes(POSITION);
4181 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004182 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004184 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185}
4186
4187TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188 prepareButtons();
4189 prepareAxes(POSITION);
4190 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004191 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004193 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194}
4195
4196TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197 addConfigurationProperty("touch.deviceType", "touchScreen");
4198 prepareDisplay(DISPLAY_ORIENTATION_0);
4199 prepareButtons();
4200 prepareAxes(POSITION);
4201 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004202 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203
4204 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004205 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206
4207 // Virtual key is down.
4208 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4209 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4210 processDown(mapper, x, y);
4211 processSync(mapper);
4212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4213
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004214 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215
4216 // Virtual key is up.
4217 processUp(mapper);
4218 processSync(mapper);
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4220
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004221 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222}
4223
4224TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 addConfigurationProperty("touch.deviceType", "touchScreen");
4226 prepareDisplay(DISPLAY_ORIENTATION_0);
4227 prepareButtons();
4228 prepareAxes(POSITION);
4229 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004230 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231
4232 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004233 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234
4235 // Virtual key is down.
4236 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4237 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4238 processDown(mapper, x, y);
4239 processSync(mapper);
4240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4241
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004242 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243
4244 // Virtual key is up.
4245 processUp(mapper);
4246 processSync(mapper);
4247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004249 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250}
4251
4252TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 addConfigurationProperty("touch.deviceType", "touchScreen");
4254 prepareDisplay(DISPLAY_ORIENTATION_0);
4255 prepareButtons();
4256 prepareAxes(POSITION);
4257 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004258 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259
4260 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4261 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004262 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_TRUE(flags[0]);
4264 ASSERT_FALSE(flags[1]);
4265}
4266
4267TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 addConfigurationProperty("touch.deviceType", "touchScreen");
4269 prepareDisplay(DISPLAY_ORIENTATION_0);
4270 prepareButtons();
4271 prepareAxes(POSITION);
4272 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004273 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
arthurhungdcef2dc2020-08-11 14:47:50 +08004275 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276
4277 NotifyKeyArgs args;
4278
4279 // Press virtual key.
4280 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4281 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4282 processDown(mapper, x, y);
4283 processSync(mapper);
4284
4285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4286 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4287 ASSERT_EQ(DEVICE_ID, args.deviceId);
4288 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4289 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4290 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4291 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4292 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4293 ASSERT_EQ(KEY_HOME, args.scanCode);
4294 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4295 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4296
4297 // Release virtual key.
4298 processUp(mapper);
4299 processSync(mapper);
4300
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4302 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4303 ASSERT_EQ(DEVICE_ID, args.deviceId);
4304 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4305 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4306 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4307 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4308 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4309 ASSERT_EQ(KEY_HOME, args.scanCode);
4310 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4311 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4312
4313 // Should not have sent any motions.
4314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4315}
4316
4317TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 addConfigurationProperty("touch.deviceType", "touchScreen");
4319 prepareDisplay(DISPLAY_ORIENTATION_0);
4320 prepareButtons();
4321 prepareAxes(POSITION);
4322 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004323 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324
arthurhungdcef2dc2020-08-11 14:47:50 +08004325 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326
4327 NotifyKeyArgs keyArgs;
4328
4329 // Press virtual key.
4330 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4331 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4332 processDown(mapper, x, y);
4333 processSync(mapper);
4334
4335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4336 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4337 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4338 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4339 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4340 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4341 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4342 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4343 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4345 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4346
4347 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4348 // into the display area.
4349 y -= 100;
4350 processMove(mapper, x, y);
4351 processSync(mapper);
4352
4353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4354 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4355 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4356 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4357 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4358 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4359 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4360 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4361 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4362 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4363 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4364 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4365
4366 NotifyMotionArgs motionArgs;
4367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4368 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4369 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4370 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4371 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4372 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4373 ASSERT_EQ(0, motionArgs.flags);
4374 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4375 ASSERT_EQ(0, motionArgs.buttonState);
4376 ASSERT_EQ(0, motionArgs.edgeFlags);
4377 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4378 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4381 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4382 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4383 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4384 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4385
4386 // Keep moving out of bounds. Should generate a pointer move.
4387 y -= 50;
4388 processMove(mapper, x, y);
4389 processSync(mapper);
4390
4391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4392 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4393 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4394 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4395 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4397 ASSERT_EQ(0, motionArgs.flags);
4398 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4399 ASSERT_EQ(0, motionArgs.buttonState);
4400 ASSERT_EQ(0, motionArgs.edgeFlags);
4401 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4402 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4405 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4406 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4407 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4408 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4409
4410 // Release out of bounds. Should generate a pointer up.
4411 processUp(mapper);
4412 processSync(mapper);
4413
4414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4416 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4417 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4418 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4419 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4420 ASSERT_EQ(0, motionArgs.flags);
4421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4422 ASSERT_EQ(0, motionArgs.buttonState);
4423 ASSERT_EQ(0, motionArgs.edgeFlags);
4424 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4428 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4429 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4430 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4431 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4432
4433 // Should not have sent any more keys or motions.
4434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4436}
4437
4438TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 addConfigurationProperty("touch.deviceType", "touchScreen");
4440 prepareDisplay(DISPLAY_ORIENTATION_0);
4441 prepareButtons();
4442 prepareAxes(POSITION);
4443 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004444 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445
arthurhungdcef2dc2020-08-11 14:47:50 +08004446 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
4448 NotifyMotionArgs motionArgs;
4449
4450 // Initially go down out of bounds.
4451 int32_t x = -10;
4452 int32_t y = -10;
4453 processDown(mapper, x, y);
4454 processSync(mapper);
4455
4456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4457
4458 // Move into the display area. Should generate a pointer down.
4459 x = 50;
4460 y = 75;
4461 processMove(mapper, x, y);
4462 processSync(mapper);
4463
4464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4465 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4466 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4467 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4468 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4469 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4470 ASSERT_EQ(0, motionArgs.flags);
4471 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4472 ASSERT_EQ(0, motionArgs.buttonState);
4473 ASSERT_EQ(0, motionArgs.edgeFlags);
4474 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4475 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4478 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4479 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4480 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4481 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4482
4483 // Release. Should generate a pointer up.
4484 processUp(mapper);
4485 processSync(mapper);
4486
4487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4488 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4489 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4490 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4491 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4492 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4493 ASSERT_EQ(0, motionArgs.flags);
4494 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4495 ASSERT_EQ(0, motionArgs.buttonState);
4496 ASSERT_EQ(0, motionArgs.edgeFlags);
4497 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4498 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4499 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4501 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4502 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4503 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4504 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4505
4506 // Should not have sent any more keys or motions.
4507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4509}
4510
Santos Cordonfa5cf462017-04-05 10:37:00 -07004511TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004512 addConfigurationProperty("touch.deviceType", "touchScreen");
4513 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4514
4515 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4516 prepareButtons();
4517 prepareAxes(POSITION);
4518 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004519 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004520
arthurhungdcef2dc2020-08-11 14:47:50 +08004521 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004522
4523 NotifyMotionArgs motionArgs;
4524
4525 // Down.
4526 int32_t x = 100;
4527 int32_t y = 125;
4528 processDown(mapper, x, y);
4529 processSync(mapper);
4530
4531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4532 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4533 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4534 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4535 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4536 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4537 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4538 ASSERT_EQ(0, motionArgs.flags);
4539 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4540 ASSERT_EQ(0, motionArgs.buttonState);
4541 ASSERT_EQ(0, motionArgs.edgeFlags);
4542 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4543 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4546 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4547 1, 0, 0, 0, 0, 0, 0, 0));
4548 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4549 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4550 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4551
4552 // Move.
4553 x += 50;
4554 y += 75;
4555 processMove(mapper, x, y);
4556 processSync(mapper);
4557
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4559 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4560 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4561 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4562 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4563 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4565 ASSERT_EQ(0, motionArgs.flags);
4566 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4567 ASSERT_EQ(0, motionArgs.buttonState);
4568 ASSERT_EQ(0, motionArgs.edgeFlags);
4569 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4570 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4573 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4574 1, 0, 0, 0, 0, 0, 0, 0));
4575 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4576 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4577 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4578
4579 // Up.
4580 processUp(mapper);
4581 processSync(mapper);
4582
4583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4585 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4586 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4587 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4588 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4589 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4590 ASSERT_EQ(0, motionArgs.flags);
4591 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4592 ASSERT_EQ(0, motionArgs.buttonState);
4593 ASSERT_EQ(0, motionArgs.edgeFlags);
4594 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4595 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4598 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4599 1, 0, 0, 0, 0, 0, 0, 0));
4600 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4601 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4602 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4603
4604 // Should not have sent any more keys or motions.
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4607}
4608
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 addConfigurationProperty("touch.deviceType", "touchScreen");
4611 prepareDisplay(DISPLAY_ORIENTATION_0);
4612 prepareButtons();
4613 prepareAxes(POSITION);
4614 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004615 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616
arthurhungdcef2dc2020-08-11 14:47:50 +08004617 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618
4619 NotifyMotionArgs motionArgs;
4620
4621 // Down.
4622 int32_t x = 100;
4623 int32_t y = 125;
4624 processDown(mapper, x, y);
4625 processSync(mapper);
4626
4627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4628 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4629 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4630 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4631 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4632 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4633 ASSERT_EQ(0, motionArgs.flags);
4634 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4635 ASSERT_EQ(0, motionArgs.buttonState);
4636 ASSERT_EQ(0, motionArgs.edgeFlags);
4637 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4638 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4641 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4642 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4643 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4644 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4645
4646 // Move.
4647 x += 50;
4648 y += 75;
4649 processMove(mapper, x, y);
4650 processSync(mapper);
4651
4652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4653 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4654 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4655 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4656 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4658 ASSERT_EQ(0, motionArgs.flags);
4659 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4660 ASSERT_EQ(0, motionArgs.buttonState);
4661 ASSERT_EQ(0, motionArgs.edgeFlags);
4662 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4663 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4666 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4667 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4668 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4670
4671 // Up.
4672 processUp(mapper);
4673 processSync(mapper);
4674
4675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4676 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4677 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4678 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4679 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4680 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4681 ASSERT_EQ(0, motionArgs.flags);
4682 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4683 ASSERT_EQ(0, motionArgs.buttonState);
4684 ASSERT_EQ(0, motionArgs.edgeFlags);
4685 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4686 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4689 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4690 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4691 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4692 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4693
4694 // Should not have sent any more keys or motions.
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4697}
4698
4699TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 addConfigurationProperty("touch.deviceType", "touchScreen");
4701 prepareButtons();
4702 prepareAxes(POSITION);
4703 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004704 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705
4706 NotifyMotionArgs args;
4707
4708 // Rotation 90.
4709 prepareDisplay(DISPLAY_ORIENTATION_90);
4710 processDown(mapper, toRawX(50), toRawY(75));
4711 processSync(mapper);
4712
4713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4714 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4715 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4716
4717 processUp(mapper);
4718 processSync(mapper);
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4720}
4721
4722TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723 addConfigurationProperty("touch.deviceType", "touchScreen");
4724 prepareButtons();
4725 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004726 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727
4728 NotifyMotionArgs args;
4729
4730 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004731 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732 prepareDisplay(DISPLAY_ORIENTATION_0);
4733 processDown(mapper, toRawX(50), toRawY(75));
4734 processSync(mapper);
4735
4736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4737 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4738 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4739
4740 processUp(mapper);
4741 processSync(mapper);
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4743
4744 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004745 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004746 prepareDisplay(DISPLAY_ORIENTATION_90);
4747 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4748 processSync(mapper);
4749
4750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4751 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4752 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4753
4754 processUp(mapper);
4755 processSync(mapper);
4756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4757
4758 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004759 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 prepareDisplay(DISPLAY_ORIENTATION_180);
4761 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4762 processSync(mapper);
4763
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4765 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4766 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4767
4768 processUp(mapper);
4769 processSync(mapper);
4770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4771
4772 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004773 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 prepareDisplay(DISPLAY_ORIENTATION_270);
4775 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4776 processSync(mapper);
4777
4778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4779 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4780 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4781
4782 processUp(mapper);
4783 processSync(mapper);
4784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4785}
4786
4787TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 addConfigurationProperty("touch.deviceType", "touchScreen");
4789 prepareDisplay(DISPLAY_ORIENTATION_0);
4790 prepareButtons();
4791 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004792 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793
4794 // These calculations are based on the input device calibration documentation.
4795 int32_t rawX = 100;
4796 int32_t rawY = 200;
4797 int32_t rawPressure = 10;
4798 int32_t rawToolMajor = 12;
4799 int32_t rawDistance = 2;
4800 int32_t rawTiltX = 30;
4801 int32_t rawTiltY = 110;
4802
4803 float x = toDisplayX(rawX);
4804 float y = toDisplayY(rawY);
4805 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4806 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4807 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4808 float distance = float(rawDistance);
4809
4810 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4811 float tiltScale = M_PI / 180;
4812 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4813 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4814 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4815 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4816
4817 processDown(mapper, rawX, rawY);
4818 processPressure(mapper, rawPressure);
4819 processToolMajor(mapper, rawToolMajor);
4820 processDistance(mapper, rawDistance);
4821 processTilt(mapper, rawTiltX, rawTiltY);
4822 processSync(mapper);
4823
4824 NotifyMotionArgs args;
4825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4827 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4828 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4829}
4830
Jason Gerecke489fda82012-09-07 17:19:40 -07004831TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004832 addConfigurationProperty("touch.deviceType", "touchScreen");
4833 prepareDisplay(DISPLAY_ORIENTATION_0);
4834 prepareLocationCalibration();
4835 prepareButtons();
4836 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004837 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004838
4839 int32_t rawX = 100;
4840 int32_t rawY = 200;
4841
4842 float x = toDisplayX(toCookedX(rawX, rawY));
4843 float y = toDisplayY(toCookedY(rawX, rawY));
4844
4845 processDown(mapper, rawX, rawY);
4846 processSync(mapper);
4847
4848 NotifyMotionArgs args;
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4851 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4852}
4853
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855 addConfigurationProperty("touch.deviceType", "touchScreen");
4856 prepareDisplay(DISPLAY_ORIENTATION_0);
4857 prepareButtons();
4858 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004859 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860
4861 NotifyMotionArgs motionArgs;
4862 NotifyKeyArgs keyArgs;
4863
4864 processDown(mapper, 100, 200);
4865 processSync(mapper);
4866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4867 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4868 ASSERT_EQ(0, motionArgs.buttonState);
4869
4870 // press BTN_LEFT, release BTN_LEFT
4871 processKey(mapper, BTN_LEFT, 1);
4872 processSync(mapper);
4873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4875 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4876
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4879 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4880
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881 processKey(mapper, BTN_LEFT, 0);
4882 processSync(mapper);
4883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004884 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004886
4887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004889 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890
4891 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4892 processKey(mapper, BTN_RIGHT, 1);
4893 processKey(mapper, BTN_MIDDLE, 1);
4894 processSync(mapper);
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4897 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4898 motionArgs.buttonState);
4899
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4901 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4902 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4903
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4905 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4906 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4907 motionArgs.buttonState);
4908
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909 processKey(mapper, BTN_RIGHT, 0);
4910 processSync(mapper);
4911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004914
4915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004917 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918
4919 processKey(mapper, BTN_MIDDLE, 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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928
4929 // press BTN_BACK, release BTN_BACK
4930 processKey(mapper, BTN_BACK, 1);
4931 processSync(mapper);
4932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4933 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4934 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004935
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004938 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4939
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4942 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943
4944 processKey(mapper, BTN_BACK, 0);
4945 processSync(mapper);
4946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004949
4950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004952 ASSERT_EQ(0, motionArgs.buttonState);
4953
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4955 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4956 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4957
4958 // press BTN_SIDE, release BTN_SIDE
4959 processKey(mapper, BTN_SIDE, 1);
4960 processSync(mapper);
4961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4962 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4963 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004964
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004967 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4968
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4970 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4971 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972
4973 processKey(mapper, BTN_SIDE, 0);
4974 processSync(mapper);
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004976 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004978
4979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004981 ASSERT_EQ(0, motionArgs.buttonState);
4982
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4984 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4985 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4986
4987 // press BTN_FORWARD, release BTN_FORWARD
4988 processKey(mapper, BTN_FORWARD, 1);
4989 processSync(mapper);
4990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4991 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4992 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004993
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004996 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4997
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5000 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001
5002 processKey(mapper, BTN_FORWARD, 0);
5003 processSync(mapper);
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005007
5008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005010 ASSERT_EQ(0, motionArgs.buttonState);
5011
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5013 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5014 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5015
5016 // press BTN_EXTRA, release BTN_EXTRA
5017 processKey(mapper, BTN_EXTRA, 1);
5018 processSync(mapper);
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5020 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5021 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005022
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005025 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5026
5027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5029 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030
5031 processKey(mapper, BTN_EXTRA, 0);
5032 processSync(mapper);
5033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005036
5037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005039 ASSERT_EQ(0, motionArgs.buttonState);
5040
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5042 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5043 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5044
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5046
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 // press BTN_STYLUS, release BTN_STYLUS
5048 processKey(mapper, BTN_STYLUS, 1);
5049 processSync(mapper);
5050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005052 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5053
5054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5056 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057
5058 processKey(mapper, BTN_STYLUS, 0);
5059 processSync(mapper);
5060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005063
5064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005066 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067
5068 // press BTN_STYLUS2, release BTN_STYLUS2
5069 processKey(mapper, BTN_STYLUS2, 1);
5070 processSync(mapper);
5071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005073 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5074
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5077 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078
5079 processKey(mapper, BTN_STYLUS2, 0);
5080 processSync(mapper);
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005084
5085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005087 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088
5089 // release touch
5090 processUp(mapper);
5091 processSync(mapper);
5092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5093 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5094 ASSERT_EQ(0, motionArgs.buttonState);
5095}
5096
5097TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098 addConfigurationProperty("touch.deviceType", "touchScreen");
5099 prepareDisplay(DISPLAY_ORIENTATION_0);
5100 prepareButtons();
5101 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005102 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103
5104 NotifyMotionArgs motionArgs;
5105
5106 // default tool type is finger
5107 processDown(mapper, 100, 200);
5108 processSync(mapper);
5109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5110 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5112
5113 // eraser
5114 processKey(mapper, BTN_TOOL_RUBBER, 1);
5115 processSync(mapper);
5116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5119
5120 // stylus
5121 processKey(mapper, BTN_TOOL_RUBBER, 0);
5122 processKey(mapper, BTN_TOOL_PEN, 1);
5123 processSync(mapper);
5124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5127
5128 // brush
5129 processKey(mapper, BTN_TOOL_PEN, 0);
5130 processKey(mapper, BTN_TOOL_BRUSH, 1);
5131 processSync(mapper);
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5135
5136 // pencil
5137 processKey(mapper, BTN_TOOL_BRUSH, 0);
5138 processKey(mapper, BTN_TOOL_PENCIL, 1);
5139 processSync(mapper);
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5143
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005144 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145 processKey(mapper, BTN_TOOL_PENCIL, 0);
5146 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5147 processSync(mapper);
5148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5151
5152 // mouse
5153 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5154 processKey(mapper, BTN_TOOL_MOUSE, 1);
5155 processSync(mapper);
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5158 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5159
5160 // lens
5161 processKey(mapper, BTN_TOOL_MOUSE, 0);
5162 processKey(mapper, BTN_TOOL_LENS, 1);
5163 processSync(mapper);
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5167
5168 // double-tap
5169 processKey(mapper, BTN_TOOL_LENS, 0);
5170 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5171 processSync(mapper);
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5175
5176 // triple-tap
5177 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5178 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5179 processSync(mapper);
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5183
5184 // quad-tap
5185 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5186 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5187 processSync(mapper);
5188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5191
5192 // finger
5193 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5194 processKey(mapper, BTN_TOOL_FINGER, 1);
5195 processSync(mapper);
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5199
5200 // stylus trumps finger
5201 processKey(mapper, BTN_TOOL_PEN, 1);
5202 processSync(mapper);
5203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5206
5207 // eraser trumps stylus
5208 processKey(mapper, BTN_TOOL_RUBBER, 1);
5209 processSync(mapper);
5210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5213
5214 // mouse trumps eraser
5215 processKey(mapper, BTN_TOOL_MOUSE, 1);
5216 processSync(mapper);
5217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5220
5221 // back to default tool type
5222 processKey(mapper, BTN_TOOL_MOUSE, 0);
5223 processKey(mapper, BTN_TOOL_RUBBER, 0);
5224 processKey(mapper, BTN_TOOL_PEN, 0);
5225 processKey(mapper, BTN_TOOL_FINGER, 0);
5226 processSync(mapper);
5227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5230}
5231
5232TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005233 addConfigurationProperty("touch.deviceType", "touchScreen");
5234 prepareDisplay(DISPLAY_ORIENTATION_0);
5235 prepareButtons();
5236 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005237 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005238 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239
5240 NotifyMotionArgs motionArgs;
5241
5242 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5243 processKey(mapper, BTN_TOOL_FINGER, 1);
5244 processMove(mapper, 100, 200);
5245 processSync(mapper);
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5247 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5249 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5250
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5254 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5255
5256 // move a little
5257 processMove(mapper, 150, 250);
5258 processSync(mapper);
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5260 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5262 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5263
5264 // down when BTN_TOUCH is pressed, pressure defaults to 1
5265 processKey(mapper, BTN_TOUCH, 1);
5266 processSync(mapper);
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5270 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5271
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5273 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5275 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5276
5277 // up when BTN_TOUCH is released, hover restored
5278 processKey(mapper, BTN_TOUCH, 0);
5279 processSync(mapper);
5280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5281 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5283 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5284
5285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5288 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5289
5290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5293 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5294
5295 // exit hover when pointer goes away
5296 processKey(mapper, BTN_TOOL_FINGER, 0);
5297 processSync(mapper);
5298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5299 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5301 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5302}
5303
5304TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305 addConfigurationProperty("touch.deviceType", "touchScreen");
5306 prepareDisplay(DISPLAY_ORIENTATION_0);
5307 prepareButtons();
5308 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005309 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310
5311 NotifyMotionArgs motionArgs;
5312
5313 // initially hovering because pressure is 0
5314 processDown(mapper, 100, 200);
5315 processPressure(mapper, 0);
5316 processSync(mapper);
5317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5318 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5320 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5321
5322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5325 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5326
5327 // move a little
5328 processMove(mapper, 150, 250);
5329 processSync(mapper);
5330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5333 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5334
5335 // down when pressure is non-zero
5336 processPressure(mapper, RAW_PRESSURE_MAX);
5337 processSync(mapper);
5338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5341 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5342
5343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5344 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5346 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5347
5348 // up when pressure becomes 0, hover restored
5349 processPressure(mapper, 0);
5350 processSync(mapper);
5351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5352 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5354 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5355
5356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5357 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5359 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5360
5361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5362 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5364 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5365
5366 // exit hover when pointer goes away
5367 processUp(mapper);
5368 processSync(mapper);
5369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5372 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5373}
5374
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375// --- MultiTouchInputMapperTest ---
5376
5377class MultiTouchInputMapperTest : public TouchInputMapperTest {
5378protected:
5379 void prepareAxes(int axes);
5380
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005381 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5382 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5383 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5384 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5385 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5386 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5387 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5388 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5389 void processId(MultiTouchInputMapper& mapper, int32_t id);
5390 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5391 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5392 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5393 void processMTSync(MultiTouchInputMapper& mapper);
5394 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395};
5396
5397void MultiTouchInputMapperTest::prepareAxes(int axes) {
5398 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005399 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5400 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401 }
5402 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005403 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5404 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005406 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5407 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408 }
5409 }
5410 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005411 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5412 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005414 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5415 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 }
5417 }
5418 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005419 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5420 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421 }
5422 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005423 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5424 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425 }
5426 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005427 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5428 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429 }
5430 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005431 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5432 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433 }
5434 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005435 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5436 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437 }
5438 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005439 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 }
5441}
5442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005443void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5444 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005445 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5446 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447}
5448
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005449void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5450 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005451 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452}
5453
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005454void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5455 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005456 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457}
5458
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005459void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005460 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461}
5462
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005463void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005464 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465}
5466
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005467void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5468 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005469 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470}
5471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005472void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005473 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474}
5475
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005476void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005477 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478}
5479
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005480void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005481 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482}
5483
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005484void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005485 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005486}
5487
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005488void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005489 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490}
5491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5493 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005494 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495}
5496
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005497void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005498 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499}
5500
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005501void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005502 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005503}
5504
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506 addConfigurationProperty("touch.deviceType", "touchScreen");
5507 prepareDisplay(DISPLAY_ORIENTATION_0);
5508 prepareAxes(POSITION);
5509 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005510 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511
arthurhungdcef2dc2020-08-11 14:47:50 +08005512 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513
5514 NotifyMotionArgs motionArgs;
5515
5516 // Two fingers down at once.
5517 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5518 processPosition(mapper, x1, y1);
5519 processMTSync(mapper);
5520 processPosition(mapper, x2, y2);
5521 processMTSync(mapper);
5522 processSync(mapper);
5523
5524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5525 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5526 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5527 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5528 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5529 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5530 ASSERT_EQ(0, motionArgs.flags);
5531 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5532 ASSERT_EQ(0, motionArgs.buttonState);
5533 ASSERT_EQ(0, motionArgs.edgeFlags);
5534 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5535 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].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_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5540 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5541 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5542
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5545 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5546 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5547 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5548 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5549 motionArgs.action);
5550 ASSERT_EQ(0, motionArgs.flags);
5551 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5552 ASSERT_EQ(0, motionArgs.buttonState);
5553 ASSERT_EQ(0, motionArgs.edgeFlags);
5554 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5557 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5560 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5562 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5563 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5564 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5565 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5566
5567 // Move.
5568 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5569 processPosition(mapper, x1, y1);
5570 processMTSync(mapper);
5571 processPosition(mapper, x2, y2);
5572 processMTSync(mapper);
5573 processSync(mapper);
5574
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5576 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5577 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5578 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5579 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5581 ASSERT_EQ(0, motionArgs.flags);
5582 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5583 ASSERT_EQ(0, motionArgs.buttonState);
5584 ASSERT_EQ(0, motionArgs.edgeFlags);
5585 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5586 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5588 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5591 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5593 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5594 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5595 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5596 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5597
5598 // First finger up.
5599 x2 += 15; y2 -= 20;
5600 processPosition(mapper, x2, y2);
5601 processMTSync(mapper);
5602 processSync(mapper);
5603
5604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5605 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5606 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5607 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5608 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5609 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5610 motionArgs.action);
5611 ASSERT_EQ(0, motionArgs.flags);
5612 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5613 ASSERT_EQ(0, motionArgs.buttonState);
5614 ASSERT_EQ(0, motionArgs.edgeFlags);
5615 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5616 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5618 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5621 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5623 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5624 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5625 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5626 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5627
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5629 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5630 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5631 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5632 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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(1), motionArgs.pointerCount);
5639 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5642 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5643 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5644 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5645 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5646
5647 // Move.
5648 x2 += 20; y2 -= 25;
5649 processPosition(mapper, x2, y2);
5650 processMTSync(mapper);
5651 processSync(mapper);
5652
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5655 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5656 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5657 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5659 ASSERT_EQ(0, motionArgs.flags);
5660 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5661 ASSERT_EQ(0, motionArgs.buttonState);
5662 ASSERT_EQ(0, motionArgs.edgeFlags);
5663 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5664 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5667 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5668 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5669 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5670 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5671
5672 // New finger down.
5673 int32_t x3 = 700, y3 = 300;
5674 processPosition(mapper, x2, y2);
5675 processMTSync(mapper);
5676 processPosition(mapper, x3, y3);
5677 processMTSync(mapper);
5678 processSync(mapper);
5679
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5681 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5682 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5683 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5684 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5685 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5686 motionArgs.action);
5687 ASSERT_EQ(0, motionArgs.flags);
5688 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5689 ASSERT_EQ(0, motionArgs.buttonState);
5690 ASSERT_EQ(0, motionArgs.edgeFlags);
5691 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5692 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5694 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5697 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5699 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5700 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5701 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5702 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5703
5704 // Second finger up.
5705 x3 += 30; y3 -= 20;
5706 processPosition(mapper, x3, y3);
5707 processMTSync(mapper);
5708 processSync(mapper);
5709
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5712 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5713 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5714 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5715 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5716 motionArgs.action);
5717 ASSERT_EQ(0, motionArgs.flags);
5718 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5719 ASSERT_EQ(0, motionArgs.buttonState);
5720 ASSERT_EQ(0, motionArgs.edgeFlags);
5721 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5722 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5724 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5727 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5729 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5730 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5731 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5732 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5733
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5735 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5736 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5737 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5738 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5740 ASSERT_EQ(0, motionArgs.flags);
5741 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5742 ASSERT_EQ(0, motionArgs.buttonState);
5743 ASSERT_EQ(0, motionArgs.edgeFlags);
5744 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5745 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5748 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5749 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5750 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5751 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5752
5753 // Last finger up.
5754 processMTSync(mapper);
5755 processSync(mapper);
5756
5757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5758 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5759 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5760 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5761 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5762 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5763 ASSERT_EQ(0, motionArgs.flags);
5764 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5765 ASSERT_EQ(0, motionArgs.buttonState);
5766 ASSERT_EQ(0, motionArgs.edgeFlags);
5767 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5768 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5771 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5772 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5773 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5774 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5775
5776 // Should not have sent any more keys or motions.
5777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5779}
5780
5781TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005782 addConfigurationProperty("touch.deviceType", "touchScreen");
5783 prepareDisplay(DISPLAY_ORIENTATION_0);
5784 prepareAxes(POSITION | ID);
5785 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005786 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787
arthurhungdcef2dc2020-08-11 14:47:50 +08005788 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789
5790 NotifyMotionArgs motionArgs;
5791
5792 // Two fingers down at once.
5793 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5794 processPosition(mapper, x1, y1);
5795 processId(mapper, 1);
5796 processMTSync(mapper);
5797 processPosition(mapper, x2, y2);
5798 processId(mapper, 2);
5799 processMTSync(mapper);
5800 processSync(mapper);
5801
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5803 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5804 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5805 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5806 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5808 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5809
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5811 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5812 motionArgs.action);
5813 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5814 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5815 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5816 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5819 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5821 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5822
5823 // Move.
5824 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5825 processPosition(mapper, x1, y1);
5826 processId(mapper, 1);
5827 processMTSync(mapper);
5828 processPosition(mapper, x2, y2);
5829 processId(mapper, 2);
5830 processMTSync(mapper);
5831 processSync(mapper);
5832
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5835 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5836 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5837 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5838 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5839 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5841 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5843 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5844
5845 // First finger up.
5846 x2 += 15; y2 -= 20;
5847 processPosition(mapper, x2, y2);
5848 processId(mapper, 2);
5849 processMTSync(mapper);
5850 processSync(mapper);
5851
5852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5853 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5854 motionArgs.action);
5855 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5856 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5858 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5861 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5863 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5864
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5867 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5868 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5871 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5872
5873 // Move.
5874 x2 += 20; y2 -= 25;
5875 processPosition(mapper, x2, y2);
5876 processId(mapper, 2);
5877 processMTSync(mapper);
5878 processSync(mapper);
5879
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5882 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5883 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5886 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5887
5888 // New finger down.
5889 int32_t x3 = 700, y3 = 300;
5890 processPosition(mapper, x2, y2);
5891 processId(mapper, 2);
5892 processMTSync(mapper);
5893 processPosition(mapper, x3, y3);
5894 processId(mapper, 3);
5895 processMTSync(mapper);
5896 processSync(mapper);
5897
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5899 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5900 motionArgs.action);
5901 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5902 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5903 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5904 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5905 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5907 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5909 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5910
5911 // Second finger up.
5912 x3 += 30; y3 -= 20;
5913 processPosition(mapper, x3, y3);
5914 processId(mapper, 3);
5915 processMTSync(mapper);
5916 processSync(mapper);
5917
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5919 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5920 motionArgs.action);
5921 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5922 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5923 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5924 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5927 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5929 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5930
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5933 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5934 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5937 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5938
5939 // Last finger up.
5940 processMTSync(mapper);
5941 processSync(mapper);
5942
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5945 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5946 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5949 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5950
5951 // Should not have sent any more keys or motions.
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5954}
5955
5956TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957 addConfigurationProperty("touch.deviceType", "touchScreen");
5958 prepareDisplay(DISPLAY_ORIENTATION_0);
5959 prepareAxes(POSITION | ID | SLOT);
5960 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005961 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005962
arthurhungdcef2dc2020-08-11 14:47:50 +08005963 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005964
5965 NotifyMotionArgs motionArgs;
5966
5967 // Two fingers down at once.
5968 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5969 processPosition(mapper, x1, y1);
5970 processId(mapper, 1);
5971 processSlot(mapper, 1);
5972 processPosition(mapper, x2, y2);
5973 processId(mapper, 2);
5974 processSync(mapper);
5975
5976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5977 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5978 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5979 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5982 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5983
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5985 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << 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(x1), toDisplayY(y1), 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 // Move.
5998 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5999 processSlot(mapper, 0);
6000 processPosition(mapper, x1, y1);
6001 processSlot(mapper, 1);
6002 processPosition(mapper, x2, y2);
6003 processSync(mapper);
6004
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6007 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6008 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6010 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6013 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6015 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6016
6017 // First finger up.
6018 x2 += 15; y2 -= 20;
6019 processSlot(mapper, 0);
6020 processId(mapper, -1);
6021 processSlot(mapper, 1);
6022 processPosition(mapper, x2, y2);
6023 processSync(mapper);
6024
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6027 motionArgs.action);
6028 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6029 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6031 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6034 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6036 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6037
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6040 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6041 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6044 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6045
6046 // Move.
6047 x2 += 20; y2 -= 25;
6048 processPosition(mapper, x2, y2);
6049 processSync(mapper);
6050
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6053 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6054 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6057 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6058
6059 // New finger down.
6060 int32_t x3 = 700, y3 = 300;
6061 processPosition(mapper, x2, y2);
6062 processSlot(mapper, 0);
6063 processId(mapper, 3);
6064 processPosition(mapper, x3, y3);
6065 processSync(mapper);
6066
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6068 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6069 motionArgs.action);
6070 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6071 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6073 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6076 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6078 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6079
6080 // Second finger up.
6081 x3 += 30; y3 -= 20;
6082 processSlot(mapper, 1);
6083 processId(mapper, -1);
6084 processSlot(mapper, 0);
6085 processPosition(mapper, x3, y3);
6086 processSync(mapper);
6087
6088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6089 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6090 motionArgs.action);
6091 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6092 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6094 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6095 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6097 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6099 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6100
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6103 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6104 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6107 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6108
6109 // Last finger up.
6110 processId(mapper, -1);
6111 processSync(mapper);
6112
6113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6114 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6115 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6116 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6119 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6120
6121 // Should not have sent any more keys or motions.
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6124}
6125
6126TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127 addConfigurationProperty("touch.deviceType", "touchScreen");
6128 prepareDisplay(DISPLAY_ORIENTATION_0);
6129 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006130 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131
6132 // These calculations are based on the input device calibration documentation.
6133 int32_t rawX = 100;
6134 int32_t rawY = 200;
6135 int32_t rawTouchMajor = 7;
6136 int32_t rawTouchMinor = 6;
6137 int32_t rawToolMajor = 9;
6138 int32_t rawToolMinor = 8;
6139 int32_t rawPressure = 11;
6140 int32_t rawDistance = 0;
6141 int32_t rawOrientation = 3;
6142 int32_t id = 5;
6143
6144 float x = toDisplayX(rawX);
6145 float y = toDisplayY(rawY);
6146 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6147 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6148 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6149 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6150 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6151 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6152 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6153 float distance = float(rawDistance);
6154
6155 processPosition(mapper, rawX, rawY);
6156 processTouchMajor(mapper, rawTouchMajor);
6157 processTouchMinor(mapper, rawTouchMinor);
6158 processToolMajor(mapper, rawToolMajor);
6159 processToolMinor(mapper, rawToolMinor);
6160 processPressure(mapper, rawPressure);
6161 processOrientation(mapper, rawOrientation);
6162 processDistance(mapper, rawDistance);
6163 processId(mapper, id);
6164 processMTSync(mapper);
6165 processSync(mapper);
6166
6167 NotifyMotionArgs args;
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6169 ASSERT_EQ(0, args.pointerProperties[0].id);
6170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6171 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6172 orientation, distance));
6173}
6174
6175TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176 addConfigurationProperty("touch.deviceType", "touchScreen");
6177 prepareDisplay(DISPLAY_ORIENTATION_0);
6178 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6179 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006180 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181
6182 // These calculations are based on the input device calibration documentation.
6183 int32_t rawX = 100;
6184 int32_t rawY = 200;
6185 int32_t rawTouchMajor = 140;
6186 int32_t rawTouchMinor = 120;
6187 int32_t rawToolMajor = 180;
6188 int32_t rawToolMinor = 160;
6189
6190 float x = toDisplayX(rawX);
6191 float y = toDisplayY(rawY);
6192 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6193 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6194 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6195 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6196 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6197
6198 processPosition(mapper, rawX, rawY);
6199 processTouchMajor(mapper, rawTouchMajor);
6200 processTouchMinor(mapper, rawTouchMinor);
6201 processToolMajor(mapper, rawToolMajor);
6202 processToolMinor(mapper, rawToolMinor);
6203 processMTSync(mapper);
6204 processSync(mapper);
6205
6206 NotifyMotionArgs args;
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6209 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6210}
6211
6212TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213 addConfigurationProperty("touch.deviceType", "touchScreen");
6214 prepareDisplay(DISPLAY_ORIENTATION_0);
6215 prepareAxes(POSITION | TOUCH | TOOL);
6216 addConfigurationProperty("touch.size.calibration", "diameter");
6217 addConfigurationProperty("touch.size.scale", "10");
6218 addConfigurationProperty("touch.size.bias", "160");
6219 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006220 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221
6222 // These calculations are based on the input device calibration documentation.
6223 // Note: We only provide a single common touch/tool value because the device is assumed
6224 // not to emit separate values for each pointer (isSummed = 1).
6225 int32_t rawX = 100;
6226 int32_t rawY = 200;
6227 int32_t rawX2 = 150;
6228 int32_t rawY2 = 250;
6229 int32_t rawTouchMajor = 5;
6230 int32_t rawToolMajor = 8;
6231
6232 float x = toDisplayX(rawX);
6233 float y = toDisplayY(rawY);
6234 float x2 = toDisplayX(rawX2);
6235 float y2 = toDisplayY(rawY2);
6236 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6237 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6238 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6239
6240 processPosition(mapper, rawX, rawY);
6241 processTouchMajor(mapper, rawTouchMajor);
6242 processToolMajor(mapper, rawToolMajor);
6243 processMTSync(mapper);
6244 processPosition(mapper, rawX2, rawY2);
6245 processTouchMajor(mapper, rawTouchMajor);
6246 processToolMajor(mapper, rawToolMajor);
6247 processMTSync(mapper);
6248 processSync(mapper);
6249
6250 NotifyMotionArgs args;
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6252 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6253
6254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6255 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6256 args.action);
6257 ASSERT_EQ(size_t(2), args.pointerCount);
6258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6259 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6261 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6262}
6263
6264TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006265 addConfigurationProperty("touch.deviceType", "touchScreen");
6266 prepareDisplay(DISPLAY_ORIENTATION_0);
6267 prepareAxes(POSITION | TOUCH | TOOL);
6268 addConfigurationProperty("touch.size.calibration", "area");
6269 addConfigurationProperty("touch.size.scale", "43");
6270 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006271 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272
6273 // These calculations are based on the input device calibration documentation.
6274 int32_t rawX = 100;
6275 int32_t rawY = 200;
6276 int32_t rawTouchMajor = 5;
6277 int32_t rawToolMajor = 8;
6278
6279 float x = toDisplayX(rawX);
6280 float y = toDisplayY(rawY);
6281 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6282 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6283 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6284
6285 processPosition(mapper, rawX, rawY);
6286 processTouchMajor(mapper, rawTouchMajor);
6287 processToolMajor(mapper, rawToolMajor);
6288 processMTSync(mapper);
6289 processSync(mapper);
6290
6291 NotifyMotionArgs args;
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6294 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6295}
6296
6297TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 addConfigurationProperty("touch.deviceType", "touchScreen");
6299 prepareDisplay(DISPLAY_ORIENTATION_0);
6300 prepareAxes(POSITION | PRESSURE);
6301 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6302 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006303 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304
Michael Wrightaa449c92017-12-13 21:21:43 +00006305 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006306 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006307 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6308 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6309 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6310
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 // These calculations are based on the input device calibration documentation.
6312 int32_t rawX = 100;
6313 int32_t rawY = 200;
6314 int32_t rawPressure = 60;
6315
6316 float x = toDisplayX(rawX);
6317 float y = toDisplayY(rawY);
6318 float pressure = float(rawPressure) * 0.01f;
6319
6320 processPosition(mapper, rawX, rawY);
6321 processPressure(mapper, rawPressure);
6322 processMTSync(mapper);
6323 processSync(mapper);
6324
6325 NotifyMotionArgs args;
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6328 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6329}
6330
6331TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006332 addConfigurationProperty("touch.deviceType", "touchScreen");
6333 prepareDisplay(DISPLAY_ORIENTATION_0);
6334 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006335 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336
6337 NotifyMotionArgs motionArgs;
6338 NotifyKeyArgs keyArgs;
6339
6340 processId(mapper, 1);
6341 processPosition(mapper, 100, 200);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6344 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6345 ASSERT_EQ(0, motionArgs.buttonState);
6346
6347 // press BTN_LEFT, release BTN_LEFT
6348 processKey(mapper, BTN_LEFT, 1);
6349 processSync(mapper);
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6352 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6353
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6355 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6356 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6357
Michael Wrightd02c5b62014-02-10 15:10:22 -08006358 processKey(mapper, BTN_LEFT, 0);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006361 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006366 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367
6368 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6369 processKey(mapper, BTN_RIGHT, 1);
6370 processKey(mapper, BTN_MIDDLE, 1);
6371 processSync(mapper);
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6374 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6375 motionArgs.buttonState);
6376
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6379 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6380
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6383 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6384 motionArgs.buttonState);
6385
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 processKey(mapper, BTN_RIGHT, 0);
6387 processSync(mapper);
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006394 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395
6396 processKey(mapper, BTN_MIDDLE, 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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405
6406 // press BTN_BACK, release BTN_BACK
6407 processKey(mapper, BTN_BACK, 1);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6410 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6411 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006412
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006415 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6416
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6419 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006420
6421 processKey(mapper, BTN_BACK, 0);
6422 processSync(mapper);
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006424 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006426
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429 ASSERT_EQ(0, motionArgs.buttonState);
6430
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6432 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6433 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6434
6435 // press BTN_SIDE, release BTN_SIDE
6436 processKey(mapper, BTN_SIDE, 1);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6439 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6440 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006441
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006444 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6445
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6448 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006449
6450 processKey(mapper, BTN_SIDE, 0);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006453 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006455
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006458 ASSERT_EQ(0, motionArgs.buttonState);
6459
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6461 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6462 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6463
6464 // press BTN_FORWARD, release BTN_FORWARD
6465 processKey(mapper, BTN_FORWARD, 1);
6466 processSync(mapper);
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6468 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6469 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006470
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006473 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6474
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6477 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478
6479 processKey(mapper, BTN_FORWARD, 0);
6480 processSync(mapper);
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006482 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006484
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006487 ASSERT_EQ(0, motionArgs.buttonState);
6488
Michael Wrightd02c5b62014-02-10 15:10:22 -08006489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6490 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6491 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6492
6493 // press BTN_EXTRA, release BTN_EXTRA
6494 processKey(mapper, BTN_EXTRA, 1);
6495 processSync(mapper);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6497 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6498 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006499
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006502 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6503
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6506 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006507
6508 processKey(mapper, BTN_EXTRA, 0);
6509 processSync(mapper);
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006511 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006512 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006513
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006516 ASSERT_EQ(0, motionArgs.buttonState);
6517
Michael Wrightd02c5b62014-02-10 15:10:22 -08006518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6519 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6520 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6521
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6523
Michael Wrightd02c5b62014-02-10 15:10:22 -08006524 // press BTN_STYLUS, release BTN_STYLUS
6525 processKey(mapper, BTN_STYLUS, 1);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006529 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6530
6531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6532 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6533 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006534
6535 processKey(mapper, BTN_STYLUS, 0);
6536 processSync(mapper);
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006538 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006540
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006543 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006544
6545 // press BTN_STYLUS2, release BTN_STYLUS2
6546 processKey(mapper, BTN_STYLUS2, 1);
6547 processSync(mapper);
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006550 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6551
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6553 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6554 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006555
6556 processKey(mapper, BTN_STYLUS2, 0);
6557 processSync(mapper);
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006560 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006561
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006564 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006565
6566 // release touch
6567 processId(mapper, -1);
6568 processSync(mapper);
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6570 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6571 ASSERT_EQ(0, motionArgs.buttonState);
6572}
6573
6574TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006575 addConfigurationProperty("touch.deviceType", "touchScreen");
6576 prepareDisplay(DISPLAY_ORIENTATION_0);
6577 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006578 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006579
6580 NotifyMotionArgs motionArgs;
6581
6582 // default tool type is finger
6583 processId(mapper, 1);
6584 processPosition(mapper, 100, 200);
6585 processSync(mapper);
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6587 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6589
6590 // eraser
6591 processKey(mapper, BTN_TOOL_RUBBER, 1);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6596
6597 // stylus
6598 processKey(mapper, BTN_TOOL_RUBBER, 0);
6599 processKey(mapper, BTN_TOOL_PEN, 1);
6600 processSync(mapper);
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6604
6605 // brush
6606 processKey(mapper, BTN_TOOL_PEN, 0);
6607 processKey(mapper, BTN_TOOL_BRUSH, 1);
6608 processSync(mapper);
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6612
6613 // pencil
6614 processKey(mapper, BTN_TOOL_BRUSH, 0);
6615 processKey(mapper, BTN_TOOL_PENCIL, 1);
6616 processSync(mapper);
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6620
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006621 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006622 processKey(mapper, BTN_TOOL_PENCIL, 0);
6623 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
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 // mouse
6630 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6631 processKey(mapper, BTN_TOOL_MOUSE, 1);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6636
6637 // lens
6638 processKey(mapper, BTN_TOOL_MOUSE, 0);
6639 processKey(mapper, BTN_TOOL_LENS, 1);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6644
6645 // double-tap
6646 processKey(mapper, BTN_TOOL_LENS, 0);
6647 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6648 processSync(mapper);
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6652
6653 // triple-tap
6654 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6655 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6656 processSync(mapper);
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6660
6661 // quad-tap
6662 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6663 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6664 processSync(mapper);
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6668
6669 // finger
6670 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6671 processKey(mapper, BTN_TOOL_FINGER, 1);
6672 processSync(mapper);
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6676
6677 // stylus trumps finger
6678 processKey(mapper, BTN_TOOL_PEN, 1);
6679 processSync(mapper);
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6683
6684 // eraser trumps stylus
6685 processKey(mapper, BTN_TOOL_RUBBER, 1);
6686 processSync(mapper);
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6690
6691 // mouse trumps eraser
6692 processKey(mapper, BTN_TOOL_MOUSE, 1);
6693 processSync(mapper);
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6697
6698 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6699 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6700 processSync(mapper);
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6704
6705 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6706 processToolType(mapper, MT_TOOL_PEN);
6707 processSync(mapper);
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6711
6712 // back to default tool type
6713 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6714 processKey(mapper, BTN_TOOL_MOUSE, 0);
6715 processKey(mapper, BTN_TOOL_RUBBER, 0);
6716 processKey(mapper, BTN_TOOL_PEN, 0);
6717 processKey(mapper, BTN_TOOL_FINGER, 0);
6718 processSync(mapper);
6719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6722}
6723
6724TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006725 addConfigurationProperty("touch.deviceType", "touchScreen");
6726 prepareDisplay(DISPLAY_ORIENTATION_0);
6727 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006728 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006729 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006730
6731 NotifyMotionArgs motionArgs;
6732
6733 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6734 processId(mapper, 1);
6735 processPosition(mapper, 100, 200);
6736 processSync(mapper);
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6738 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6740 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6741
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6743 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6745 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6746
6747 // move a little
6748 processPosition(mapper, 150, 250);
6749 processSync(mapper);
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6751 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6753 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6754
6755 // down when BTN_TOUCH is pressed, pressure defaults to 1
6756 processKey(mapper, BTN_TOUCH, 1);
6757 processSync(mapper);
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6759 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6761 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6762
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6766 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6767
6768 // up when BTN_TOUCH is released, hover restored
6769 processKey(mapper, BTN_TOUCH, 0);
6770 processSync(mapper);
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6772 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6774 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6775
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6777 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6779 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6780
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6782 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6784 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6785
6786 // exit hover when pointer goes away
6787 processId(mapper, -1);
6788 processSync(mapper);
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6790 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6792 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6793}
6794
6795TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006796 addConfigurationProperty("touch.deviceType", "touchScreen");
6797 prepareDisplay(DISPLAY_ORIENTATION_0);
6798 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006799 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800
6801 NotifyMotionArgs motionArgs;
6802
6803 // initially hovering because pressure is 0
6804 processId(mapper, 1);
6805 processPosition(mapper, 100, 200);
6806 processPressure(mapper, 0);
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6809 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6811 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6812
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6814 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6816 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6817
6818 // move a little
6819 processPosition(mapper, 150, 250);
6820 processSync(mapper);
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6822 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6824 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6825
6826 // down when pressure becomes non-zero
6827 processPressure(mapper, RAW_PRESSURE_MAX);
6828 processSync(mapper);
6829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6830 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6832 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6833
6834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6835 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6837 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6838
6839 // up when pressure becomes 0, hover restored
6840 processPressure(mapper, 0);
6841 processSync(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6843 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6845 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6846
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6848 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6850 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6851
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6853 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6855 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6856
6857 // exit hover when pointer goes away
6858 processId(mapper, -1);
6859 processSync(mapper);
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6861 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6863 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6864}
6865
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006866/**
6867 * Set the input device port <--> display port associations, and check that the
6868 * events are routed to the display that matches the display port.
6869 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6870 */
6871TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006872 const std::string usb2 = "USB2";
6873 const uint8_t hdmi1 = 0;
6874 const uint8_t hdmi2 = 1;
6875 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006876 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006877
6878 addConfigurationProperty("touch.deviceType", "touchScreen");
6879 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006880 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006881
6882 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6883 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6884
6885 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6886 // for this input device is specified, and the matching viewport is not present,
6887 // the input device should be disabled (at the mapper level).
6888
6889 // Add viewport for display 2 on hdmi2
6890 prepareSecondaryDisplay(type, hdmi2);
6891 // Send a touch event
6892 processPosition(mapper, 100, 100);
6893 processSync(mapper);
6894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6895
6896 // Add viewport for display 1 on hdmi1
6897 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6898 // Send a touch event again
6899 processPosition(mapper, 100, 100);
6900 processSync(mapper);
6901
6902 NotifyMotionArgs args;
6903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6904 ASSERT_EQ(DISPLAY_ID, args.displayId);
6905}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006906
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006907TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006908 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006909 std::shared_ptr<FakePointerController> fakePointerController =
6910 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006911 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006912 fakePointerController->setPosition(100, 200);
6913 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006914 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6915
Garfield Tan888a6a42020-01-09 11:39:16 -08006916 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006917 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006918
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006919 prepareDisplay(DISPLAY_ORIENTATION_0);
6920 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006921 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006922
6923 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006924 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006925
6926 NotifyMotionArgs motionArgs;
6927 processPosition(mapper, 100, 100);
6928 processSync(mapper);
6929
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6931 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6932 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6933}
6934
Arthur Hung7c645402019-01-25 17:45:42 +08006935TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6936 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006937 prepareAxes(POSITION | ID | SLOT);
6938 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006939 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006940
6941 // Create the second touch screen device, and enable multi fingers.
6942 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08006943 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006944 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006945 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08006946 std::shared_ptr<InputDevice> device2 =
6947 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
6948 Flags<InputDeviceClass>(0));
6949
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006950 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6951 0 /*flat*/, 0 /*fuzz*/);
6952 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6953 0 /*flat*/, 0 /*fuzz*/);
6954 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6955 0 /*flat*/, 0 /*fuzz*/);
6956 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6957 0 /*flat*/, 0 /*fuzz*/);
6958 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6959 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6960 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006961
6962 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006963 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006964 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6965 device2->reset(ARBITRARY_TIME);
6966
6967 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006968 std::shared_ptr<FakePointerController> fakePointerController =
6969 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006970 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6971 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6972
6973 // Setup policy for associated displays and show touches.
6974 const uint8_t hdmi1 = 0;
6975 const uint8_t hdmi2 = 1;
6976 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6977 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6978 mFakePolicy->setShowTouches(true);
6979
6980 // Create displays.
6981 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006982 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006983
6984 // Default device will reconfigure above, need additional reconfiguration for another device.
6985 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006986 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08006987
6988 // Two fingers down at default display.
6989 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6990 processPosition(mapper, x1, y1);
6991 processId(mapper, 1);
6992 processSlot(mapper, 1);
6993 processPosition(mapper, x2, y2);
6994 processId(mapper, 2);
6995 processSync(mapper);
6996
6997 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6998 fakePointerController->getSpots().find(DISPLAY_ID);
6999 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7000 ASSERT_EQ(size_t(2), iter->second.size());
7001
7002 // Two fingers down at second display.
7003 processPosition(mapper2, x1, y1);
7004 processId(mapper2, 1);
7005 processSlot(mapper2, 1);
7006 processPosition(mapper2, x2, y2);
7007 processId(mapper2, 2);
7008 processSync(mapper2);
7009
7010 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7011 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7012 ASSERT_EQ(size_t(2), iter->second.size());
7013}
7014
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007015TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007016 prepareAxes(POSITION);
7017 addConfigurationProperty("touch.deviceType", "touchScreen");
7018 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007019 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007020
7021 NotifyMotionArgs motionArgs;
7022 // Unrotated video frame
7023 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7024 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007025 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007026 processPosition(mapper, 100, 200);
7027 processSync(mapper);
7028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7029 ASSERT_EQ(frames, motionArgs.videoFrames);
7030
7031 // Subsequent touch events should not have any videoframes
7032 // This is implemented separately in FakeEventHub,
7033 // but that should match the behaviour of TouchVideoDevice.
7034 processPosition(mapper, 200, 200);
7035 processSync(mapper);
7036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7037 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7038}
7039
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007040TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007041 prepareAxes(POSITION);
7042 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007043 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007044 // Unrotated video frame
7045 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7046 NotifyMotionArgs motionArgs;
7047
7048 // Test all 4 orientations
7049 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7050 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7051 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7052 clearViewports();
7053 prepareDisplay(orientation);
7054 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007055 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007056 processPosition(mapper, 100, 200);
7057 processSync(mapper);
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7059 frames[0].rotate(orientation);
7060 ASSERT_EQ(frames, motionArgs.videoFrames);
7061 }
7062}
7063
7064TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007065 prepareAxes(POSITION);
7066 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007067 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007068 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7069 // so mix these.
7070 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7071 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7072 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7073 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7074 NotifyMotionArgs motionArgs;
7075
7076 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007077 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007078 processPosition(mapper, 100, 200);
7079 processSync(mapper);
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7081 std::for_each(frames.begin(), frames.end(),
7082 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7083 ASSERT_EQ(frames, motionArgs.videoFrames);
7084}
7085
Arthur Hung9da14732019-09-02 16:16:58 +08007086/**
7087 * If we had defined port associations, but the viewport is not ready, the touch device would be
7088 * expected to be disabled, and it should be enabled after the viewport has found.
7089 */
7090TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007091 constexpr uint8_t hdmi2 = 1;
7092 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007093 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007094
7095 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7096
7097 addConfigurationProperty("touch.deviceType", "touchScreen");
7098 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007099 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007100
7101 ASSERT_EQ(mDevice->isEnabled(), false);
7102
7103 // Add display on hdmi2, the device should be enabled and can receive touch event.
7104 prepareSecondaryDisplay(type, hdmi2);
7105 ASSERT_EQ(mDevice->isEnabled(), true);
7106
7107 // Send a touch event.
7108 processPosition(mapper, 100, 100);
7109 processSync(mapper);
7110
7111 NotifyMotionArgs args;
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7113 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7114}
7115
Arthur Hung6cd19a42019-08-30 19:04:12 +08007116
Arthur Hung6cd19a42019-08-30 19:04:12 +08007117
Arthur Hung421eb1c2020-01-16 00:09:42 +08007118TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007119 addConfigurationProperty("touch.deviceType", "touchScreen");
7120 prepareDisplay(DISPLAY_ORIENTATION_0);
7121 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007122 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007123
7124 NotifyMotionArgs motionArgs;
7125
7126 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7127 // finger down
7128 processId(mapper, 1);
7129 processPosition(mapper, x1, y1);
7130 processSync(mapper);
7131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7132 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7134
7135 // finger move
7136 processId(mapper, 1);
7137 processPosition(mapper, x2, y2);
7138 processSync(mapper);
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7142
7143 // finger up.
7144 processId(mapper, -1);
7145 processSync(mapper);
7146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7147 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7149
7150 // new finger down
7151 processId(mapper, 1);
7152 processPosition(mapper, x3, y3);
7153 processSync(mapper);
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7155 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7157}
7158
7159/**
arthurhungcc7f9802020-04-30 17:55:40 +08007160 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7161 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007162 */
arthurhungcc7f9802020-04-30 17:55:40 +08007163TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007164 addConfigurationProperty("touch.deviceType", "touchScreen");
7165 prepareDisplay(DISPLAY_ORIENTATION_0);
7166 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007167 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007168
7169 NotifyMotionArgs motionArgs;
7170
7171 // default tool type is finger
7172 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007173 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007174 processPosition(mapper, x1, y1);
7175 processSync(mapper);
7176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7177 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7179
7180 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7181 processToolType(mapper, MT_TOOL_PALM);
7182 processSync(mapper);
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7184 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7185
7186 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007187 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007188 processPosition(mapper, x2, y2);
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7191
7192 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007193 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007194 processSync(mapper);
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7196
7197 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007198 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007199 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007200 processPosition(mapper, x3, y3);
7201 processSync(mapper);
7202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7203 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7205}
7206
arthurhungbf89a482020-04-17 17:37:55 +08007207/**
arthurhungcc7f9802020-04-30 17:55:40 +08007208 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7209 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007210 */
arthurhungcc7f9802020-04-30 17:55:40 +08007211TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007212 addConfigurationProperty("touch.deviceType", "touchScreen");
7213 prepareDisplay(DISPLAY_ORIENTATION_0);
7214 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7215 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7216
7217 NotifyMotionArgs motionArgs;
7218
7219 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007220 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7221 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007222 processPosition(mapper, x1, y1);
7223 processSync(mapper);
7224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7225 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7227
7228 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007229 processSlot(mapper, SECOND_SLOT);
7230 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007231 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007232 processSync(mapper);
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7234 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7235 motionArgs.action);
7236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7237
7238 // If the tool type of the first finger changes to MT_TOOL_PALM,
7239 // we expect to receive ACTION_POINTER_UP with cancel flag.
7240 processSlot(mapper, FIRST_SLOT);
7241 processId(mapper, FIRST_TRACKING_ID);
7242 processToolType(mapper, MT_TOOL_PALM);
7243 processSync(mapper);
7244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7245 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7246 motionArgs.action);
7247 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7248
7249 // The following MOVE events of second finger should be processed.
7250 processSlot(mapper, SECOND_SLOT);
7251 processId(mapper, SECOND_TRACKING_ID);
7252 processPosition(mapper, x2 + 1, y2 + 1);
7253 processSync(mapper);
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7256 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7257
7258 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7259 // it. Second finger receive move.
7260 processSlot(mapper, FIRST_SLOT);
7261 processId(mapper, INVALID_TRACKING_ID);
7262 processSync(mapper);
7263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7265 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7266
7267 // Second finger keeps moving.
7268 processSlot(mapper, SECOND_SLOT);
7269 processId(mapper, SECOND_TRACKING_ID);
7270 processPosition(mapper, x2 + 2, y2 + 2);
7271 processSync(mapper);
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7274 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7275
7276 // Second finger up.
7277 processId(mapper, INVALID_TRACKING_ID);
7278 processSync(mapper);
7279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7280 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7281 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7282}
7283
7284/**
7285 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7286 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7287 */
7288TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7289 addConfigurationProperty("touch.deviceType", "touchScreen");
7290 prepareDisplay(DISPLAY_ORIENTATION_0);
7291 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7292 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7293
7294 NotifyMotionArgs motionArgs;
7295
7296 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7297 // First finger down.
7298 processId(mapper, FIRST_TRACKING_ID);
7299 processPosition(mapper, x1, y1);
7300 processSync(mapper);
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7302 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7304
7305 // Second finger down.
7306 processSlot(mapper, SECOND_SLOT);
7307 processId(mapper, SECOND_TRACKING_ID);
7308 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007309 processSync(mapper);
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7311 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7312 motionArgs.action);
7313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7314
arthurhungcc7f9802020-04-30 17:55:40 +08007315 // If the tool type of the first finger changes to MT_TOOL_PALM,
7316 // we expect to receive ACTION_POINTER_UP with cancel flag.
7317 processSlot(mapper, FIRST_SLOT);
7318 processId(mapper, FIRST_TRACKING_ID);
7319 processToolType(mapper, MT_TOOL_PALM);
7320 processSync(mapper);
7321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7322 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7323 motionArgs.action);
7324 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7325
7326 // Second finger keeps moving.
7327 processSlot(mapper, SECOND_SLOT);
7328 processId(mapper, SECOND_TRACKING_ID);
7329 processPosition(mapper, x2 + 1, y2 + 1);
7330 processSync(mapper);
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7333
7334 // second finger becomes palm, receive cancel due to only 1 finger is active.
7335 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007336 processToolType(mapper, MT_TOOL_PALM);
7337 processSync(mapper);
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7339 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7340
arthurhungcc7f9802020-04-30 17:55:40 +08007341 // third finger down.
7342 processSlot(mapper, THIRD_SLOT);
7343 processId(mapper, THIRD_TRACKING_ID);
7344 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007345 processPosition(mapper, x3, y3);
7346 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7348 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007350 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7351
7352 // third finger move
7353 processId(mapper, THIRD_TRACKING_ID);
7354 processPosition(mapper, x3 + 1, y3 + 1);
7355 processSync(mapper);
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7358
7359 // first finger up, third finger receive move.
7360 processSlot(mapper, FIRST_SLOT);
7361 processId(mapper, INVALID_TRACKING_ID);
7362 processSync(mapper);
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7365 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7366
7367 // second finger up, third finger receive move.
7368 processSlot(mapper, SECOND_SLOT);
7369 processId(mapper, INVALID_TRACKING_ID);
7370 processSync(mapper);
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7373 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7374
7375 // third finger up.
7376 processSlot(mapper, THIRD_SLOT);
7377 processId(mapper, INVALID_TRACKING_ID);
7378 processSync(mapper);
7379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7380 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7381 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7382}
7383
7384/**
7385 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7386 * and the active finger could still be allowed to receive the events
7387 */
7388TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7389 addConfigurationProperty("touch.deviceType", "touchScreen");
7390 prepareDisplay(DISPLAY_ORIENTATION_0);
7391 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7392 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7393
7394 NotifyMotionArgs motionArgs;
7395
7396 // default tool type is finger
7397 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7398 processId(mapper, FIRST_TRACKING_ID);
7399 processPosition(mapper, x1, y1);
7400 processSync(mapper);
7401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7402 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7404
7405 // Second finger down.
7406 processSlot(mapper, SECOND_SLOT);
7407 processId(mapper, SECOND_TRACKING_ID);
7408 processPosition(mapper, x2, y2);
7409 processSync(mapper);
7410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7411 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7412 motionArgs.action);
7413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7414
7415 // If the tool type of the second finger changes to MT_TOOL_PALM,
7416 // we expect to receive ACTION_POINTER_UP with cancel flag.
7417 processId(mapper, SECOND_TRACKING_ID);
7418 processToolType(mapper, MT_TOOL_PALM);
7419 processSync(mapper);
7420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7421 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7422 motionArgs.action);
7423 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7424
7425 // The following MOVE event should be processed.
7426 processSlot(mapper, FIRST_SLOT);
7427 processId(mapper, FIRST_TRACKING_ID);
7428 processPosition(mapper, x1 + 1, y1 + 1);
7429 processSync(mapper);
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7432 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7433
7434 // second finger up.
7435 processSlot(mapper, SECOND_SLOT);
7436 processId(mapper, INVALID_TRACKING_ID);
7437 processSync(mapper);
7438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7440
7441 // first finger keep moving
7442 processSlot(mapper, FIRST_SLOT);
7443 processId(mapper, FIRST_TRACKING_ID);
7444 processPosition(mapper, x1 + 2, y1 + 2);
7445 processSync(mapper);
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7448
7449 // first finger up.
7450 processId(mapper, INVALID_TRACKING_ID);
7451 processSync(mapper);
7452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7453 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7454 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007455}
7456
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007457// --- MultiTouchInputMapperTest_ExternalDevice ---
7458
7459class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7460protected:
7461 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07007462 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007463 }
7464};
7465
7466/**
7467 * Expect fallback to internal viewport if device is external and external viewport is not present.
7468 */
7469TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7470 prepareAxes(POSITION);
7471 addConfigurationProperty("touch.deviceType", "touchScreen");
7472 prepareDisplay(DISPLAY_ORIENTATION_0);
7473 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7474
7475 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7476
7477 NotifyMotionArgs motionArgs;
7478
7479 // Expect the event to be sent to the internal viewport,
7480 // because an external viewport is not present.
7481 processPosition(mapper, 100, 100);
7482 processSync(mapper);
7483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7484 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7485
7486 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007487 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007488 processPosition(mapper, 100, 100);
7489 processSync(mapper);
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7491 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7492}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007493
7494/**
7495 * Test touch should not work if outside of surface.
7496 */
7497class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7498protected:
7499 void halfDisplayToCenterHorizontal(int32_t orientation) {
7500 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007501 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007502
7503 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7504 internalViewport->orientation = orientation;
7505 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7506 internalViewport->logicalLeft = 0;
7507 internalViewport->logicalTop = 0;
7508 internalViewport->logicalRight = DISPLAY_HEIGHT;
7509 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7510
7511 internalViewport->physicalLeft = 0;
7512 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7513 internalViewport->physicalRight = DISPLAY_HEIGHT;
7514 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7515
7516 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7517 internalViewport->deviceHeight = DISPLAY_WIDTH;
7518 } else {
7519 internalViewport->logicalLeft = 0;
7520 internalViewport->logicalTop = 0;
7521 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7522 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7523
7524 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7525 internalViewport->physicalTop = 0;
7526 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7527 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7528
7529 internalViewport->deviceWidth = DISPLAY_WIDTH;
7530 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7531 }
7532
7533 mFakePolicy->updateViewport(internalViewport.value());
7534 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7535 }
7536
7537 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7538 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7539 int32_t yExpected) {
7540 // touch on outside area should not work.
7541 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7542 processSync(mapper);
7543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7544
7545 // touch on inside area should receive the event.
7546 NotifyMotionArgs args;
7547 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7548 processSync(mapper);
7549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7550 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7551 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7552
7553 // Reset.
7554 mapper.reset(ARBITRARY_TIME);
7555 }
7556};
7557
7558TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7559 addConfigurationProperty("touch.deviceType", "touchScreen");
7560 prepareDisplay(DISPLAY_ORIENTATION_0);
7561 prepareAxes(POSITION);
7562 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7563
7564 // Touch on center of normal display should work.
7565 const int32_t x = DISPLAY_WIDTH / 4;
7566 const int32_t y = DISPLAY_HEIGHT / 2;
7567 processPosition(mapper, toRawX(x), toRawY(y));
7568 processSync(mapper);
7569 NotifyMotionArgs args;
7570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7572 0.0f, 0.0f, 0.0f, 0.0f));
7573 // Reset.
7574 mapper.reset(ARBITRARY_TIME);
7575
7576 // Let physical display be different to device, and make surface and physical could be 1:1.
7577 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7578
7579 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7580 const int32_t yExpected = y;
7581 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7582}
7583
7584TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7585 addConfigurationProperty("touch.deviceType", "touchScreen");
7586 prepareDisplay(DISPLAY_ORIENTATION_0);
7587 prepareAxes(POSITION);
7588 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7589
7590 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7591 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7592
7593 const int32_t x = DISPLAY_WIDTH / 4;
7594 const int32_t y = DISPLAY_HEIGHT / 2;
7595
7596 // expect x/y = swap x/y then reverse y.
7597 const int32_t xExpected = y;
7598 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7599 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7600}
7601
7602TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7603 addConfigurationProperty("touch.deviceType", "touchScreen");
7604 prepareDisplay(DISPLAY_ORIENTATION_0);
7605 prepareAxes(POSITION);
7606 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7607
7608 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7609 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7610
7611 const int32_t x = DISPLAY_WIDTH / 4;
7612 const int32_t y = DISPLAY_HEIGHT / 2;
7613
7614 // expect x/y = swap x/y then reverse x.
7615 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7616 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7617 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7618}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007619
7620TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7621 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7622 std::shared_ptr<FakePointerController> fakePointerController =
7623 std::make_shared<FakePointerController>();
7624 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7625 fakePointerController->setPosition(0, 0);
7626 fakePointerController->setButtonState(0);
7627
7628 // prepare device and capture
7629 prepareDisplay(DISPLAY_ORIENTATION_0);
7630 prepareAxes(POSITION | ID | SLOT);
7631 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7632 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7633 mFakePolicy->setPointerCapture(true);
7634 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7635 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7636
7637 // captured touchpad should be a touchpad source
7638 NotifyDeviceResetArgs resetArgs;
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7640 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7641
7642 // run captured pointer tests - note that this is unscaled, so input listener events should be
7643 // identical to what the hardware sends (accounting for any
7644 // calibration).
7645 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007646 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007647 processId(mapper, 1);
7648 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7649 processKey(mapper, BTN_TOUCH, 1);
7650 processSync(mapper);
7651
7652 // expect coord[0] to contain initial location of touch 0
7653 NotifyMotionArgs args;
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7655 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7656 ASSERT_EQ(1U, args.pointerCount);
7657 ASSERT_EQ(0, args.pointerProperties[0].id);
7658 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7659 ASSERT_NO_FATAL_FAILURE(
7660 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7661
7662 // FINGER 1 DOWN
7663 processSlot(mapper, 1);
7664 processId(mapper, 2);
7665 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7666 processSync(mapper);
7667
7668 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007670 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7671 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007672 ASSERT_EQ(2U, args.pointerCount);
7673 ASSERT_EQ(0, args.pointerProperties[0].id);
7674 ASSERT_EQ(1, args.pointerProperties[1].id);
7675 ASSERT_NO_FATAL_FAILURE(
7676 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7677 ASSERT_NO_FATAL_FAILURE(
7678 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7679
7680 // FINGER 1 MOVE
7681 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7682 processSync(mapper);
7683
7684 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7685 // from move
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7688 ASSERT_NO_FATAL_FAILURE(
7689 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7690 ASSERT_NO_FATAL_FAILURE(
7691 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7692
7693 // FINGER 0 MOVE
7694 processSlot(mapper, 0);
7695 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7696 processSync(mapper);
7697
7698 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7701 ASSERT_NO_FATAL_FAILURE(
7702 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7703 ASSERT_NO_FATAL_FAILURE(
7704 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7705
7706 // BUTTON DOWN
7707 processKey(mapper, BTN_LEFT, 1);
7708 processSync(mapper);
7709
7710 // touchinputmapper design sends a move before button press
7711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7715
7716 // BUTTON UP
7717 processKey(mapper, BTN_LEFT, 0);
7718 processSync(mapper);
7719
7720 // touchinputmapper design sends a move after button release
7721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7722 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7725
7726 // FINGER 0 UP
7727 processId(mapper, -1);
7728 processSync(mapper);
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7730 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7731
7732 // FINGER 1 MOVE
7733 processSlot(mapper, 1);
7734 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7735 processSync(mapper);
7736
7737 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7740 ASSERT_EQ(1U, args.pointerCount);
7741 ASSERT_EQ(1, args.pointerProperties[0].id);
7742 ASSERT_NO_FATAL_FAILURE(
7743 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7744
7745 // FINGER 1 UP
7746 processId(mapper, -1);
7747 processKey(mapper, BTN_TOUCH, 0);
7748 processSync(mapper);
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7750 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7751
7752 // non captured touchpad should be a mouse source
7753 mFakePolicy->setPointerCapture(false);
7754 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7756 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7757}
7758
7759TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7760 std::shared_ptr<FakePointerController> fakePointerController =
7761 std::make_shared<FakePointerController>();
7762 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7763 fakePointerController->setPosition(0, 0);
7764 fakePointerController->setButtonState(0);
7765
7766 // prepare device and capture
7767 prepareDisplay(DISPLAY_ORIENTATION_0);
7768 prepareAxes(POSITION | ID | SLOT);
7769 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7770 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7771 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7772 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7773 // run uncaptured pointer tests - pushes out generic events
7774 // FINGER 0 DOWN
7775 processId(mapper, 3);
7776 processPosition(mapper, 100, 100);
7777 processKey(mapper, BTN_TOUCH, 1);
7778 processSync(mapper);
7779
7780 // start at (100,100), cursor should be at (0,0) * scale
7781 NotifyMotionArgs args;
7782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7783 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7784 ASSERT_NO_FATAL_FAILURE(
7785 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7786
7787 // FINGER 0 MOVE
7788 processPosition(mapper, 200, 200);
7789 processSync(mapper);
7790
7791 // compute scaling to help with touch position checking
7792 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7793 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7794 float scale =
7795 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7796
7797 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7799 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7801 0, 0, 0, 0, 0, 0, 0));
7802}
7803
7804TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7805 std::shared_ptr<FakePointerController> fakePointerController =
7806 std::make_shared<FakePointerController>();
7807
7808 prepareDisplay(DISPLAY_ORIENTATION_0);
7809 prepareAxes(POSITION | ID | SLOT);
7810 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7811 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7812 mFakePolicy->setPointerCapture(false);
7813 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7814
7815 // uncaptured touchpad should be a pointer device
7816 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7817
7818 // captured touchpad should be a touchpad device
7819 mFakePolicy->setPointerCapture(true);
7820 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7821 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7822}
7823
Michael Wrightd02c5b62014-02-10 15:10:22 -08007824} // namespace android