blob: 82157b69d99534f3fb9161acd64ebd3dc1c39338 [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
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100343 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700344 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345 }
346
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100347 virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
348 return "";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800350
351 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
352 std::unique_lock<std::mutex> lock(mLock);
353 base::ScopedLockAssertion assumeLocked(mLock);
354
355 const bool devicesChanged =
356 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
357 return mInputDevicesChanged;
358 });
359 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
360 mInputDevicesChanged = false;
361 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800362};
363
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364// --- FakeEventHub ---
365
366class FakeEventHub : public EventHubInterface {
367 struct KeyInfo {
368 int32_t keyCode;
369 uint32_t flags;
370 };
371
372 struct Device {
373 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700374 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375 PropertyMap configuration;
376 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
377 KeyedVector<int, bool> relativeAxes;
378 KeyedVector<int32_t, int32_t> keyCodeStates;
379 KeyedVector<int32_t, int32_t> scanCodeStates;
380 KeyedVector<int32_t, int32_t> switchStates;
381 KeyedVector<int32_t, int32_t> absoluteAxisValue;
382 KeyedVector<int32_t, KeyInfo> keysByScanCode;
383 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
384 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800385 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700386 bool enabled;
387
388 status_t enable() {
389 enabled = true;
390 return OK;
391 }
392
393 status_t disable() {
394 enabled = false;
395 return OK;
396 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397
Chris Ye1b0c7342020-07-28 21:57:03 -0700398 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399 };
400
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700401 std::mutex mLock;
402 std::condition_variable mEventsCondition;
403
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100405 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700406 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600407 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700409public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 virtual ~FakeEventHub() {
411 for (size_t i = 0; i < mDevices.size(); i++) {
412 delete mDevices.valueAt(i);
413 }
414 }
415
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416 FakeEventHub() { }
417
Chris Ye1b0c7342020-07-28 21:57:03 -0700418 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800419 Device* device = new Device(classes);
420 device->identifier.name = name;
421 mDevices.add(deviceId, device);
422
423 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
424 }
425
426 void removeDevice(int32_t deviceId) {
427 delete mDevices.valueFor(deviceId);
428 mDevices.removeItem(deviceId);
429
430 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
431 }
432
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700433 bool isDeviceEnabled(int32_t deviceId) {
434 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700435 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700436 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
437 return false;
438 }
439 return device->enabled;
440 }
441
442 status_t enableDevice(int32_t deviceId) {
443 status_t result;
444 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700445 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700446 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
447 return BAD_VALUE;
448 }
449 if (device->enabled) {
450 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
451 return OK;
452 }
453 result = device->enable();
454 return result;
455 }
456
457 status_t disableDevice(int32_t deviceId) {
458 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700459 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700460 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
461 return BAD_VALUE;
462 }
463 if (!device->enabled) {
464 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
465 return OK;
466 }
467 return device->disable();
468 }
469
Michael Wrightd02c5b62014-02-10 15:10:22 -0800470 void finishDeviceScan() {
471 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
472 }
473
474 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
475 Device* device = getDevice(deviceId);
476 device->configuration.addProperty(key, value);
477 }
478
479 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
480 Device* device = getDevice(deviceId);
481 device->configuration.addAll(configuration);
482 }
483
484 void addAbsoluteAxis(int32_t deviceId, int axis,
485 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
486 Device* device = getDevice(deviceId);
487
488 RawAbsoluteAxisInfo info;
489 info.valid = true;
490 info.minValue = minValue;
491 info.maxValue = maxValue;
492 info.flat = flat;
493 info.fuzz = fuzz;
494 info.resolution = resolution;
495 device->absoluteAxes.add(axis, info);
496 }
497
498 void addRelativeAxis(int32_t deviceId, int32_t axis) {
499 Device* device = getDevice(deviceId);
500 device->relativeAxes.add(axis, true);
501 }
502
503 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
504 Device* device = getDevice(deviceId);
505 device->keyCodeStates.replaceValueFor(keyCode, state);
506 }
507
508 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
509 Device* device = getDevice(deviceId);
510 device->scanCodeStates.replaceValueFor(scanCode, state);
511 }
512
513 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
514 Device* device = getDevice(deviceId);
515 device->switchStates.replaceValueFor(switchCode, state);
516 }
517
518 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
519 Device* device = getDevice(deviceId);
520 device->absoluteAxisValue.replaceValueFor(axis, value);
521 }
522
523 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
524 int32_t keyCode, uint32_t flags) {
525 Device* device = getDevice(deviceId);
526 KeyInfo info;
527 info.keyCode = keyCode;
528 info.flags = flags;
529 if (scanCode) {
530 device->keysByScanCode.add(scanCode, info);
531 }
532 if (usageCode) {
533 device->keysByUsageCode.add(usageCode, info);
534 }
535 }
536
537 void addLed(int32_t deviceId, int32_t led, bool initialState) {
538 Device* device = getDevice(deviceId);
539 device->leds.add(led, initialState);
540 }
541
542 bool getLedState(int32_t deviceId, int32_t led) {
543 Device* device = getDevice(deviceId);
544 return device->leds.valueFor(led);
545 }
546
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100547 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800548 return mExcludedDevices;
549 }
550
551 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
552 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800553 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 }
555
556 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
557 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700558 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 RawEvent event;
560 event.when = when;
561 event.deviceId = deviceId;
562 event.type = type;
563 event.code = code;
564 event.value = value;
565 mEvents.push_back(event);
566
567 if (type == EV_ABS) {
568 setAbsoluteAxisValue(deviceId, code, value);
569 }
570 }
571
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600572 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
573 std::vector<TouchVideoFrame>> videoFrames) {
574 mVideoFrames = std::move(videoFrames);
575 }
576
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700578 std::unique_lock<std::mutex> lock(mLock);
579 base::ScopedLockAssertion assumeLocked(mLock);
580 const bool queueIsEmpty =
581 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
582 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
583 if (!queueIsEmpty) {
584 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
585 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 }
587
588private:
589 Device* getDevice(int32_t deviceId) const {
590 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100591 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 }
593
Chris Ye1b0c7342020-07-28 21:57:03 -0700594 virtual Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700596 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 }
598
599 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
600 Device* device = getDevice(deviceId);
601 return device ? device->identifier : InputDeviceIdentifier();
602 }
603
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100604 virtual int32_t getDeviceControllerNumber(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 return 0;
606 }
607
608 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
609 Device* device = getDevice(deviceId);
610 if (device) {
611 *outConfiguration = device->configuration;
612 }
613 }
614
615 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
616 RawAbsoluteAxisInfo* outAxisInfo) const {
617 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800618 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619 ssize_t index = device->absoluteAxes.indexOfKey(axis);
620 if (index >= 0) {
621 *outAxisInfo = device->absoluteAxes.valueAt(index);
622 return OK;
623 }
624 }
625 outAxisInfo->clear();
626 return -1;
627 }
628
629 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
630 Device* device = getDevice(deviceId);
631 if (device) {
632 return device->relativeAxes.indexOfKey(axis) >= 0;
633 }
634 return false;
635 }
636
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100637 virtual bool hasInputProperty(int32_t, int) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 return false;
639 }
640
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700641 virtual status_t mapKey(int32_t deviceId,
642 int32_t scanCode, int32_t usageCode, int32_t metaState,
643 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644 Device* device = getDevice(deviceId);
645 if (device) {
646 const KeyInfo* key = getKey(device, scanCode, usageCode);
647 if (key) {
648 if (outKeycode) {
649 *outKeycode = key->keyCode;
650 }
651 if (outFlags) {
652 *outFlags = key->flags;
653 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700654 if (outMetaState) {
655 *outMetaState = metaState;
656 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 return OK;
658 }
659 }
660 return NAME_NOT_FOUND;
661 }
662
663 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
664 if (usageCode) {
665 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
666 if (index >= 0) {
667 return &device->keysByUsageCode.valueAt(index);
668 }
669 }
670 if (scanCode) {
671 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
672 if (index >= 0) {
673 return &device->keysByScanCode.valueAt(index);
674 }
675 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700676 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 }
678
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100679 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 return NAME_NOT_FOUND;
681 }
682
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100683 virtual void setExcludedDevices(const std::vector<std::string>& devices) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684 mExcludedDevices = devices;
685 }
686
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100687 virtual size_t getEvents(int, RawEvent* buffer, size_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700688 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 if (mEvents.empty()) {
690 return 0;
691 }
692
693 *buffer = *mEvents.begin();
694 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700695 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 return 1;
697 }
698
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800699 virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600700 auto it = mVideoFrames.find(deviceId);
701 if (it != mVideoFrames.end()) {
702 std::vector<TouchVideoFrame> frames = std::move(it->second);
703 mVideoFrames.erase(deviceId);
704 return frames;
705 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800706 return {};
707 }
708
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
710 Device* device = getDevice(deviceId);
711 if (device) {
712 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
713 if (index >= 0) {
714 return device->scanCodeStates.valueAt(index);
715 }
716 }
717 return AKEY_STATE_UNKNOWN;
718 }
719
720 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
721 Device* device = getDevice(deviceId);
722 if (device) {
723 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
724 if (index >= 0) {
725 return device->keyCodeStates.valueAt(index);
726 }
727 }
728 return AKEY_STATE_UNKNOWN;
729 }
730
731 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
732 Device* device = getDevice(deviceId);
733 if (device) {
734 ssize_t index = device->switchStates.indexOfKey(sw);
735 if (index >= 0) {
736 return device->switchStates.valueAt(index);
737 }
738 }
739 return AKEY_STATE_UNKNOWN;
740 }
741
742 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
743 int32_t* outValue) const {
744 Device* device = getDevice(deviceId);
745 if (device) {
746 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
747 if (index >= 0) {
748 *outValue = device->absoluteAxisValue.valueAt(index);
749 return OK;
750 }
751 }
752 *outValue = 0;
753 return -1;
754 }
755
756 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
757 uint8_t* outFlags) const {
758 bool result = false;
759 Device* device = getDevice(deviceId);
760 if (device) {
761 for (size_t i = 0; i < numCodes; i++) {
762 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
763 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
764 outFlags[i] = 1;
765 result = true;
766 }
767 }
768 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
769 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
770 outFlags[i] = 1;
771 result = true;
772 }
773 }
774 }
775 }
776 return result;
777 }
778
779 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
780 Device* device = getDevice(deviceId);
781 if (device) {
782 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
783 return index >= 0;
784 }
785 return false;
786 }
787
788 virtual bool hasLed(int32_t deviceId, int32_t led) const {
789 Device* device = getDevice(deviceId);
790 return device && device->leds.indexOfKey(led) >= 0;
791 }
792
793 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
794 Device* device = getDevice(deviceId);
795 if (device) {
796 ssize_t index = device->leds.indexOfKey(led);
797 if (index >= 0) {
798 device->leds.replaceValueAt(led, on);
799 } else {
800 ADD_FAILURE()
801 << "Attempted to set the state of an LED that the EventHub declared "
802 "was not present. led=" << led;
803 }
804 }
805 }
806
807 virtual void getVirtualKeyDefinitions(int32_t deviceId,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800808 std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 outVirtualKeys.clear();
810
811 Device* device = getDevice(deviceId);
812 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800813 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814 }
815 }
816
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100817 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
Yi Kong9b14ac62018-07-17 13:48:38 -0700818 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800819 }
820
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100821 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822 return false;
823 }
824
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000825 virtual void vibrate(int32_t, const VibrationElement&) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100827 virtual void cancelVibrate(int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 }
829
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100830 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831 return false;
832 }
833
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800834 virtual void dump(std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800835 }
836
837 virtual void monitor() {
838 }
839
840 virtual void requestReopenDevices() {
841 }
842
843 virtual void wake() {
844 }
845};
846
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847// --- FakeInputMapper ---
848
849class FakeInputMapper : public InputMapper {
850 uint32_t mSources;
851 int32_t mKeyboardType;
852 int32_t mMetaState;
853 KeyedVector<int32_t, int32_t> mKeyCodeStates;
854 KeyedVector<int32_t, int32_t> mScanCodeStates;
855 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800856 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700858 std::mutex mLock;
859 std::condition_variable mStateChangedCondition;
860 bool mConfigureWasCalled GUARDED_BY(mLock);
861 bool mResetWasCalled GUARDED_BY(mLock);
862 bool mProcessWasCalled GUARDED_BY(mLock);
863 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864
Arthur Hungc23540e2018-11-29 20:42:11 +0800865 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800867 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
868 : InputMapper(deviceContext),
869 mSources(sources),
870 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800871 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800872 mConfigureWasCalled(false),
873 mResetWasCalled(false),
874 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875
876 virtual ~FakeInputMapper() { }
877
878 void setKeyboardType(int32_t keyboardType) {
879 mKeyboardType = keyboardType;
880 }
881
882 void setMetaState(int32_t metaState) {
883 mMetaState = metaState;
884 }
885
886 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700887 std::unique_lock<std::mutex> lock(mLock);
888 base::ScopedLockAssertion assumeLocked(mLock);
889 const bool configureCalled =
890 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
891 return mConfigureWasCalled;
892 });
893 if (!configureCalled) {
894 FAIL() << "Expected configure() to have been called.";
895 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 mConfigureWasCalled = false;
897 }
898
899 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700900 std::unique_lock<std::mutex> lock(mLock);
901 base::ScopedLockAssertion assumeLocked(mLock);
902 const bool resetCalled =
903 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
904 return mResetWasCalled;
905 });
906 if (!resetCalled) {
907 FAIL() << "Expected reset() to have been called.";
908 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 mResetWasCalled = false;
910 }
911
Yi Kong9b14ac62018-07-17 13:48:38 -0700912 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700913 std::unique_lock<std::mutex> lock(mLock);
914 base::ScopedLockAssertion assumeLocked(mLock);
915 const bool processCalled =
916 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
917 return mProcessWasCalled;
918 });
919 if (!processCalled) {
920 FAIL() << "Expected process() to have been called.";
921 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922 if (outLastEvent) {
923 *outLastEvent = mLastEvent;
924 }
925 mProcessWasCalled = false;
926 }
927
928 void setKeyCodeState(int32_t keyCode, int32_t state) {
929 mKeyCodeStates.replaceValueFor(keyCode, state);
930 }
931
932 void setScanCodeState(int32_t scanCode, int32_t state) {
933 mScanCodeStates.replaceValueFor(scanCode, state);
934 }
935
936 void setSwitchState(int32_t switchCode, int32_t state) {
937 mSwitchStates.replaceValueFor(switchCode, state);
938 }
939
940 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800941 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942 }
943
944private:
945 virtual uint32_t getSources() {
946 return mSources;
947 }
948
949 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
950 InputMapper::populateDeviceInfo(deviceInfo);
951
952 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
953 deviceInfo->setKeyboardType(mKeyboardType);
954 }
955 }
956
Arthur Hungc23540e2018-11-29 20:42:11 +0800957 virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700958 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800960
961 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800962 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800963 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
964 mViewport = config->getDisplayViewportByPort(*displayPort);
965 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700966
967 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 }
969
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100970 virtual void reset(nsecs_t) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700971 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700973 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 }
975
976 virtual void process(const RawEvent* rawEvent) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700977 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800978 mLastEvent = *rawEvent;
979 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700980 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981 }
982
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100983 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
985 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
986 }
987
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100988 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
990 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
991 }
992
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100993 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994 ssize_t index = mSwitchStates.indexOfKey(switchCode);
995 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
996 }
997
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100998 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999 const int32_t* keyCodes, uint8_t* outFlags) {
1000 bool result = false;
1001 for (size_t i = 0; i < numCodes; i++) {
1002 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1003 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1004 outFlags[i] = 1;
1005 result = true;
1006 }
1007 }
1008 }
1009 return result;
1010 }
1011
1012 virtual int32_t getMetaState() {
1013 return mMetaState;
1014 }
1015
1016 virtual void fadePointer() {
1017 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001018
1019 virtual std::optional<int32_t> getAssociatedDisplay() {
1020 if (mViewport) {
1021 return std::make_optional(mViewport->displayId);
1022 }
1023 return std::nullopt;
1024 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025};
1026
1027
1028// --- InstrumentedInputReader ---
1029
1030class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001031 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032
1033public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001034 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1035 const sp<InputReaderPolicyInterface>& policy,
1036 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001037 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001039 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001040
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001041 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001043 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001044 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045 InputDeviceIdentifier identifier;
1046 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001047 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001049 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050 }
1051
Prabir Pradhan28efc192019-11-05 01:10:04 +00001052 // Make the protected loopOnce method accessible to tests.
1053 using InputReader::loopOnce;
1054
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055protected:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001056 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1057 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001058 if (!mNextDevices.empty()) {
1059 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1060 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061 return device;
1062 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001063 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001064 }
1065
arthurhungdcef2dc2020-08-11 14:47:50 +08001066 // --- FakeInputReaderContext ---
1067 class FakeInputReaderContext : public ContextImpl {
1068 int32_t mGlobalMetaState;
1069 bool mUpdateGlobalMetaStateWasCalled;
1070 int32_t mGeneration;
1071
1072 public:
1073 FakeInputReaderContext(InputReader* reader)
1074 : ContextImpl(reader),
1075 mGlobalMetaState(0),
1076 mUpdateGlobalMetaStateWasCalled(false),
1077 mGeneration(1) {}
1078
1079 virtual ~FakeInputReaderContext() {}
1080
1081 void assertUpdateGlobalMetaStateWasCalled() {
1082 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1083 << "Expected updateGlobalMetaState() to have been called.";
1084 mUpdateGlobalMetaStateWasCalled = false;
1085 }
1086
1087 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1088
1089 uint32_t getGeneration() { return mGeneration; }
1090
1091 void updateGlobalMetaState() override {
1092 mUpdateGlobalMetaStateWasCalled = true;
1093 ContextImpl::updateGlobalMetaState();
1094 }
1095
1096 int32_t getGlobalMetaState() override {
1097 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1098 }
1099
1100 int32_t bumpGeneration() override {
1101 mGeneration = ContextImpl::bumpGeneration();
1102 return mGeneration;
1103 }
1104 } mFakeContext;
1105
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001107
1108public:
1109 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110};
1111
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001112// --- InputReaderPolicyTest ---
1113class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001114protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001115 sp<FakeInputReaderPolicy> mFakePolicy;
1116
Prabir Pradhan28efc192019-11-05 01:10:04 +00001117 virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1118 virtual void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001119};
1120
1121/**
1122 * Check that empty set of viewports is an acceptable configuration.
1123 * Also try to get internal viewport two different ways - by type and by uniqueId.
1124 *
1125 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1126 * Such configuration is not currently allowed.
1127 */
1128TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001129 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001130
1131 // We didn't add any viewports yet, so there shouldn't be any.
1132 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001133 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001134 ASSERT_FALSE(internalViewport);
1135
1136 // Add an internal viewport, then clear it
1137 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001138 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT,
1139 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001140
1141 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001142 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001143 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001144 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001145
1146 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001147 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001148 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001149 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001150
1151 mFakePolicy->clearViewports();
1152 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001153 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001154 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001155 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001156 ASSERT_FALSE(internalViewport);
1157}
1158
1159TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1160 const std::string internalUniqueId = "local:0";
1161 const std::string externalUniqueId = "local:1";
1162 const std::string virtualUniqueId1 = "virtual:2";
1163 const std::string virtualUniqueId2 = "virtual:3";
1164 constexpr int32_t virtualDisplayId1 = 2;
1165 constexpr int32_t virtualDisplayId2 = 3;
1166
1167 // Add an internal viewport
1168 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001169 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT,
1170 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001171 // Add an external viewport
1172 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001173 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT,
1174 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001175 // Add an virtual viewport
1176 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001177 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT,
1178 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001179 // Add another virtual viewport
1180 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001181 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT,
1182 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001183
1184 // Check matching by type for internal
1185 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001186 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001187 ASSERT_TRUE(internalViewport);
1188 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1189
1190 // Check matching by type for external
1191 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001192 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001193 ASSERT_TRUE(externalViewport);
1194 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1195
1196 // Check matching by uniqueId for virtual viewport #1
1197 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001198 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001199 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001200 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001201 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1202 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1203
1204 // Check matching by uniqueId for virtual viewport #2
1205 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001206 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001207 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001208 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001209 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1210 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1211}
1212
1213
1214/**
1215 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1216 * that lookup works by checking display id.
1217 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1218 */
1219TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1220 const std::string uniqueId1 = "uniqueId1";
1221 const std::string uniqueId2 = "uniqueId2";
1222 constexpr int32_t displayId1 = 2;
1223 constexpr int32_t displayId2 = 3;
1224
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001225 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1226 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001227 for (const ViewportType& type : types) {
1228 mFakePolicy->clearViewports();
1229 // Add a viewport
1230 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001231 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001232 // Add another viewport
1233 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001234 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001235
1236 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001237 std::optional<DisplayViewport> viewport1 =
1238 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001239 ASSERT_TRUE(viewport1);
1240 ASSERT_EQ(displayId1, viewport1->displayId);
1241 ASSERT_EQ(type, viewport1->type);
1242
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001243 std::optional<DisplayViewport> viewport2 =
1244 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001245 ASSERT_TRUE(viewport2);
1246 ASSERT_EQ(displayId2, viewport2->displayId);
1247 ASSERT_EQ(type, viewport2->type);
1248
1249 // When there are multiple viewports of the same kind, and uniqueId is not specified
1250 // in the call to getDisplayViewport, then that situation is not supported.
1251 // The viewports can be stored in any order, so we cannot rely on the order, since that
1252 // is just implementation detail.
1253 // However, we can check that it still returns *a* viewport, we just cannot assert
1254 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001255 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001256 ASSERT_TRUE(someViewport);
1257 }
1258}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001260/**
1261 * Check getDisplayViewportByPort
1262 */
1263TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001264 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001265 const std::string uniqueId1 = "uniqueId1";
1266 const std::string uniqueId2 = "uniqueId2";
1267 constexpr int32_t displayId1 = 1;
1268 constexpr int32_t displayId2 = 2;
1269 const uint8_t hdmi1 = 0;
1270 const uint8_t hdmi2 = 1;
1271 const uint8_t hdmi3 = 2;
1272
1273 mFakePolicy->clearViewports();
1274 // Add a viewport that's associated with some display port that's not of interest.
1275 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1276 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1277 // Add another viewport, connected to HDMI1 port
1278 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1279 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1280
1281 // Check that correct display viewport was returned by comparing the display ports.
1282 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1283 ASSERT_TRUE(hdmi1Viewport);
1284 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1285 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1286
1287 // Check that we can still get the same viewport using the uniqueId
1288 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1289 ASSERT_TRUE(hdmi1Viewport);
1290 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1291 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1292 ASSERT_EQ(type, hdmi1Viewport->type);
1293
1294 // Check that we cannot find a port with "HDMI2", because we never added one
1295 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1296 ASSERT_FALSE(hdmi2Viewport);
1297}
1298
Michael Wrightd02c5b62014-02-10 15:10:22 -08001299// --- InputReaderTest ---
1300
1301class InputReaderTest : public testing::Test {
1302protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001303 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001304 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001305 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001306 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001307
Prabir Pradhan28efc192019-11-05 01:10:04 +00001308 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001309 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001310 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001311 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001312
Prabir Pradhan28efc192019-11-05 01:10:04 +00001313 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1314 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001315 }
1316
Prabir Pradhan28efc192019-11-05 01:10:04 +00001317 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318 mFakeListener.clear();
1319 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001320 }
1321
Chris Ye1b0c7342020-07-28 21:57:03 -07001322 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001323 const PropertyMap* configuration) {
1324 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325
1326 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001327 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328 }
1329 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001330 mReader->loopOnce();
1331 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001332 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1333 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001334 }
1335
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001336 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001337 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001338 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001339 }
1340
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001341 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001342 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001343 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001344 }
1345
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001346 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001347 const std::string& name,
1348 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001349 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001350 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1351 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001352 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001353 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001354 return mapper;
1355 }
1356};
1357
1358TEST_F(InputReaderTest, GetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001359 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1360 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1361 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001362
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001363 std::vector<InputDeviceInfo> inputDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001364 mReader->getInputDevices(inputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001365 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001366 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001367 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001368 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1369 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1370 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1371
1372 // Should also have received a notification describing the new input devices.
1373 inputDevices = mFakePolicy->getInputDevices();
1374 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001375 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001376 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001377 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1378 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1379 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1380}
1381
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001382TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001383 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001384 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001385 constexpr int32_t eventHubId = 1;
1386 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001387 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001388 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001389 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001390 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001391
Yi Kong9b14ac62018-07-17 13:48:38 -07001392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001393
1394 NotifyDeviceResetArgs resetArgs;
1395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001396 ASSERT_EQ(deviceId, resetArgs.deviceId);
1397
1398 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001399 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001400 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001401
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001403 ASSERT_EQ(deviceId, resetArgs.deviceId);
1404 ASSERT_EQ(device->isEnabled(), false);
1405
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001406 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001407 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001410 ASSERT_EQ(device->isEnabled(), false);
1411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001412 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001413 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001415 ASSERT_EQ(deviceId, resetArgs.deviceId);
1416 ASSERT_EQ(device->isEnabled(), true);
1417}
1418
Michael Wrightd02c5b62014-02-10 15:10:22 -08001419TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001420 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001421 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001422 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001423 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001424 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001425 AINPUT_SOURCE_KEYBOARD, nullptr);
1426 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001427
1428 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1429 AINPUT_SOURCE_ANY, AKEYCODE_A))
1430 << "Should return unknown when the device id is >= 0 but unknown.";
1431
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001432 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1433 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1434 << "Should return unknown when the device id is valid but the sources are not "
1435 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001436
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001437 ASSERT_EQ(AKEY_STATE_DOWN,
1438 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1439 AKEYCODE_A))
1440 << "Should return value provided by mapper when device id is valid and the device "
1441 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001442
1443 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1444 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1445 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1446
1447 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1448 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1449 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1450}
1451
1452TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001453 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001454 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001455 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001456 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001457 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001458 AINPUT_SOURCE_KEYBOARD, nullptr);
1459 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001460
1461 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1462 AINPUT_SOURCE_ANY, KEY_A))
1463 << "Should return unknown when the device id is >= 0 but unknown.";
1464
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001465 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1466 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1467 << "Should return unknown when the device id is valid but the sources are not "
1468 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001469
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001470 ASSERT_EQ(AKEY_STATE_DOWN,
1471 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1472 KEY_A))
1473 << "Should return value provided by mapper when device id is valid and the device "
1474 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001475
1476 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1477 AINPUT_SOURCE_TRACKBALL, KEY_A))
1478 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1479
1480 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1481 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1482 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1483}
1484
1485TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001486 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001487 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001488 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001489 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001490 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001491 AINPUT_SOURCE_KEYBOARD, nullptr);
1492 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001493
1494 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1495 AINPUT_SOURCE_ANY, SW_LID))
1496 << "Should return unknown when the device id is >= 0 but unknown.";
1497
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001498 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1499 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1500 << "Should return unknown when the device id is valid but the sources are not "
1501 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001502
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001503 ASSERT_EQ(AKEY_STATE_DOWN,
1504 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1505 SW_LID))
1506 << "Should return value provided by mapper when device id is valid and the device "
1507 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001508
1509 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1510 AINPUT_SOURCE_TRACKBALL, SW_LID))
1511 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1512
1513 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1514 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1515 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1516}
1517
1518TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001519 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001520 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001521 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001522 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001523 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001524 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001525
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001526 mapper.addSupportedKeyCode(AKEYCODE_A);
1527 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001528
1529 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1530 uint8_t flags[4] = { 0, 0, 0, 1 };
1531
1532 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1533 << "Should return false when device id is >= 0 but unknown.";
1534 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1535
1536 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001537 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1538 << "Should return false when device id is valid but the sources are not supported by "
1539 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001540 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1541
1542 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001543 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1544 keyCodes, flags))
1545 << "Should return value provided by mapper when device id is valid and the device "
1546 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001547 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1548
1549 flags[3] = 1;
1550 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1551 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1552 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1553
1554 flags[3] = 1;
1555 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1556 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1557 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1558}
1559
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001560TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001561 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001562 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001563
1564 NotifyConfigurationChangedArgs args;
1565
1566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1567 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1568}
1569
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001570TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001571 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001572 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001573 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001574 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001575 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001576 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001578 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001579 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001580 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1581
1582 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001583 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001585 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586 ASSERT_EQ(EV_KEY, event.type);
1587 ASSERT_EQ(KEY_A, event.code);
1588 ASSERT_EQ(1, event.value);
1589}
1590
Garfield Tan1c7bc862020-01-28 13:24:04 -08001591TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001592 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001593 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001594 constexpr int32_t eventHubId = 1;
1595 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001596 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001597 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001598 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001599 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001600
1601 NotifyDeviceResetArgs resetArgs;
1602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001603 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001604
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001605 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001606 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001608 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001609 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001611 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001612 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001614 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001615 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001616
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001617 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001618 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001620 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001621 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001622}
1623
Garfield Tan1c7bc862020-01-28 13:24:04 -08001624TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1625 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001626 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001627 constexpr int32_t eventHubId = 1;
1628 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1629 // Must add at least one mapper or the device will be ignored!
1630 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001631 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001632 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1633
1634 NotifyDeviceResetArgs resetArgs;
1635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1636 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1637}
1638
Arthur Hungc23540e2018-11-29 20:42:11 +08001639TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001640 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001641 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001643 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001644 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1645 FakeInputMapper& mapper =
1646 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001647 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001648
1649 const uint8_t hdmi1 = 1;
1650
1651 // Associated touch screen with second display.
1652 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1653
1654 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001655 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001656 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001657 DISPLAY_ORIENTATION_0, "local:0", NO_PORT,
1658 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001659 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001660 DISPLAY_ORIENTATION_0, "local:1", hdmi1,
1661 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001662 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001663 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001664
1665 // Add the device, and make sure all of the callbacks are triggered.
1666 // The device is added after the input port associations are processed since
1667 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001668 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001671 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001672
Arthur Hung2c9a3342019-07-23 14:18:59 +08001673 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001674 ASSERT_EQ(deviceId, device->getId());
1675 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1676 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001677
1678 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001679 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001680 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001681 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001682}
1683
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001684TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1685 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1686 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1687 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1688 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1689 // Must add at least one mapper or the device will be ignored!
1690 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1691 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1692 mReader->pushNextDevice(device);
1693 mReader->pushNextDevice(device);
1694 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1695 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1696
1697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1698
1699 NotifyDeviceResetArgs resetArgs;
1700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1701 ASSERT_EQ(deviceId, resetArgs.deviceId);
1702 ASSERT_TRUE(device->isEnabled());
1703 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1704 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1705
1706 disableDevice(deviceId);
1707 mReader->loopOnce();
1708
1709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1710 ASSERT_EQ(deviceId, resetArgs.deviceId);
1711 ASSERT_FALSE(device->isEnabled());
1712 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1713 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1714
1715 enableDevice(deviceId);
1716 mReader->loopOnce();
1717
1718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1719 ASSERT_EQ(deviceId, resetArgs.deviceId);
1720 ASSERT_TRUE(device->isEnabled());
1721 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1722 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1723}
1724
1725TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1726 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1727 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1728 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1729 // Add two subdevices to device
1730 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1731 FakeInputMapper& mapperDevice1 =
1732 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1733 FakeInputMapper& mapperDevice2 =
1734 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1735 mReader->pushNextDevice(device);
1736 mReader->pushNextDevice(device);
1737 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1738 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1739
1740 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1741 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1742
1743 ASSERT_EQ(AKEY_STATE_DOWN,
1744 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1745 ASSERT_EQ(AKEY_STATE_DOWN,
1746 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1747 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1748 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1749}
1750
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001751// --- InputReaderIntegrationTest ---
1752
1753// These tests create and interact with the InputReader only through its interface.
1754// The InputReader is started during SetUp(), which starts its processing in its own
1755// thread. The tests use linux uinput to emulate input devices.
1756// NOTE: Interacting with the physical device while these tests are running may cause
1757// the tests to fail.
1758class InputReaderIntegrationTest : public testing::Test {
1759protected:
1760 sp<TestInputListener> mTestListener;
1761 sp<FakeInputReaderPolicy> mFakePolicy;
1762 sp<InputReaderInterface> mReader;
1763
1764 virtual void SetUp() override {
1765 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001766 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1767 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001768
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001769 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001770 ASSERT_EQ(mReader->start(), OK);
1771
1772 // Since this test is run on a real device, all the input devices connected
1773 // to the test device will show up in mReader. We wait for those input devices to
1774 // show up before beginning the tests.
1775 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1776 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1777 }
1778
1779 virtual void TearDown() override {
1780 ASSERT_EQ(mReader->stop(), OK);
1781 mTestListener.clear();
1782 mFakePolicy.clear();
1783 }
1784};
1785
1786TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1787 // An invalid input device that is only used for this test.
1788 class InvalidUinputDevice : public UinputDevice {
1789 public:
1790 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1791
1792 private:
1793 void configureDevice(int fd, uinput_user_dev* device) override {}
1794 };
1795
1796 const size_t numDevices = mFakePolicy->getInputDevices().size();
1797
1798 // UinputDevice does not set any event or key bits, so InputReader should not
1799 // consider it as a valid device.
1800 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1801 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1802 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1803 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1804
1805 invalidDevice.reset();
1806 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1807 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1808 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1809}
1810
1811TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1812 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1813
1814 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1815 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1817 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1818
1819 // Find the test device by its name.
1820 std::vector<InputDeviceInfo> inputDevices;
1821 mReader->getInputDevices(inputDevices);
1822 InputDeviceInfo* keyboardInfo = nullptr;
1823 const char* keyboardName = keyboard->getName();
1824 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1825 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1826 keyboardInfo = &inputDevices[i];
1827 break;
1828 }
1829 }
1830 ASSERT_NE(keyboardInfo, nullptr);
1831 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1832 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1833 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1834
1835 keyboard.reset();
1836 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1837 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1838 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1839}
1840
1841TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1842 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1843 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1844
1845 NotifyConfigurationChangedArgs configChangedArgs;
1846 ASSERT_NO_FATAL_FAILURE(
1847 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001848 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001849 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1850
1851 NotifyKeyArgs keyArgs;
1852 keyboard->pressAndReleaseHomeKey();
1853 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1854 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001855 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001856 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001857 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1858 prevTimestamp = keyArgs.eventTime;
1859
1860 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1861 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001862 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001863 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1864}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001865
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001866/**
1867 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1868 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1869 * are passed to the listener.
1870 */
1871static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1872TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1873 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1874 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1875 NotifyKeyArgs keyArgs;
1876
1877 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1878 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1879 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1880 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1881
1882 controller->pressAndReleaseKey(BTN_GEAR_UP);
1883 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1884 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1885 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1886}
1887
Arthur Hungaab25622020-01-16 11:22:11 +08001888// --- TouchProcessTest ---
1889class TouchIntegrationTest : public InputReaderIntegrationTest {
1890protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001891 const std::string UNIQUE_ID = "local:0";
1892
1893 virtual void SetUp() override {
1894 InputReaderIntegrationTest::SetUp();
1895 // At least add an internal display.
1896 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1897 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001898 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001899
1900 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1901 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1902 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1903 }
1904
1905 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1906 int32_t orientation, const std::string& uniqueId,
1907 std::optional<uint8_t> physicalPort,
1908 ViewportType viewportType) {
1909 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1910 physicalPort, viewportType);
1911 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1912 }
1913
1914 std::unique_ptr<UinputTouchScreen> mDevice;
1915};
1916
1917TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1918 NotifyMotionArgs args;
1919 const Point centerPoint = mDevice->getCenterPoint();
1920
1921 // ACTION_DOWN
1922 mDevice->sendDown(centerPoint);
1923 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1925
1926 // ACTION_MOVE
1927 mDevice->sendMove(centerPoint + Point(1, 1));
1928 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1930
1931 // ACTION_UP
1932 mDevice->sendUp();
1933 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1934 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1935}
1936
1937TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1938 NotifyMotionArgs args;
1939 const Point centerPoint = mDevice->getCenterPoint();
1940
1941 // ACTION_DOWN
1942 mDevice->sendDown(centerPoint);
1943 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1944 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1945
1946 // ACTION_POINTER_DOWN (Second slot)
1947 const Point secondPoint = centerPoint + Point(100, 100);
1948 mDevice->sendSlot(SECOND_SLOT);
1949 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1950 mDevice->sendDown(secondPoint + Point(1, 1));
1951 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1952 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1953 args.action);
1954
1955 // ACTION_MOVE (Second slot)
1956 mDevice->sendMove(secondPoint);
1957 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1959
1960 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001961 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08001962 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08001963 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08001964 args.action);
1965
1966 // ACTION_UP
1967 mDevice->sendSlot(FIRST_SLOT);
1968 mDevice->sendUp();
1969 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1970 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1971}
1972
1973TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1974 NotifyMotionArgs args;
1975 const Point centerPoint = mDevice->getCenterPoint();
1976
1977 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001978 mDevice->sendSlot(FIRST_SLOT);
1979 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001980 mDevice->sendDown(centerPoint);
1981 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1982 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1983
arthurhungcc7f9802020-04-30 17:55:40 +08001984 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001985 const Point secondPoint = centerPoint + Point(100, 100);
1986 mDevice->sendSlot(SECOND_SLOT);
1987 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1988 mDevice->sendDown(secondPoint);
1989 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1990 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1991 args.action);
1992
arthurhungcc7f9802020-04-30 17:55:40 +08001993 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001994 mDevice->sendMove(secondPoint + Point(1, 1));
1995 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1997
arthurhungcc7f9802020-04-30 17:55:40 +08001998 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1999 // a palm event.
2000 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002001 mDevice->sendToolType(MT_TOOL_PALM);
2002 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002003 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2004 args.action);
2005 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002006
arthurhungcc7f9802020-04-30 17:55:40 +08002007 // Send up to second slot, expect first slot send moving.
2008 mDevice->sendPointerUp();
2009 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002011
arthurhungcc7f9802020-04-30 17:55:40 +08002012 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002013 mDevice->sendSlot(FIRST_SLOT);
2014 mDevice->sendUp();
2015
arthurhungcc7f9802020-04-30 17:55:40 +08002016 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2017 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002018}
2019
Michael Wrightd02c5b62014-02-10 15:10:22 -08002020// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002021class InputDeviceTest : public testing::Test {
2022protected:
2023 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002024 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002025 static const int32_t DEVICE_ID;
2026 static const int32_t DEVICE_GENERATION;
2027 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002028 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002029 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002030
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002031 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002032 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002033 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002034 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002035 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002036
Prabir Pradhan28efc192019-11-05 01:10:04 +00002037 virtual void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002038 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002039 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002040 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002041 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2042 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002043 InputDeviceIdentifier identifier;
2044 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002045 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002046 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002047 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002048 mReader->pushNextDevice(mDevice);
2049 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2050 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002051 }
2052
Prabir Pradhan28efc192019-11-05 01:10:04 +00002053 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002054 mFakeListener.clear();
2055 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002056 }
2057};
2058
2059const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002060const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002061const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002062const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2063const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002064const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2065 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002066const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002067
2068TEST_F(InputDeviceTest, ImmutableProperties) {
2069 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002070 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002071 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002072}
2073
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002074TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2075 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002076}
2077
Michael Wrightd02c5b62014-02-10 15:10:22 -08002078TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2079 // Configuration.
2080 InputReaderConfiguration config;
2081 mDevice->configure(ARBITRARY_TIME, &config, 0);
2082
2083 // Reset.
2084 mDevice->reset(ARBITRARY_TIME);
2085
2086 NotifyDeviceResetArgs resetArgs;
2087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2088 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2089 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2090
2091 // Metadata.
2092 ASSERT_TRUE(mDevice->isIgnored());
2093 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2094
2095 InputDeviceInfo info;
2096 mDevice->getDeviceInfo(&info);
2097 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002098 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002099 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2100 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2101
2102 // State queries.
2103 ASSERT_EQ(0, mDevice->getMetaState());
2104
2105 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2106 << "Ignored device should return unknown key code state.";
2107 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2108 << "Ignored device should return unknown scan code state.";
2109 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2110 << "Ignored device should return unknown switch state.";
2111
2112 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2113 uint8_t flags[2] = { 0, 1 };
2114 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2115 << "Ignored device should never mark any key codes.";
2116 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2117 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2118}
2119
2120TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2121 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002122 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002123
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002124 FakeInputMapper& mapper1 =
2125 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002126 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2127 mapper1.setMetaState(AMETA_ALT_ON);
2128 mapper1.addSupportedKeyCode(AKEYCODE_A);
2129 mapper1.addSupportedKeyCode(AKEYCODE_B);
2130 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2131 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2132 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2133 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2134 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002135
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002136 FakeInputMapper& mapper2 =
2137 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002138 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002139
2140 InputReaderConfiguration config;
2141 mDevice->configure(ARBITRARY_TIME, &config, 0);
2142
2143 String8 propertyValue;
2144 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2145 << "Device should have read configuration during configuration phase.";
2146 ASSERT_STREQ("value", propertyValue.string());
2147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002148 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2149 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002150
2151 // Reset
2152 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002153 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2154 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002155
2156 NotifyDeviceResetArgs resetArgs;
2157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2158 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2159 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2160
2161 // Metadata.
2162 ASSERT_FALSE(mDevice->isIgnored());
2163 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2164
2165 InputDeviceInfo info;
2166 mDevice->getDeviceInfo(&info);
2167 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002168 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002169 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2170 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2171
2172 // State queries.
2173 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2174 << "Should query mappers and combine meta states.";
2175
2176 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2177 << "Should return unknown key code state when source not supported.";
2178 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2179 << "Should return unknown scan code state when source not supported.";
2180 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2181 << "Should return unknown switch state when source not supported.";
2182
2183 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2184 << "Should query mapper when source is supported.";
2185 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2186 << "Should query mapper when source is supported.";
2187 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2188 << "Should query mapper when source is supported.";
2189
2190 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2191 uint8_t flags[4] = { 0, 0, 0, 1 };
2192 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2193 << "Should do nothing when source is unsupported.";
2194 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2195 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2196 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2197 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2198
2199 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2200 << "Should query mapper when source is supported.";
2201 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2202 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2203 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2204 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2205
2206 // Event handling.
2207 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002208 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002209 mDevice->process(&event, 1);
2210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002211 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2212 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002213}
2214
Arthur Hung2c9a3342019-07-23 14:18:59 +08002215// A single input device is associated with a specific display. Check that:
2216// 1. Device is disabled if the viewport corresponding to the associated display is not found
2217// 2. Device is disabled when setEnabled API is called
2218TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002219 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002220
2221 // First Configuration.
2222 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2223
2224 // Device should be enabled by default.
2225 ASSERT_TRUE(mDevice->isEnabled());
2226
2227 // Prepare associated info.
2228 constexpr uint8_t hdmi = 1;
2229 const std::string UNIQUE_ID = "local:1";
2230
2231 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2232 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2233 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2234 // Device should be disabled because it is associated with a specific display via
2235 // input port <-> display port association, but the corresponding display is not found
2236 ASSERT_FALSE(mDevice->isEnabled());
2237
2238 // Prepare displays.
2239 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002240 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002241 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2242 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2243 ASSERT_TRUE(mDevice->isEnabled());
2244
2245 // Device should be disabled after set disable.
2246 mFakePolicy->addDisabledDevice(mDevice->getId());
2247 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2248 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2249 ASSERT_FALSE(mDevice->isEnabled());
2250
2251 // Device should still be disabled even found the associated display.
2252 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2253 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2254 ASSERT_FALSE(mDevice->isEnabled());
2255}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256
2257// --- InputMapperTest ---
2258
2259class InputMapperTest : public testing::Test {
2260protected:
2261 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002262 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002263 static const int32_t DEVICE_ID;
2264 static const int32_t DEVICE_GENERATION;
2265 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002266 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002267 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002268
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002269 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002270 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002271 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002272 std::unique_ptr<InstrumentedInputReader> mReader;
2273 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274
Chris Ye1b0c7342020-07-28 21:57:03 -07002275 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002276 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002277 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002278 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002279 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2280 mFakeListener);
2281 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002282 }
2283
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002284 virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2285
Prabir Pradhan28efc192019-11-05 01:10:04 +00002286 virtual void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002287 mFakeListener.clear();
2288 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289 }
2290
2291 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002292 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002293 }
2294
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002295 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002296 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002297 mReader->requestRefreshConfiguration(changes);
2298 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002299 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002300 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2301 }
2302
arthurhungdcef2dc2020-08-11 14:47:50 +08002303 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2304 const std::string& location, int32_t eventHubId,
2305 Flags<InputDeviceClass> classes) {
2306 InputDeviceIdentifier identifier;
2307 identifier.name = name;
2308 identifier.location = location;
2309 std::shared_ptr<InputDevice> device =
2310 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2311 identifier);
2312 mReader->pushNextDevice(device);
2313 mFakeEventHub->addDevice(eventHubId, name, classes);
2314 mReader->loopOnce();
2315 return device;
2316 }
2317
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002318 template <class T, typename... Args>
2319 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002320 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002321 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002322 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002323 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002324 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325 }
2326
2327 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002328 int32_t orientation, const std::string& uniqueId,
2329 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002330 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002331 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002332 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2333 }
2334
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002335 void clearViewports() {
2336 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337 }
2338
arthurhungdcef2dc2020-08-11 14:47:50 +08002339 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002340 RawEvent event;
2341 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002342 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002343 event.type = type;
2344 event.code = code;
2345 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002346 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002347 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002348 }
2349
2350 static void assertMotionRange(const InputDeviceInfo& info,
2351 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2352 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002353 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002354 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2355 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2356 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2357 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2358 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2359 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2360 }
2361
2362 static void assertPointerCoords(const PointerCoords& coords,
2363 float x, float y, float pressure, float size,
2364 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2365 float orientation, float distance) {
2366 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2367 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2368 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2369 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2370 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2371 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2372 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2373 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2374 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2375 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2376 }
2377
Michael Wright17db18e2020-06-26 20:51:44 +01002378 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002380 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381 ASSERT_NEAR(x, actualX, 1);
2382 ASSERT_NEAR(y, actualY, 1);
2383 }
2384};
2385
2386const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002387const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002388const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2390const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002391const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2392 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002393const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394
2395// --- SwitchInputMapperTest ---
2396
2397class SwitchInputMapperTest : public InputMapperTest {
2398protected:
2399};
2400
2401TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002402 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002404 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405}
2406
2407TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002408 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002410 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002411 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002413 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002414 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415}
2416
2417TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002418 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002420 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2421 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2422 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2423 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424
2425 NotifySwitchArgs args;
2426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2427 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002428 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2429 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 args.switchMask);
2431 ASSERT_EQ(uint32_t(0), args.policyFlags);
2432}
2433
2434
2435// --- KeyboardInputMapperTest ---
2436
2437class KeyboardInputMapperTest : public InputMapperTest {
2438protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002439 const std::string UNIQUE_ID = "local:0";
2440
2441 void prepareDisplay(int32_t orientation);
2442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002443 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002444 int32_t originalKeyCode, int32_t rotatedKeyCode,
2445 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002446};
2447
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002448/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2449 * orientation.
2450 */
2451void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002452 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2453 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002454}
2455
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002456void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002457 int32_t originalScanCode, int32_t originalKeyCode,
2458 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459 NotifyKeyArgs args;
2460
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002461 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2463 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2464 ASSERT_EQ(originalScanCode, args.scanCode);
2465 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002466 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002468 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2470 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2471 ASSERT_EQ(originalScanCode, args.scanCode);
2472 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002473 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474}
2475
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002477 KeyboardInputMapper& mapper =
2478 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2479 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002481 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002482}
2483
2484TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2485 const int32_t USAGE_A = 0x070004;
2486 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002487 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2488 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002490 KeyboardInputMapper& mapper =
2491 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2492 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002493 // Initial metastate to AMETA_NONE.
2494 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2495 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496
2497 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002498 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002499 NotifyKeyArgs args;
2500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2501 ASSERT_EQ(DEVICE_ID, args.deviceId);
2502 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2503 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2504 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2505 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2506 ASSERT_EQ(KEY_HOME, args.scanCode);
2507 ASSERT_EQ(AMETA_NONE, args.metaState);
2508 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2509 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2510 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2511
2512 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002513 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2515 ASSERT_EQ(DEVICE_ID, args.deviceId);
2516 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2517 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2518 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2519 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2520 ASSERT_EQ(KEY_HOME, args.scanCode);
2521 ASSERT_EQ(AMETA_NONE, args.metaState);
2522 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2523 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2524 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2525
2526 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002527 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2528 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2530 ASSERT_EQ(DEVICE_ID, args.deviceId);
2531 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2532 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2533 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2534 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2535 ASSERT_EQ(0, args.scanCode);
2536 ASSERT_EQ(AMETA_NONE, args.metaState);
2537 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2538 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2539 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2540
2541 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002542 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2543 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2545 ASSERT_EQ(DEVICE_ID, args.deviceId);
2546 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2547 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2548 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2549 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2550 ASSERT_EQ(0, args.scanCode);
2551 ASSERT_EQ(AMETA_NONE, args.metaState);
2552 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2553 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2554 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2555
2556 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002557 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2558 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2560 ASSERT_EQ(DEVICE_ID, args.deviceId);
2561 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2562 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2563 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2564 ASSERT_EQ(0, args.keyCode);
2565 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2566 ASSERT_EQ(AMETA_NONE, args.metaState);
2567 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2568 ASSERT_EQ(0U, args.policyFlags);
2569 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2570
2571 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002572 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2573 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2575 ASSERT_EQ(DEVICE_ID, args.deviceId);
2576 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2577 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2578 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2579 ASSERT_EQ(0, args.keyCode);
2580 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2581 ASSERT_EQ(AMETA_NONE, args.metaState);
2582 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2583 ASSERT_EQ(0U, args.policyFlags);
2584 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2585}
2586
2587TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002588 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2589 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002591 KeyboardInputMapper& mapper =
2592 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2593 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002594
arthurhungc903df12020-08-11 15:08:42 +08002595 // Initial metastate to AMETA_NONE.
2596 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2597 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598
2599 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002600 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002601 NotifyKeyArgs args;
2602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2603 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002604 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002605 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606
2607 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002608 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2610 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002611 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002612
2613 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002614 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2616 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002617 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618
2619 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002620 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2622 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002623 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002624 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625}
2626
2627TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002628 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2629 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2630 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2631 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002633 KeyboardInputMapper& mapper =
2634 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2635 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002637 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2639 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2640 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2641 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2642 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2643 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2644 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2645 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2646}
2647
2648TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002649 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2650 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2651 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2652 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002655 KeyboardInputMapper& mapper =
2656 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2657 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002659 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002660 ASSERT_NO_FATAL_FAILURE(
2661 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2662 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2663 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2664 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2665 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2666 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2667 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002669 clearViewports();
2670 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002671 ASSERT_NO_FATAL_FAILURE(
2672 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2673 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2674 AKEYCODE_DPAD_UP, DISPLAY_ID));
2675 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2676 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2677 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2678 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002680 clearViewports();
2681 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002682 ASSERT_NO_FATAL_FAILURE(
2683 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2684 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2685 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2686 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2687 AKEYCODE_DPAD_UP, DISPLAY_ID));
2688 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2689 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002691 clearViewports();
2692 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002693 ASSERT_NO_FATAL_FAILURE(
2694 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2695 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2696 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2697 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2698 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2699 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2700 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002701
2702 // Special case: if orientation changes while key is down, we still emit the same keycode
2703 // in the key up as we did in the key down.
2704 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002705 clearViewports();
2706 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002707 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2709 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2710 ASSERT_EQ(KEY_UP, args.scanCode);
2711 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2712
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002713 clearViewports();
2714 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002715 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2717 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2718 ASSERT_EQ(KEY_UP, args.scanCode);
2719 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2720}
2721
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002722TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2723 // If the keyboard is not orientation aware,
2724 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002725 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002726
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002727 KeyboardInputMapper& mapper =
2728 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2729 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002730 NotifyKeyArgs args;
2731
2732 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002733 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002735 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2737 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2738
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002739 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002740 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002742 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2744 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2745}
2746
2747TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2748 // If the keyboard is orientation aware,
2749 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002750 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002751
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002752 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002753 KeyboardInputMapper& mapper =
2754 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2755 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002756 NotifyKeyArgs args;
2757
2758 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2759 // ^--- already checked by the previous test
2760
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002761 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002762 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002763 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002765 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2767 ASSERT_EQ(DISPLAY_ID, args.displayId);
2768
2769 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002770 clearViewports();
2771 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002772 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002773 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002775 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2777 ASSERT_EQ(newDisplayId, args.displayId);
2778}
2779
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002781 KeyboardInputMapper& mapper =
2782 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2783 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002785 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002786 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002788 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002789 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002790}
2791
2792TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002793 KeyboardInputMapper& mapper =
2794 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2795 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002797 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002798 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002800 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002801 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802}
2803
2804TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002805 KeyboardInputMapper& mapper =
2806 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2807 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002809 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810
2811 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2812 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002813 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814 ASSERT_TRUE(flags[0]);
2815 ASSERT_FALSE(flags[1]);
2816}
2817
2818TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002819 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2820 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2821 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2822 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2823 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2824 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 KeyboardInputMapper& mapper =
2827 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2828 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002829 // Initial metastate to AMETA_NONE.
2830 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2831 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832
2833 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002834 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2835 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2836 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837
2838 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002839 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2840 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002841 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2842 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2843 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002844 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845
2846 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002847 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2848 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002849 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2850 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2851 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002852 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002853
2854 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002855 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2856 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002857 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2858 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2859 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002860 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861
2862 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002863 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2864 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002865 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2866 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2867 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002868 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869
2870 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002871 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2872 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002873 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2874 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2875 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002876 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877
2878 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002879 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2880 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002881 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2882 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2883 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002884 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885}
2886
Arthur Hung2c9a3342019-07-23 14:18:59 +08002887TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2888 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002889 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2890 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2891 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2892 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002893
2894 // keyboard 2.
2895 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08002896 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002897 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002898 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08002899 std::shared_ptr<InputDevice> device2 =
2900 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
2901 Flags<InputDeviceClass>(0));
2902
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002903 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2904 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2905 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2906 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002907
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002908 KeyboardInputMapper& mapper =
2909 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2910 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002911
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002912 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002913 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002914 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002915 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2916 device2->reset(ARBITRARY_TIME);
2917
2918 // Prepared displays and associated info.
2919 constexpr uint8_t hdmi1 = 0;
2920 constexpr uint8_t hdmi2 = 1;
2921 const std::string SECONDARY_UNIQUE_ID = "local:1";
2922
2923 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2924 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2925
2926 // No associated display viewport found, should disable the device.
2927 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2928 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2929 ASSERT_FALSE(device2->isEnabled());
2930
2931 // Prepare second display.
2932 constexpr int32_t newDisplayId = 2;
2933 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002934 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002935 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002936 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002937 // Default device will reconfigure above, need additional reconfiguration for another device.
2938 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2939 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2940
2941 // Device should be enabled after the associated display is found.
2942 ASSERT_TRUE(mDevice->isEnabled());
2943 ASSERT_TRUE(device2->isEnabled());
2944
2945 // Test pad key events
2946 ASSERT_NO_FATAL_FAILURE(
2947 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2948 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2949 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2950 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2951 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2952 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2953 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2954
2955 ASSERT_NO_FATAL_FAILURE(
2956 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2957 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2958 AKEYCODE_DPAD_RIGHT, newDisplayId));
2959 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2960 AKEYCODE_DPAD_DOWN, newDisplayId));
2961 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2962 AKEYCODE_DPAD_LEFT, newDisplayId));
2963}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964
arthurhungc903df12020-08-11 15:08:42 +08002965TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
2966 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2967 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2968 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2969 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2970 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2971 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2972
2973 KeyboardInputMapper& mapper =
2974 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2975 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2976 // Initial metastate to AMETA_NONE.
2977 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2978 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
2979
2980 // Initialization should have turned all of the lights off.
2981 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2982 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2983 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
2984
2985 // Toggle caps lock on.
2986 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2987 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
2988 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2989 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
2990
2991 // Toggle num lock on.
2992 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2993 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
2994 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2995 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
2996
2997 // Toggle scroll lock on.
2998 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2999 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3000 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3001 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3002
3003 mFakeEventHub->removeDevice(EVENTHUB_ID);
3004 mReader->loopOnce();
3005
3006 // keyboard 2 should default toggle keys.
3007 const std::string USB2 = "USB2";
3008 const std::string DEVICE_NAME2 = "KEYBOARD2";
3009 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3010 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3011 std::shared_ptr<InputDevice> device2 =
3012 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3013 Flags<InputDeviceClass>(0));
3014 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3015 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3016 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3017 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3018 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3019 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3020
3021 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3022 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3023 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3024 device2->reset(ARBITRARY_TIME);
3025
3026 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3027 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3028 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
3029 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3030}
3031
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003032// --- KeyboardInputMapperTest_ExternalDevice ---
3033
3034class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3035protected:
3036 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07003037 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003038 }
3039};
3040
3041TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003042 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3043 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003044
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003045 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3046 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3047 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3048 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003050 KeyboardInputMapper& mapper =
3051 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3052 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003053
3054 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3055 NotifyKeyArgs args;
3056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3057 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3058
3059 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3061 ASSERT_EQ(uint32_t(0), args.policyFlags);
3062
3063 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3065 ASSERT_EQ(uint32_t(0), args.policyFlags);
3066
3067 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3069 ASSERT_EQ(uint32_t(0), args.policyFlags);
3070
3071 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3073 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3074
3075 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3077 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3078}
3079
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003080TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003081 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003082
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003083 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3084 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3085 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003086
Powei Fengd041c5d2019-05-03 17:11:33 -07003087 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 KeyboardInputMapper& mapper =
3089 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3090 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003091
3092 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3093 NotifyKeyArgs args;
3094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3095 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3096
3097 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3099 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3100
3101 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3103 ASSERT_EQ(uint32_t(0), args.policyFlags);
3104
3105 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3107 ASSERT_EQ(uint32_t(0), args.policyFlags);
3108
3109 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3111 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3112
3113 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3115 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3116}
3117
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118// --- CursorInputMapperTest ---
3119
3120class CursorInputMapperTest : public InputMapperTest {
3121protected:
3122 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3123
Michael Wright17db18e2020-06-26 20:51:44 +01003124 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125
Prabir Pradhan28efc192019-11-05 01:10:04 +00003126 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127 InputMapperTest::SetUp();
3128
Michael Wright17db18e2020-06-26 20:51:44 +01003129 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003130 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131 }
3132
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003133 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3134 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003135
3136 void prepareDisplay(int32_t orientation) {
3137 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003138 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003139 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3140 orientation, uniqueId, NO_PORT, viewportType);
3141 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142};
3143
3144const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003146void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3147 int32_t originalY, int32_t rotatedX,
3148 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149 NotifyMotionArgs args;
3150
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003151 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3152 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3153 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3157 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3158 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3159 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3160}
3161
3162TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003164 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003166 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167}
3168
3169TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003170 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003171 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003173 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174}
3175
3176TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003178 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003179
3180 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003181 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003182
3183 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003184 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3185 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003186 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3187 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3188
3189 // When the bounds are set, then there should be a valid motion range.
3190 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3191
3192 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003193 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
3195 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3196 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3197 1, 800 - 1, 0.0f, 0.0f));
3198 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3199 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3200 2, 480 - 1, 0.0f, 0.0f));
3201 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3202 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3203 0.0f, 1.0f, 0.0f, 0.0f));
3204}
3205
3206TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003208 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209
3210 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003211 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212
3213 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3214 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3215 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3216 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3217 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3218 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3219 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3220 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3221 0.0f, 1.0f, 0.0f, 0.0f));
3222}
3223
3224TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003226 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227
arthurhungdcef2dc2020-08-11 14:47:50 +08003228 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229
3230 NotifyMotionArgs args;
3231
3232 // Button press.
3233 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003234 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3235 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3237 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3238 ASSERT_EQ(DEVICE_ID, args.deviceId);
3239 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3240 ASSERT_EQ(uint32_t(0), args.policyFlags);
3241 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3242 ASSERT_EQ(0, args.flags);
3243 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3244 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3245 ASSERT_EQ(0, args.edgeFlags);
3246 ASSERT_EQ(uint32_t(1), args.pointerCount);
3247 ASSERT_EQ(0, args.pointerProperties[0].id);
3248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3250 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3251 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3252 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3253 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3254
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3256 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3257 ASSERT_EQ(DEVICE_ID, args.deviceId);
3258 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3259 ASSERT_EQ(uint32_t(0), args.policyFlags);
3260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3261 ASSERT_EQ(0, args.flags);
3262 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3263 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3264 ASSERT_EQ(0, args.edgeFlags);
3265 ASSERT_EQ(uint32_t(1), args.pointerCount);
3266 ASSERT_EQ(0, args.pointerProperties[0].id);
3267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3269 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3270 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3271 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3272 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3273
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003275 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3276 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3278 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3279 ASSERT_EQ(DEVICE_ID, args.deviceId);
3280 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3281 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003282 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3283 ASSERT_EQ(0, args.flags);
3284 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3285 ASSERT_EQ(0, args.buttonState);
3286 ASSERT_EQ(0, args.edgeFlags);
3287 ASSERT_EQ(uint32_t(1), args.pointerCount);
3288 ASSERT_EQ(0, args.pointerProperties[0].id);
3289 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3291 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3292 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3293 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3294 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3295
3296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3297 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3298 ASSERT_EQ(DEVICE_ID, args.deviceId);
3299 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3300 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3302 ASSERT_EQ(0, args.flags);
3303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3304 ASSERT_EQ(0, args.buttonState);
3305 ASSERT_EQ(0, args.edgeFlags);
3306 ASSERT_EQ(uint32_t(1), args.pointerCount);
3307 ASSERT_EQ(0, args.pointerProperties[0].id);
3308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3310 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3311 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3312 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3313 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3314}
3315
3316TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003318 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319
3320 NotifyMotionArgs args;
3321
3322 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003323 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3324 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3328 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3329
3330 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003331 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3332 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3336 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3337}
3338
3339TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003341 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342
3343 NotifyMotionArgs args;
3344
3345 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003346 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3347 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3349 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3351 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3352
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3356 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3357
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003359 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3360 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3364 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3365
3366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3369 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3370}
3371
3372TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003374 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375
3376 NotifyMotionArgs args;
3377
3378 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003379 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3380 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3381 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3382 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3384 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3386 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3387 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3388
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3390 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3392 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3393 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3394
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003396 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3397 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3398 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3402 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3403 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3404
3405 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003406 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3407 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3411 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3412
3413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3416 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3417}
3418
3419TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003421 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003423 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3425 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3426 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3427 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3428 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3429 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3431 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3432}
3433
3434TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435 addConfigurationProperty("cursor.mode", "navigation");
3436 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003437 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003439 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3441 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3442 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3443 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3444 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3445 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3446 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3447 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3448
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003449 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003450 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3451 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3452 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3453 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3454 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3455 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3456 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3457 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3458
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003459 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3461 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3462 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3463 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3464 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3465 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3466 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3467 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3468
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003469 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003470 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3471 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3472 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3473 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3474 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3475 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3476 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3478}
3479
3480TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003482 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
3484 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3485 mFakePointerController->setPosition(100, 200);
3486 mFakePointerController->setButtonState(0);
3487
3488 NotifyMotionArgs motionArgs;
3489 NotifyKeyArgs keyArgs;
3490
3491 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003492 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3493 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3495 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3496 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3497 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3499 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3500
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3502 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3503 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3504 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3506 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3507
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003508 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3509 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003511 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512 ASSERT_EQ(0, motionArgs.buttonState);
3513 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3515 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3516
3517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003518 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519 ASSERT_EQ(0, motionArgs.buttonState);
3520 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3522 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3523
3524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003526 ASSERT_EQ(0, motionArgs.buttonState);
3527 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3529 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3530
3531 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003532 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3533 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3534 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3536 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3537 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3538 motionArgs.buttonState);
3539 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3540 mFakePointerController->getButtonState());
3541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3542 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3543
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3546 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3547 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3548 mFakePointerController->getButtonState());
3549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3550 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3551
3552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3553 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3554 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3555 motionArgs.buttonState);
3556 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3557 mFakePointerController->getButtonState());
3558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3559 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3560
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003561 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3562 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3566 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3568 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3569
3570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003572 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3573 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3575 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3576
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003577 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3578 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003580 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3581 ASSERT_EQ(0, motionArgs.buttonState);
3582 ASSERT_EQ(0, mFakePointerController->getButtonState());
3583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3584 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 -08003585 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3586 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003587
3588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589 ASSERT_EQ(0, motionArgs.buttonState);
3590 ASSERT_EQ(0, mFakePointerController->getButtonState());
3591 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3593 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 -08003594
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3596 ASSERT_EQ(0, motionArgs.buttonState);
3597 ASSERT_EQ(0, mFakePointerController->getButtonState());
3598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3600 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3601
3602 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003603 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3604 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3606 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3607 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003608
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003610 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3612 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3614 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3615
3616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3618 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3619 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3621 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3622
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003623 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3624 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003626 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 ASSERT_EQ(0, motionArgs.buttonState);
3628 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3630 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3631
3632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003634 ASSERT_EQ(0, motionArgs.buttonState);
3635 ASSERT_EQ(0, mFakePointerController->getButtonState());
3636
Michael Wrightd02c5b62014-02-10 15:10:22 -08003637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3638 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3640 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3641 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3642
3643 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003644 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3645 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3647 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3648 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003649
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3653 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3655 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3656
3657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3658 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3659 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3660 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3662 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3663
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003664 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3665 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 ASSERT_EQ(0, motionArgs.buttonState);
3669 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3671 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003672
3673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3675 ASSERT_EQ(0, motionArgs.buttonState);
3676 ASSERT_EQ(0, mFakePointerController->getButtonState());
3677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3678 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3679
Michael Wrightd02c5b62014-02-10 15:10:22 -08003680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3681 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3682 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3683
3684 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003685 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3686 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3688 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3689 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003690
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003692 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3694 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3696 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3697
3698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3700 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3701 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3703 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3704
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003705 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3706 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709 ASSERT_EQ(0, motionArgs.buttonState);
3710 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3712 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 -08003713
3714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3715 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3716 ASSERT_EQ(0, motionArgs.buttonState);
3717 ASSERT_EQ(0, mFakePointerController->getButtonState());
3718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3719 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3720
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3722 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3723 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3724
3725 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003726 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3727 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3729 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3730 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003731
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3735 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3737 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3738
3739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3740 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3741 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3742 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3744 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3745
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003746 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3747 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003749 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 ASSERT_EQ(0, motionArgs.buttonState);
3751 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3753 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 -08003754
3755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3757 ASSERT_EQ(0, motionArgs.buttonState);
3758 ASSERT_EQ(0, mFakePointerController->getButtonState());
3759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3760 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3761
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3763 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3764 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3765}
3766
3767TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003769 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770
3771 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3772 mFakePointerController->setPosition(100, 200);
3773 mFakePointerController->setButtonState(0);
3774
3775 NotifyMotionArgs args;
3776
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003777 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3778 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3779 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003781 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3782 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3784 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 +01003785 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003786}
3787
3788TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003789 addConfigurationProperty("cursor.mode", "pointer");
3790 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003791 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003792
3793 NotifyDeviceResetArgs resetArgs;
3794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3795 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3796 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3797
3798 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3799 mFakePointerController->setPosition(100, 200);
3800 mFakePointerController->setButtonState(0);
3801
3802 NotifyMotionArgs args;
3803
3804 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003805 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3806 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3807 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3809 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3812 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 +01003813 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003814
3815 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003816 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3817 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3819 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3820 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3822 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3824 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3825 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3827 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3828
3829 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003830 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3831 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3833 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3834 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3836 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3838 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3839 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3841 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3842
3843 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003844 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3845 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3846 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3848 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3851 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 +01003852 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003853
3854 // Disable pointer capture and check that the device generation got bumped
3855 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08003856 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003857 mFakePolicy->setPointerCapture(false);
3858 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08003859 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003860
3861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3862 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3863 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3864
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003865 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3866 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3867 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3869 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3872 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 +01003873 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874}
3875
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003876TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003877 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003878
Garfield Tan888a6a42020-01-09 11:39:16 -08003879 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003880 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003881 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3882 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003883 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003884 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3885 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3886
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003887 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3888 mFakePointerController->setPosition(100, 200);
3889 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003890
3891 NotifyMotionArgs args;
3892 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3893 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3894 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3896 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3897 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3899 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 +01003900 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003901 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3902}
3903
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904// --- TouchInputMapperTest ---
3905
3906class TouchInputMapperTest : public InputMapperTest {
3907protected:
3908 static const int32_t RAW_X_MIN;
3909 static const int32_t RAW_X_MAX;
3910 static const int32_t RAW_Y_MIN;
3911 static const int32_t RAW_Y_MAX;
3912 static const int32_t RAW_TOUCH_MIN;
3913 static const int32_t RAW_TOUCH_MAX;
3914 static const int32_t RAW_TOOL_MIN;
3915 static const int32_t RAW_TOOL_MAX;
3916 static const int32_t RAW_PRESSURE_MIN;
3917 static const int32_t RAW_PRESSURE_MAX;
3918 static const int32_t RAW_ORIENTATION_MIN;
3919 static const int32_t RAW_ORIENTATION_MAX;
3920 static const int32_t RAW_DISTANCE_MIN;
3921 static const int32_t RAW_DISTANCE_MAX;
3922 static const int32_t RAW_TILT_MIN;
3923 static const int32_t RAW_TILT_MAX;
3924 static const int32_t RAW_ID_MIN;
3925 static const int32_t RAW_ID_MAX;
3926 static const int32_t RAW_SLOT_MIN;
3927 static const int32_t RAW_SLOT_MAX;
3928 static const float X_PRECISION;
3929 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003930 static const float X_PRECISION_VIRTUAL;
3931 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003932
3933 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003934 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935
3936 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3937
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003938 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003939 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003940
Michael Wrightd02c5b62014-02-10 15:10:22 -08003941 enum Axes {
3942 POSITION = 1 << 0,
3943 TOUCH = 1 << 1,
3944 TOOL = 1 << 2,
3945 PRESSURE = 1 << 3,
3946 ORIENTATION = 1 << 4,
3947 MINOR = 1 << 5,
3948 ID = 1 << 6,
3949 DISTANCE = 1 << 7,
3950 TILT = 1 << 8,
3951 SLOT = 1 << 9,
3952 TOOL_TYPE = 1 << 10,
3953 };
3954
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003955 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3956 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003957 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003959 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003960 int32_t toRawX(float displayX);
3961 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003962 float toCookedX(float rawX, float rawY);
3963 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003964 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003965 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003967 float toDisplayY(int32_t rawY, int32_t displayHeight);
3968
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969};
3970
3971const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3972const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3973const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3974const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3975const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3976const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3977const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3978const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003979const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3980const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3982const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3983const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3984const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3985const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3986const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3987const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3988const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3989const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3990const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3991const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3992const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003993const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3994 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3995const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3996 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003997const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3998 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999
4000const float TouchInputMapperTest::GEOMETRIC_SCALE =
4001 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4002 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4003
4004const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4005 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4006 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4007};
4008
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004009void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004010 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4011 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004012}
4013
4014void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4015 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4016 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017}
4018
Santos Cordonfa5cf462017-04-05 10:37:00 -07004019void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004020 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4021 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4022 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004023}
4024
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004026 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4027 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4028 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4029 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030}
4031
Jason Gerecke489fda82012-09-07 17:19:40 -07004032void TouchInputMapperTest::prepareLocationCalibration() {
4033 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4034}
4035
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036int32_t TouchInputMapperTest::toRawX(float displayX) {
4037 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4038}
4039
4040int32_t TouchInputMapperTest::toRawY(float displayY) {
4041 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4042}
4043
Jason Gerecke489fda82012-09-07 17:19:40 -07004044float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4045 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4046 return rawX;
4047}
4048
4049float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4050 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4051 return rawY;
4052}
4053
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004055 return toDisplayX(rawX, DISPLAY_WIDTH);
4056}
4057
4058float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4059 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060}
4061
4062float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004063 return toDisplayY(rawY, DISPLAY_HEIGHT);
4064}
4065
4066float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4067 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068}
4069
4070
4071// --- SingleTouchInputMapperTest ---
4072
4073class SingleTouchInputMapperTest : public TouchInputMapperTest {
4074protected:
4075 void prepareButtons();
4076 void prepareAxes(int axes);
4077
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004078 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4079 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4080 void processUp(SingleTouchInputMapper& mappery);
4081 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4082 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4083 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4084 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4085 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4086 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087};
4088
4089void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004090 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091}
4092
4093void SingleTouchInputMapperTest::prepareAxes(int axes) {
4094 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004095 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4096 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 }
4098 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004099 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4100 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101 }
4102 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4104 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105 }
4106 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004107 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4108 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109 }
4110 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004111 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 }
4114}
4115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004116void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004117 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4118 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4119 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120}
4121
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004122void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004123 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4124 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125}
4126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004127void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004128 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004129}
4130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004131void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004132 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133}
4134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004135void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4136 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004137 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138}
4139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004140void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004141 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142}
4143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004144void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4145 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004146 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4147 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148}
4149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004150void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4151 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004152 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153}
4154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004155void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004156 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157}
4158
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 prepareButtons();
4161 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004162 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004164 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165}
4166
4167TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004168 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4169 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 prepareButtons();
4171 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004172 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004174 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175}
4176
4177TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 prepareButtons();
4179 prepareAxes(POSITION);
4180 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004181 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004183 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184}
4185
4186TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 prepareButtons();
4188 prepareAxes(POSITION);
4189 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004190 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004192 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193}
4194
4195TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 addConfigurationProperty("touch.deviceType", "touchScreen");
4197 prepareDisplay(DISPLAY_ORIENTATION_0);
4198 prepareButtons();
4199 prepareAxes(POSITION);
4200 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004201 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202
4203 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004204 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205
4206 // Virtual key is down.
4207 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4208 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4209 processDown(mapper, x, y);
4210 processSync(mapper);
4211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004213 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214
4215 // Virtual key is up.
4216 processUp(mapper);
4217 processSync(mapper);
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4219
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004220 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221}
4222
4223TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224 addConfigurationProperty("touch.deviceType", "touchScreen");
4225 prepareDisplay(DISPLAY_ORIENTATION_0);
4226 prepareButtons();
4227 prepareAxes(POSITION);
4228 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004229 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230
4231 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004232 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233
4234 // Virtual key is down.
4235 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4236 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4237 processDown(mapper, x, y);
4238 processSync(mapper);
4239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4240
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004241 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242
4243 // Virtual key is up.
4244 processUp(mapper);
4245 processSync(mapper);
4246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004248 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249}
4250
4251TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 addConfigurationProperty("touch.deviceType", "touchScreen");
4253 prepareDisplay(DISPLAY_ORIENTATION_0);
4254 prepareButtons();
4255 prepareAxes(POSITION);
4256 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004257 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258
4259 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4260 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004261 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 ASSERT_TRUE(flags[0]);
4263 ASSERT_FALSE(flags[1]);
4264}
4265
4266TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 addConfigurationProperty("touch.deviceType", "touchScreen");
4268 prepareDisplay(DISPLAY_ORIENTATION_0);
4269 prepareButtons();
4270 prepareAxes(POSITION);
4271 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004272 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273
arthurhungdcef2dc2020-08-11 14:47:50 +08004274 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275
4276 NotifyKeyArgs args;
4277
4278 // Press virtual key.
4279 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4280 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4281 processDown(mapper, x, y);
4282 processSync(mapper);
4283
4284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4285 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4286 ASSERT_EQ(DEVICE_ID, args.deviceId);
4287 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4288 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4289 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4290 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4291 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4292 ASSERT_EQ(KEY_HOME, args.scanCode);
4293 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4294 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4295
4296 // Release virtual key.
4297 processUp(mapper);
4298 processSync(mapper);
4299
4300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4301 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4302 ASSERT_EQ(DEVICE_ID, args.deviceId);
4303 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4304 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4305 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4306 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4307 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4308 ASSERT_EQ(KEY_HOME, args.scanCode);
4309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4310 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4311
4312 // Should not have sent any motions.
4313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4314}
4315
4316TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 addConfigurationProperty("touch.deviceType", "touchScreen");
4318 prepareDisplay(DISPLAY_ORIENTATION_0);
4319 prepareButtons();
4320 prepareAxes(POSITION);
4321 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004322 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323
arthurhungdcef2dc2020-08-11 14:47:50 +08004324 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325
4326 NotifyKeyArgs keyArgs;
4327
4328 // Press virtual key.
4329 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4330 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4331 processDown(mapper, x, y);
4332 processSync(mapper);
4333
4334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4335 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4336 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4337 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4338 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4339 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4340 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4341 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4342 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4344 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4345
4346 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4347 // into the display area.
4348 y -= 100;
4349 processMove(mapper, x, y);
4350 processSync(mapper);
4351
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4353 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4354 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4355 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4356 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4357 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4358 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4359 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4360 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4361 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4362 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4363 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4364
4365 NotifyMotionArgs motionArgs;
4366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4367 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4368 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4369 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4370 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4371 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4372 ASSERT_EQ(0, motionArgs.flags);
4373 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4374 ASSERT_EQ(0, motionArgs.buttonState);
4375 ASSERT_EQ(0, motionArgs.edgeFlags);
4376 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4377 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4380 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4381 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4382 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4383 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4384
4385 // Keep moving out of bounds. Should generate a pointer move.
4386 y -= 50;
4387 processMove(mapper, x, y);
4388 processSync(mapper);
4389
4390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4391 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4392 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4393 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4394 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4396 ASSERT_EQ(0, motionArgs.flags);
4397 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4398 ASSERT_EQ(0, motionArgs.buttonState);
4399 ASSERT_EQ(0, motionArgs.edgeFlags);
4400 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4401 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4404 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4405 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4406 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4407 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4408
4409 // Release out of bounds. Should generate a pointer up.
4410 processUp(mapper);
4411 processSync(mapper);
4412
4413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4414 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4415 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4416 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4417 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4418 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4419 ASSERT_EQ(0, motionArgs.flags);
4420 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4421 ASSERT_EQ(0, motionArgs.buttonState);
4422 ASSERT_EQ(0, motionArgs.edgeFlags);
4423 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4424 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4425 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4427 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4428 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4429 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4430 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4431
4432 // Should not have sent any more keys or motions.
4433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4435}
4436
4437TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438 addConfigurationProperty("touch.deviceType", "touchScreen");
4439 prepareDisplay(DISPLAY_ORIENTATION_0);
4440 prepareButtons();
4441 prepareAxes(POSITION);
4442 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004443 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444
arthurhungdcef2dc2020-08-11 14:47:50 +08004445 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446
4447 NotifyMotionArgs motionArgs;
4448
4449 // Initially go down out of bounds.
4450 int32_t x = -10;
4451 int32_t y = -10;
4452 processDown(mapper, x, y);
4453 processSync(mapper);
4454
4455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4456
4457 // Move into the display area. Should generate a pointer down.
4458 x = 50;
4459 y = 75;
4460 processMove(mapper, x, y);
4461 processSync(mapper);
4462
4463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4464 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4465 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4466 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4467 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4469 ASSERT_EQ(0, motionArgs.flags);
4470 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4471 ASSERT_EQ(0, motionArgs.buttonState);
4472 ASSERT_EQ(0, motionArgs.edgeFlags);
4473 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4474 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4477 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4478 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4479 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4480 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4481
4482 // Release. Should generate a pointer up.
4483 processUp(mapper);
4484 processSync(mapper);
4485
4486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4487 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4488 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4489 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4490 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4491 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4492 ASSERT_EQ(0, motionArgs.flags);
4493 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4494 ASSERT_EQ(0, motionArgs.buttonState);
4495 ASSERT_EQ(0, motionArgs.edgeFlags);
4496 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4497 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4500 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4501 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4502 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4503 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4504
4505 // Should not have sent any more keys or motions.
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4508}
4509
Santos Cordonfa5cf462017-04-05 10:37:00 -07004510TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004511 addConfigurationProperty("touch.deviceType", "touchScreen");
4512 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4513
4514 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4515 prepareButtons();
4516 prepareAxes(POSITION);
4517 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004518 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004519
arthurhungdcef2dc2020-08-11 14:47:50 +08004520 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004521
4522 NotifyMotionArgs motionArgs;
4523
4524 // Down.
4525 int32_t x = 100;
4526 int32_t y = 125;
4527 processDown(mapper, x, y);
4528 processSync(mapper);
4529
4530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4531 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4532 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4533 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4534 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4535 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4536 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4537 ASSERT_EQ(0, motionArgs.flags);
4538 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4539 ASSERT_EQ(0, motionArgs.buttonState);
4540 ASSERT_EQ(0, motionArgs.edgeFlags);
4541 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4542 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4545 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4546 1, 0, 0, 0, 0, 0, 0, 0));
4547 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4548 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4549 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4550
4551 // Move.
4552 x += 50;
4553 y += 75;
4554 processMove(mapper, x, y);
4555 processSync(mapper);
4556
4557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4558 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4559 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4560 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4561 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4562 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4564 ASSERT_EQ(0, motionArgs.flags);
4565 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4566 ASSERT_EQ(0, motionArgs.buttonState);
4567 ASSERT_EQ(0, motionArgs.edgeFlags);
4568 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4572 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4573 1, 0, 0, 0, 0, 0, 0, 0));
4574 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4575 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4576 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4577
4578 // Up.
4579 processUp(mapper);
4580 processSync(mapper);
4581
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4583 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4584 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4585 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4586 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4587 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4588 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4589 ASSERT_EQ(0, motionArgs.flags);
4590 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4591 ASSERT_EQ(0, motionArgs.buttonState);
4592 ASSERT_EQ(0, motionArgs.edgeFlags);
4593 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4594 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4597 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4598 1, 0, 0, 0, 0, 0, 0, 0));
4599 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4600 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4601 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4602
4603 // Should not have sent any more keys or motions.
4604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4606}
4607
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609 addConfigurationProperty("touch.deviceType", "touchScreen");
4610 prepareDisplay(DISPLAY_ORIENTATION_0);
4611 prepareButtons();
4612 prepareAxes(POSITION);
4613 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004614 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615
arthurhungdcef2dc2020-08-11 14:47:50 +08004616 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617
4618 NotifyMotionArgs motionArgs;
4619
4620 // Down.
4621 int32_t x = 100;
4622 int32_t y = 125;
4623 processDown(mapper, x, y);
4624 processSync(mapper);
4625
4626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4628 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4629 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4630 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4631 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4632 ASSERT_EQ(0, motionArgs.flags);
4633 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4634 ASSERT_EQ(0, motionArgs.buttonState);
4635 ASSERT_EQ(0, motionArgs.edgeFlags);
4636 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4637 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4640 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4641 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4642 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4643 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4644
4645 // Move.
4646 x += 50;
4647 y += 75;
4648 processMove(mapper, x, y);
4649 processSync(mapper);
4650
4651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4652 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4653 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4654 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4655 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4657 ASSERT_EQ(0, motionArgs.flags);
4658 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4659 ASSERT_EQ(0, motionArgs.buttonState);
4660 ASSERT_EQ(0, motionArgs.edgeFlags);
4661 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4665 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4666 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4667 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4668 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4669
4670 // Up.
4671 processUp(mapper);
4672 processSync(mapper);
4673
4674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4675 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4676 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4677 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4678 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4679 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4680 ASSERT_EQ(0, motionArgs.flags);
4681 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4682 ASSERT_EQ(0, motionArgs.buttonState);
4683 ASSERT_EQ(0, motionArgs.edgeFlags);
4684 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4685 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4686 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4688 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4689 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4690 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4691 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4692
4693 // Should not have sent any more keys or motions.
4694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4696}
4697
4698TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 addConfigurationProperty("touch.deviceType", "touchScreen");
4700 prepareButtons();
4701 prepareAxes(POSITION);
4702 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004703 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704
4705 NotifyMotionArgs args;
4706
4707 // Rotation 90.
4708 prepareDisplay(DISPLAY_ORIENTATION_90);
4709 processDown(mapper, toRawX(50), toRawY(75));
4710 processSync(mapper);
4711
4712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4713 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4714 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4715
4716 processUp(mapper);
4717 processSync(mapper);
4718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4719}
4720
4721TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722 addConfigurationProperty("touch.deviceType", "touchScreen");
4723 prepareButtons();
4724 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004725 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726
4727 NotifyMotionArgs args;
4728
4729 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004730 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 prepareDisplay(DISPLAY_ORIENTATION_0);
4732 processDown(mapper, toRawX(50), toRawY(75));
4733 processSync(mapper);
4734
4735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4736 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4737 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4738
4739 processUp(mapper);
4740 processSync(mapper);
4741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4742
4743 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004744 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745 prepareDisplay(DISPLAY_ORIENTATION_90);
4746 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4747 processSync(mapper);
4748
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4750 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4751 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4752
4753 processUp(mapper);
4754 processSync(mapper);
4755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4756
4757 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004758 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759 prepareDisplay(DISPLAY_ORIENTATION_180);
4760 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4761 processSync(mapper);
4762
4763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4764 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4765 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4766
4767 processUp(mapper);
4768 processSync(mapper);
4769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4770
4771 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004772 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773 prepareDisplay(DISPLAY_ORIENTATION_270);
4774 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4775 processSync(mapper);
4776
4777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4778 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4779 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4780
4781 processUp(mapper);
4782 processSync(mapper);
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4784}
4785
4786TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787 addConfigurationProperty("touch.deviceType", "touchScreen");
4788 prepareDisplay(DISPLAY_ORIENTATION_0);
4789 prepareButtons();
4790 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004791 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792
4793 // These calculations are based on the input device calibration documentation.
4794 int32_t rawX = 100;
4795 int32_t rawY = 200;
4796 int32_t rawPressure = 10;
4797 int32_t rawToolMajor = 12;
4798 int32_t rawDistance = 2;
4799 int32_t rawTiltX = 30;
4800 int32_t rawTiltY = 110;
4801
4802 float x = toDisplayX(rawX);
4803 float y = toDisplayY(rawY);
4804 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4805 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4806 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4807 float distance = float(rawDistance);
4808
4809 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4810 float tiltScale = M_PI / 180;
4811 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4812 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4813 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4814 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4815
4816 processDown(mapper, rawX, rawY);
4817 processPressure(mapper, rawPressure);
4818 processToolMajor(mapper, rawToolMajor);
4819 processDistance(mapper, rawDistance);
4820 processTilt(mapper, rawTiltX, rawTiltY);
4821 processSync(mapper);
4822
4823 NotifyMotionArgs args;
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4826 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4827 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4828}
4829
Jason Gerecke489fda82012-09-07 17:19:40 -07004830TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004831 addConfigurationProperty("touch.deviceType", "touchScreen");
4832 prepareDisplay(DISPLAY_ORIENTATION_0);
4833 prepareLocationCalibration();
4834 prepareButtons();
4835 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004836 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004837
4838 int32_t rawX = 100;
4839 int32_t rawY = 200;
4840
4841 float x = toDisplayX(toCookedX(rawX, rawY));
4842 float y = toDisplayY(toCookedY(rawX, rawY));
4843
4844 processDown(mapper, rawX, rawY);
4845 processSync(mapper);
4846
4847 NotifyMotionArgs args;
4848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4850 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4851}
4852
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 addConfigurationProperty("touch.deviceType", "touchScreen");
4855 prepareDisplay(DISPLAY_ORIENTATION_0);
4856 prepareButtons();
4857 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004858 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859
4860 NotifyMotionArgs motionArgs;
4861 NotifyKeyArgs keyArgs;
4862
4863 processDown(mapper, 100, 200);
4864 processSync(mapper);
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4866 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4867 ASSERT_EQ(0, motionArgs.buttonState);
4868
4869 // press BTN_LEFT, release BTN_LEFT
4870 processKey(mapper, BTN_LEFT, 1);
4871 processSync(mapper);
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4874 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4875
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4877 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4878 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4879
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880 processKey(mapper, BTN_LEFT, 0);
4881 processSync(mapper);
4882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004883 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004885
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004888 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889
4890 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4891 processKey(mapper, BTN_RIGHT, 1);
4892 processKey(mapper, BTN_MIDDLE, 1);
4893 processSync(mapper);
4894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4896 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4897 motionArgs.buttonState);
4898
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4900 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4901 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4902
4903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4904 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4905 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4906 motionArgs.buttonState);
4907
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 processKey(mapper, BTN_RIGHT, 0);
4909 processSync(mapper);
4910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004913
4914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004916 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917
4918 processKey(mapper, BTN_MIDDLE, 0);
4919 processSync(mapper);
4920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004921 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004923
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004926 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927
4928 // press BTN_BACK, release BTN_BACK
4929 processKey(mapper, BTN_BACK, 1);
4930 processSync(mapper);
4931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4932 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4933 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004934
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004937 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4938
4939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4941 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942
4943 processKey(mapper, BTN_BACK, 0);
4944 processSync(mapper);
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004946 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004948
4949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004951 ASSERT_EQ(0, motionArgs.buttonState);
4952
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4954 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4955 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4956
4957 // press BTN_SIDE, release BTN_SIDE
4958 processKey(mapper, BTN_SIDE, 1);
4959 processSync(mapper);
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4961 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4962 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004963
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004966 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4967
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4969 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4970 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971
4972 processKey(mapper, BTN_SIDE, 0);
4973 processSync(mapper);
4974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004975 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004977
4978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004980 ASSERT_EQ(0, motionArgs.buttonState);
4981
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4983 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4984 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4985
4986 // press BTN_FORWARD, release BTN_FORWARD
4987 processKey(mapper, BTN_FORWARD, 1);
4988 processSync(mapper);
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4990 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4991 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004992
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004995 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4996
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4998 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4999 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000
5001 processKey(mapper, BTN_FORWARD, 0);
5002 processSync(mapper);
5003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005006
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005009 ASSERT_EQ(0, motionArgs.buttonState);
5010
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5012 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5013 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5014
5015 // press BTN_EXTRA, release BTN_EXTRA
5016 processKey(mapper, BTN_EXTRA, 1);
5017 processSync(mapper);
5018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5019 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5020 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005021
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005024 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5025
5026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5028 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029
5030 processKey(mapper, BTN_EXTRA, 0);
5031 processSync(mapper);
5032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005035
5036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005038 ASSERT_EQ(0, motionArgs.buttonState);
5039
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5041 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5042 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5043
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5045
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046 // press BTN_STYLUS, release BTN_STYLUS
5047 processKey(mapper, BTN_STYLUS, 1);
5048 processSync(mapper);
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005051 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5052
5053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5055 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005056
5057 processKey(mapper, BTN_STYLUS, 0);
5058 processSync(mapper);
5059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005062
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005065 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066
5067 // press BTN_STYLUS2, release BTN_STYLUS2
5068 processKey(mapper, BTN_STYLUS2, 1);
5069 processSync(mapper);
5070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005072 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5073
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5076 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077
5078 processKey(mapper, BTN_STYLUS2, 0);
5079 processSync(mapper);
5080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005083
5084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005086 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087
5088 // release touch
5089 processUp(mapper);
5090 processSync(mapper);
5091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5092 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5093 ASSERT_EQ(0, motionArgs.buttonState);
5094}
5095
5096TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005097 addConfigurationProperty("touch.deviceType", "touchScreen");
5098 prepareDisplay(DISPLAY_ORIENTATION_0);
5099 prepareButtons();
5100 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005101 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102
5103 NotifyMotionArgs motionArgs;
5104
5105 // default tool type is finger
5106 processDown(mapper, 100, 200);
5107 processSync(mapper);
5108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5109 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5111
5112 // eraser
5113 processKey(mapper, BTN_TOOL_RUBBER, 1);
5114 processSync(mapper);
5115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5118
5119 // stylus
5120 processKey(mapper, BTN_TOOL_RUBBER, 0);
5121 processKey(mapper, BTN_TOOL_PEN, 1);
5122 processSync(mapper);
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5126
5127 // brush
5128 processKey(mapper, BTN_TOOL_PEN, 0);
5129 processKey(mapper, BTN_TOOL_BRUSH, 1);
5130 processSync(mapper);
5131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5134
5135 // pencil
5136 processKey(mapper, BTN_TOOL_BRUSH, 0);
5137 processKey(mapper, BTN_TOOL_PENCIL, 1);
5138 processSync(mapper);
5139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5142
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005143 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144 processKey(mapper, BTN_TOOL_PENCIL, 0);
5145 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5146 processSync(mapper);
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5150
5151 // mouse
5152 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5153 processKey(mapper, BTN_TOOL_MOUSE, 1);
5154 processSync(mapper);
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5158
5159 // lens
5160 processKey(mapper, BTN_TOOL_MOUSE, 0);
5161 processKey(mapper, BTN_TOOL_LENS, 1);
5162 processSync(mapper);
5163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5166
5167 // double-tap
5168 processKey(mapper, BTN_TOOL_LENS, 0);
5169 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5170 processSync(mapper);
5171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5174
5175 // triple-tap
5176 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5177 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5178 processSync(mapper);
5179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5182
5183 // quad-tap
5184 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5185 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5186 processSync(mapper);
5187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5190
5191 // finger
5192 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5193 processKey(mapper, BTN_TOOL_FINGER, 1);
5194 processSync(mapper);
5195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5198
5199 // stylus trumps finger
5200 processKey(mapper, BTN_TOOL_PEN, 1);
5201 processSync(mapper);
5202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5205
5206 // eraser trumps stylus
5207 processKey(mapper, BTN_TOOL_RUBBER, 1);
5208 processSync(mapper);
5209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5212
5213 // mouse trumps eraser
5214 processKey(mapper, BTN_TOOL_MOUSE, 1);
5215 processSync(mapper);
5216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5219
5220 // back to default tool type
5221 processKey(mapper, BTN_TOOL_MOUSE, 0);
5222 processKey(mapper, BTN_TOOL_RUBBER, 0);
5223 processKey(mapper, BTN_TOOL_PEN, 0);
5224 processKey(mapper, BTN_TOOL_FINGER, 0);
5225 processSync(mapper);
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5229}
5230
5231TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232 addConfigurationProperty("touch.deviceType", "touchScreen");
5233 prepareDisplay(DISPLAY_ORIENTATION_0);
5234 prepareButtons();
5235 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005236 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005237 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238
5239 NotifyMotionArgs motionArgs;
5240
5241 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5242 processKey(mapper, BTN_TOOL_FINGER, 1);
5243 processMove(mapper, 100, 200);
5244 processSync(mapper);
5245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5246 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5248 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5249
5250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5251 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5253 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5254
5255 // move a little
5256 processMove(mapper, 150, 250);
5257 processSync(mapper);
5258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5259 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5261 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5262
5263 // down when BTN_TOUCH is pressed, pressure defaults to 1
5264 processKey(mapper, BTN_TOUCH, 1);
5265 processSync(mapper);
5266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5269 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5270
5271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5272 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5274 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5275
5276 // up when BTN_TOUCH is released, hover restored
5277 processKey(mapper, BTN_TOUCH, 0);
5278 processSync(mapper);
5279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5280 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5282 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5283
5284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5285 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5287 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5288
5289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5290 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5292 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5293
5294 // exit hover when pointer goes away
5295 processKey(mapper, BTN_TOOL_FINGER, 0);
5296 processSync(mapper);
5297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5298 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5300 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5301}
5302
5303TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005304 addConfigurationProperty("touch.deviceType", "touchScreen");
5305 prepareDisplay(DISPLAY_ORIENTATION_0);
5306 prepareButtons();
5307 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005308 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309
5310 NotifyMotionArgs motionArgs;
5311
5312 // initially hovering because pressure is 0
5313 processDown(mapper, 100, 200);
5314 processPressure(mapper, 0);
5315 processSync(mapper);
5316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5317 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5319 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5320
5321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5322 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5324 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5325
5326 // move a little
5327 processMove(mapper, 150, 250);
5328 processSync(mapper);
5329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5330 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5332 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5333
5334 // down when pressure is non-zero
5335 processPressure(mapper, RAW_PRESSURE_MAX);
5336 processSync(mapper);
5337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5340 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5341
5342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5343 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5345 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5346
5347 // up when pressure becomes 0, hover restored
5348 processPressure(mapper, 0);
5349 processSync(mapper);
5350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5351 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5353 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5354
5355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5356 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5358 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5359
5360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5363 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5364
5365 // exit hover when pointer goes away
5366 processUp(mapper);
5367 processSync(mapper);
5368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5369 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5371 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5372}
5373
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374// --- MultiTouchInputMapperTest ---
5375
5376class MultiTouchInputMapperTest : public TouchInputMapperTest {
5377protected:
5378 void prepareAxes(int axes);
5379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005380 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5381 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5382 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5383 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5384 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5385 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5386 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5387 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5388 void processId(MultiTouchInputMapper& mapper, int32_t id);
5389 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5390 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5391 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5392 void processMTSync(MultiTouchInputMapper& mapper);
5393 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394};
5395
5396void MultiTouchInputMapperTest::prepareAxes(int axes) {
5397 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005398 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5399 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 }
5401 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005402 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5403 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005405 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5406 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407 }
5408 }
5409 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005410 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5411 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005413 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5414 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415 }
5416 }
5417 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005418 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5419 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420 }
5421 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005422 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5423 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424 }
5425 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005426 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5427 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428 }
5429 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005430 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5431 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432 }
5433 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005434 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5435 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436 }
5437 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005438 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439 }
5440}
5441
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005442void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5443 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005444 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5445 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446}
5447
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005448void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5449 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005450 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451}
5452
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005453void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5454 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005455 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456}
5457
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005458void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005459 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005460}
5461
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005462void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005463 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464}
5465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005466void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5467 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005468 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005469}
5470
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005471void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005472 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473}
5474
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005475void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005476 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477}
5478
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005479void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005480 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005481}
5482
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005483void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005484 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485}
5486
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005487void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005488 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489}
5490
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005491void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5492 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005493 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005494}
5495
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005496void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005497 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498}
5499
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005500void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005501 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005502}
5503
Michael Wrightd02c5b62014-02-10 15:10:22 -08005504TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505 addConfigurationProperty("touch.deviceType", "touchScreen");
5506 prepareDisplay(DISPLAY_ORIENTATION_0);
5507 prepareAxes(POSITION);
5508 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005509 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510
arthurhungdcef2dc2020-08-11 14:47:50 +08005511 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512
5513 NotifyMotionArgs motionArgs;
5514
5515 // Two fingers down at once.
5516 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5517 processPosition(mapper, x1, y1);
5518 processMTSync(mapper);
5519 processPosition(mapper, x2, y2);
5520 processMTSync(mapper);
5521 processSync(mapper);
5522
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5524 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5525 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5526 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5527 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5528 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5529 ASSERT_EQ(0, motionArgs.flags);
5530 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5531 ASSERT_EQ(0, motionArgs.buttonState);
5532 ASSERT_EQ(0, motionArgs.edgeFlags);
5533 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5534 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5537 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5538 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5539 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5540 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5541
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5544 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5545 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5546 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5547 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5548 motionArgs.action);
5549 ASSERT_EQ(0, motionArgs.flags);
5550 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5551 ASSERT_EQ(0, motionArgs.buttonState);
5552 ASSERT_EQ(0, motionArgs.edgeFlags);
5553 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5554 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5556 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5559 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5561 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5562 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5563 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5564 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5565
5566 // Move.
5567 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5568 processPosition(mapper, x1, y1);
5569 processMTSync(mapper);
5570 processPosition(mapper, x2, y2);
5571 processMTSync(mapper);
5572 processSync(mapper);
5573
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5575 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5576 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5577 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5578 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5580 ASSERT_EQ(0, motionArgs.flags);
5581 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5582 ASSERT_EQ(0, motionArgs.buttonState);
5583 ASSERT_EQ(0, motionArgs.edgeFlags);
5584 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5585 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5587 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5590 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5592 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5593 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5594 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5595 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5596
5597 // First finger up.
5598 x2 += 15; y2 -= 20;
5599 processPosition(mapper, x2, y2);
5600 processMTSync(mapper);
5601 processSync(mapper);
5602
5603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5604 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5605 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5606 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5607 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5608 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5609 motionArgs.action);
5610 ASSERT_EQ(0, motionArgs.flags);
5611 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5612 ASSERT_EQ(0, motionArgs.buttonState);
5613 ASSERT_EQ(0, motionArgs.edgeFlags);
5614 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5615 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5617 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5620 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5622 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5623 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5624 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5625 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5626
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5628 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5629 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5630 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5631 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5633 ASSERT_EQ(0, motionArgs.flags);
5634 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5635 ASSERT_EQ(0, motionArgs.buttonState);
5636 ASSERT_EQ(0, motionArgs.edgeFlags);
5637 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5638 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5641 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5642 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5643 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5644 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5645
5646 // Move.
5647 x2 += 20; y2 -= 25;
5648 processPosition(mapper, x2, y2);
5649 processMTSync(mapper);
5650 processSync(mapper);
5651
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5653 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5654 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5655 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5656 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5658 ASSERT_EQ(0, motionArgs.flags);
5659 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5660 ASSERT_EQ(0, motionArgs.buttonState);
5661 ASSERT_EQ(0, motionArgs.edgeFlags);
5662 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5663 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5666 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5667 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5668 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5670
5671 // New finger down.
5672 int32_t x3 = 700, y3 = 300;
5673 processPosition(mapper, x2, y2);
5674 processMTSync(mapper);
5675 processPosition(mapper, x3, y3);
5676 processMTSync(mapper);
5677 processSync(mapper);
5678
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5680 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5681 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5682 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5683 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5684 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5685 motionArgs.action);
5686 ASSERT_EQ(0, motionArgs.flags);
5687 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5688 ASSERT_EQ(0, motionArgs.buttonState);
5689 ASSERT_EQ(0, motionArgs.edgeFlags);
5690 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5693 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5696 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5698 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5699 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5700 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5701 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5702
5703 // Second finger up.
5704 x3 += 30; y3 -= 20;
5705 processPosition(mapper, x3, y3);
5706 processMTSync(mapper);
5707 processSync(mapper);
5708
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5710 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5711 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5712 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5713 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5714 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5715 motionArgs.action);
5716 ASSERT_EQ(0, motionArgs.flags);
5717 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5718 ASSERT_EQ(0, motionArgs.buttonState);
5719 ASSERT_EQ(0, motionArgs.edgeFlags);
5720 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5723 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5726 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5728 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5729 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5730 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5731 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5734 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5735 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5736 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5737 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5738 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5739 ASSERT_EQ(0, motionArgs.flags);
5740 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5741 ASSERT_EQ(0, motionArgs.buttonState);
5742 ASSERT_EQ(0, motionArgs.edgeFlags);
5743 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5744 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5747 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5748 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5749 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5750 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5751
5752 // Last finger up.
5753 processMTSync(mapper);
5754 processSync(mapper);
5755
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5757 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5758 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5759 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5760 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5761 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5762 ASSERT_EQ(0, motionArgs.flags);
5763 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5764 ASSERT_EQ(0, motionArgs.buttonState);
5765 ASSERT_EQ(0, motionArgs.edgeFlags);
5766 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5767 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5770 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5771 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5772 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5773 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5774
5775 // Should not have sent any more keys or motions.
5776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5778}
5779
5780TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781 addConfigurationProperty("touch.deviceType", "touchScreen");
5782 prepareDisplay(DISPLAY_ORIENTATION_0);
5783 prepareAxes(POSITION | ID);
5784 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005785 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786
arthurhungdcef2dc2020-08-11 14:47:50 +08005787 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005788
5789 NotifyMotionArgs motionArgs;
5790
5791 // Two fingers down at once.
5792 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5793 processPosition(mapper, x1, y1);
5794 processId(mapper, 1);
5795 processMTSync(mapper);
5796 processPosition(mapper, x2, y2);
5797 processId(mapper, 2);
5798 processMTSync(mapper);
5799 processSync(mapper);
5800
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5802 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5803 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5804 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5807 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5808
5809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5810 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5811 motionArgs.action);
5812 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5813 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5815 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5818 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5820 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5821
5822 // Move.
5823 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5824 processPosition(mapper, x1, y1);
5825 processId(mapper, 1);
5826 processMTSync(mapper);
5827 processPosition(mapper, x2, y2);
5828 processId(mapper, 2);
5829 processMTSync(mapper);
5830 processSync(mapper);
5831
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5834 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5835 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5836 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5837 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5840 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5842 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5843
5844 // First finger up.
5845 x2 += 15; y2 -= 20;
5846 processPosition(mapper, x2, y2);
5847 processId(mapper, 2);
5848 processMTSync(mapper);
5849 processSync(mapper);
5850
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5852 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5853 motionArgs.action);
5854 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5855 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5857 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5860 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5862 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5863
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5866 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5867 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5870 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5871
5872 // Move.
5873 x2 += 20; y2 -= 25;
5874 processPosition(mapper, x2, y2);
5875 processId(mapper, 2);
5876 processMTSync(mapper);
5877 processSync(mapper);
5878
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5881 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5882 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5885 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5886
5887 // New finger down.
5888 int32_t x3 = 700, y3 = 300;
5889 processPosition(mapper, x2, y2);
5890 processId(mapper, 2);
5891 processMTSync(mapper);
5892 processPosition(mapper, x3, y3);
5893 processId(mapper, 3);
5894 processMTSync(mapper);
5895 processSync(mapper);
5896
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5898 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5899 motionArgs.action);
5900 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5901 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5903 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5906 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5908 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5909
5910 // Second finger up.
5911 x3 += 30; y3 -= 20;
5912 processPosition(mapper, x3, y3);
5913 processId(mapper, 3);
5914 processMTSync(mapper);
5915 processSync(mapper);
5916
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5918 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5919 motionArgs.action);
5920 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5923 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5926 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5928 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5929
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5932 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5933 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5934 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5936 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5937
5938 // Last finger up.
5939 processMTSync(mapper);
5940 processSync(mapper);
5941
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5944 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5945 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5948 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5949
5950 // Should not have sent any more keys or motions.
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5953}
5954
5955TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956 addConfigurationProperty("touch.deviceType", "touchScreen");
5957 prepareDisplay(DISPLAY_ORIENTATION_0);
5958 prepareAxes(POSITION | ID | SLOT);
5959 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961
arthurhungdcef2dc2020-08-11 14:47:50 +08005962 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005963
5964 NotifyMotionArgs motionArgs;
5965
5966 // Two fingers down at once.
5967 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5968 processPosition(mapper, x1, y1);
5969 processId(mapper, 1);
5970 processSlot(mapper, 1);
5971 processPosition(mapper, x2, y2);
5972 processId(mapper, 2);
5973 processSync(mapper);
5974
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5976 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5977 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5978 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5979 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5981 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5985 motionArgs.action);
5986 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5987 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5989 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5992 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5994 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5995
5996 // Move.
5997 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5998 processSlot(mapper, 0);
5999 processPosition(mapper, x1, y1);
6000 processSlot(mapper, 1);
6001 processPosition(mapper, x2, y2);
6002 processSync(mapper);
6003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6006 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6007 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6009 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6012 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6014 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6015
6016 // First finger up.
6017 x2 += 15; y2 -= 20;
6018 processSlot(mapper, 0);
6019 processId(mapper, -1);
6020 processSlot(mapper, 1);
6021 processPosition(mapper, x2, y2);
6022 processSync(mapper);
6023
6024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6025 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6026 motionArgs.action);
6027 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6028 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6030 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6033 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6035 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6036
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6039 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6040 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6043 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6044
6045 // Move.
6046 x2 += 20; y2 -= 25;
6047 processPosition(mapper, x2, y2);
6048 processSync(mapper);
6049
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6052 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6053 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6056 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6057
6058 // New finger down.
6059 int32_t x3 = 700, y3 = 300;
6060 processPosition(mapper, x2, y2);
6061 processSlot(mapper, 0);
6062 processId(mapper, 3);
6063 processPosition(mapper, x3, y3);
6064 processSync(mapper);
6065
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6067 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6068 motionArgs.action);
6069 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6070 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6072 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6073 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6075 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6077 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6078
6079 // Second finger up.
6080 x3 += 30; y3 -= 20;
6081 processSlot(mapper, 1);
6082 processId(mapper, -1);
6083 processSlot(mapper, 0);
6084 processPosition(mapper, x3, y3);
6085 processSync(mapper);
6086
6087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6088 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6089 motionArgs.action);
6090 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6093 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6096 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6098 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6099
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6102 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6103 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6104 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6106 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6107
6108 // Last finger up.
6109 processId(mapper, -1);
6110 processSync(mapper);
6111
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6113 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6114 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6115 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6118 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6119
6120 // Should not have sent any more keys or motions.
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6123}
6124
6125TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 addConfigurationProperty("touch.deviceType", "touchScreen");
6127 prepareDisplay(DISPLAY_ORIENTATION_0);
6128 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006129 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130
6131 // These calculations are based on the input device calibration documentation.
6132 int32_t rawX = 100;
6133 int32_t rawY = 200;
6134 int32_t rawTouchMajor = 7;
6135 int32_t rawTouchMinor = 6;
6136 int32_t rawToolMajor = 9;
6137 int32_t rawToolMinor = 8;
6138 int32_t rawPressure = 11;
6139 int32_t rawDistance = 0;
6140 int32_t rawOrientation = 3;
6141 int32_t id = 5;
6142
6143 float x = toDisplayX(rawX);
6144 float y = toDisplayY(rawY);
6145 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6146 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6147 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6148 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6149 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6150 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6151 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6152 float distance = float(rawDistance);
6153
6154 processPosition(mapper, rawX, rawY);
6155 processTouchMajor(mapper, rawTouchMajor);
6156 processTouchMinor(mapper, rawTouchMinor);
6157 processToolMajor(mapper, rawToolMajor);
6158 processToolMinor(mapper, rawToolMinor);
6159 processPressure(mapper, rawPressure);
6160 processOrientation(mapper, rawOrientation);
6161 processDistance(mapper, rawDistance);
6162 processId(mapper, id);
6163 processMTSync(mapper);
6164 processSync(mapper);
6165
6166 NotifyMotionArgs args;
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6168 ASSERT_EQ(0, args.pointerProperties[0].id);
6169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6170 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6171 orientation, distance));
6172}
6173
6174TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 addConfigurationProperty("touch.deviceType", "touchScreen");
6176 prepareDisplay(DISPLAY_ORIENTATION_0);
6177 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6178 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006179 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180
6181 // These calculations are based on the input device calibration documentation.
6182 int32_t rawX = 100;
6183 int32_t rawY = 200;
6184 int32_t rawTouchMajor = 140;
6185 int32_t rawTouchMinor = 120;
6186 int32_t rawToolMajor = 180;
6187 int32_t rawToolMinor = 160;
6188
6189 float x = toDisplayX(rawX);
6190 float y = toDisplayY(rawY);
6191 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6192 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6193 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6194 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6195 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6196
6197 processPosition(mapper, rawX, rawY);
6198 processTouchMajor(mapper, rawTouchMajor);
6199 processTouchMinor(mapper, rawTouchMinor);
6200 processToolMajor(mapper, rawToolMajor);
6201 processToolMinor(mapper, rawToolMinor);
6202 processMTSync(mapper);
6203 processSync(mapper);
6204
6205 NotifyMotionArgs args;
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6208 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6209}
6210
6211TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212 addConfigurationProperty("touch.deviceType", "touchScreen");
6213 prepareDisplay(DISPLAY_ORIENTATION_0);
6214 prepareAxes(POSITION | TOUCH | TOOL);
6215 addConfigurationProperty("touch.size.calibration", "diameter");
6216 addConfigurationProperty("touch.size.scale", "10");
6217 addConfigurationProperty("touch.size.bias", "160");
6218 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006219 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220
6221 // These calculations are based on the input device calibration documentation.
6222 // Note: We only provide a single common touch/tool value because the device is assumed
6223 // not to emit separate values for each pointer (isSummed = 1).
6224 int32_t rawX = 100;
6225 int32_t rawY = 200;
6226 int32_t rawX2 = 150;
6227 int32_t rawY2 = 250;
6228 int32_t rawTouchMajor = 5;
6229 int32_t rawToolMajor = 8;
6230
6231 float x = toDisplayX(rawX);
6232 float y = toDisplayY(rawY);
6233 float x2 = toDisplayX(rawX2);
6234 float y2 = toDisplayY(rawY2);
6235 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6236 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6237 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6238
6239 processPosition(mapper, rawX, rawY);
6240 processTouchMajor(mapper, rawTouchMajor);
6241 processToolMajor(mapper, rawToolMajor);
6242 processMTSync(mapper);
6243 processPosition(mapper, rawX2, rawY2);
6244 processTouchMajor(mapper, rawTouchMajor);
6245 processToolMajor(mapper, rawToolMajor);
6246 processMTSync(mapper);
6247 processSync(mapper);
6248
6249 NotifyMotionArgs args;
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6251 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6252
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6254 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6255 args.action);
6256 ASSERT_EQ(size_t(2), args.pointerCount);
6257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6258 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6260 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6261}
6262
6263TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264 addConfigurationProperty("touch.deviceType", "touchScreen");
6265 prepareDisplay(DISPLAY_ORIENTATION_0);
6266 prepareAxes(POSITION | TOUCH | TOOL);
6267 addConfigurationProperty("touch.size.calibration", "area");
6268 addConfigurationProperty("touch.size.scale", "43");
6269 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006270 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271
6272 // These calculations are based on the input device calibration documentation.
6273 int32_t rawX = 100;
6274 int32_t rawY = 200;
6275 int32_t rawTouchMajor = 5;
6276 int32_t rawToolMajor = 8;
6277
6278 float x = toDisplayX(rawX);
6279 float y = toDisplayY(rawY);
6280 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6281 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6282 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6283
6284 processPosition(mapper, rawX, rawY);
6285 processTouchMajor(mapper, rawTouchMajor);
6286 processToolMajor(mapper, rawToolMajor);
6287 processMTSync(mapper);
6288 processSync(mapper);
6289
6290 NotifyMotionArgs args;
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6293 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6294}
6295
6296TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006297 addConfigurationProperty("touch.deviceType", "touchScreen");
6298 prepareDisplay(DISPLAY_ORIENTATION_0);
6299 prepareAxes(POSITION | PRESSURE);
6300 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6301 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006302 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303
Michael Wrightaa449c92017-12-13 21:21:43 +00006304 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006305 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006306 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6307 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6308 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6309
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310 // These calculations are based on the input device calibration documentation.
6311 int32_t rawX = 100;
6312 int32_t rawY = 200;
6313 int32_t rawPressure = 60;
6314
6315 float x = toDisplayX(rawX);
6316 float y = toDisplayY(rawY);
6317 float pressure = float(rawPressure) * 0.01f;
6318
6319 processPosition(mapper, rawX, rawY);
6320 processPressure(mapper, rawPressure);
6321 processMTSync(mapper);
6322 processSync(mapper);
6323
6324 NotifyMotionArgs args;
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6327 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6328}
6329
6330TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 addConfigurationProperty("touch.deviceType", "touchScreen");
6332 prepareDisplay(DISPLAY_ORIENTATION_0);
6333 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006334 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335
6336 NotifyMotionArgs motionArgs;
6337 NotifyKeyArgs keyArgs;
6338
6339 processId(mapper, 1);
6340 processPosition(mapper, 100, 200);
6341 processSync(mapper);
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6343 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6344 ASSERT_EQ(0, motionArgs.buttonState);
6345
6346 // press BTN_LEFT, release BTN_LEFT
6347 processKey(mapper, BTN_LEFT, 1);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6351 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6352
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6355 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6356
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357 processKey(mapper, BTN_LEFT, 0);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006365 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366
6367 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6368 processKey(mapper, BTN_RIGHT, 1);
6369 processKey(mapper, BTN_MIDDLE, 1);
6370 processSync(mapper);
6371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6373 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6374 motionArgs.buttonState);
6375
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6377 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6378 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6379
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6381 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6382 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6383 motionArgs.buttonState);
6384
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 processKey(mapper, BTN_RIGHT, 0);
6386 processSync(mapper);
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006388 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006390
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006393 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394
6395 processKey(mapper, BTN_MIDDLE, 0);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006403 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404
6405 // press BTN_BACK, release BTN_BACK
6406 processKey(mapper, BTN_BACK, 1);
6407 processSync(mapper);
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6409 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6410 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006411
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006414 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6415
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6418 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419
6420 processKey(mapper, BTN_BACK, 0);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006425
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006428 ASSERT_EQ(0, motionArgs.buttonState);
6429
Michael Wrightd02c5b62014-02-10 15:10:22 -08006430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6431 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6432 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6433
6434 // press BTN_SIDE, release BTN_SIDE
6435 processKey(mapper, BTN_SIDE, 1);
6436 processSync(mapper);
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6438 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6439 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006440
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006443 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6444
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6447 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006448
6449 processKey(mapper, BTN_SIDE, 0);
6450 processSync(mapper);
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006452 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006454
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006457 ASSERT_EQ(0, motionArgs.buttonState);
6458
Michael Wrightd02c5b62014-02-10 15:10:22 -08006459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6460 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6461 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6462
6463 // press BTN_FORWARD, release BTN_FORWARD
6464 processKey(mapper, BTN_FORWARD, 1);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6467 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6468 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006469
Michael Wrightd02c5b62014-02-10 15:10:22 -08006470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006472 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6473
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6475 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6476 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006477
6478 processKey(mapper, BTN_FORWARD, 0);
6479 processSync(mapper);
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006481 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006483
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006486 ASSERT_EQ(0, motionArgs.buttonState);
6487
Michael Wrightd02c5b62014-02-10 15:10:22 -08006488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6489 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6490 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6491
6492 // press BTN_EXTRA, release BTN_EXTRA
6493 processKey(mapper, BTN_EXTRA, 1);
6494 processSync(mapper);
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6496 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6497 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006498
Michael Wrightd02c5b62014-02-10 15:10:22 -08006499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006501 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6502
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6505 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006506
6507 processKey(mapper, BTN_EXTRA, 0);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006512
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006515 ASSERT_EQ(0, motionArgs.buttonState);
6516
Michael Wrightd02c5b62014-02-10 15:10:22 -08006517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6518 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6519 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6520
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6522
Michael Wrightd02c5b62014-02-10 15:10:22 -08006523 // press BTN_STYLUS, release BTN_STYLUS
6524 processKey(mapper, BTN_STYLUS, 1);
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006528 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6529
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6532 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533
6534 processKey(mapper, BTN_STYLUS, 0);
6535 processSync(mapper);
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006539
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006542 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006543
6544 // press BTN_STYLUS2, release BTN_STYLUS2
6545 processKey(mapper, BTN_STYLUS2, 1);
6546 processSync(mapper);
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006549 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6550
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6553 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006554
6555 processKey(mapper, BTN_STYLUS2, 0);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006558 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006559 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006560
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006563 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006564
6565 // release touch
6566 processId(mapper, -1);
6567 processSync(mapper);
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6569 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6570 ASSERT_EQ(0, motionArgs.buttonState);
6571}
6572
6573TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006574 addConfigurationProperty("touch.deviceType", "touchScreen");
6575 prepareDisplay(DISPLAY_ORIENTATION_0);
6576 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006577 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006578
6579 NotifyMotionArgs motionArgs;
6580
6581 // default tool type is finger
6582 processId(mapper, 1);
6583 processPosition(mapper, 100, 200);
6584 processSync(mapper);
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6588
6589 // eraser
6590 processKey(mapper, BTN_TOOL_RUBBER, 1);
6591 processSync(mapper);
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6595
6596 // stylus
6597 processKey(mapper, BTN_TOOL_RUBBER, 0);
6598 processKey(mapper, BTN_TOOL_PEN, 1);
6599 processSync(mapper);
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6603
6604 // brush
6605 processKey(mapper, BTN_TOOL_PEN, 0);
6606 processKey(mapper, BTN_TOOL_BRUSH, 1);
6607 processSync(mapper);
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6611
6612 // pencil
6613 processKey(mapper, BTN_TOOL_BRUSH, 0);
6614 processKey(mapper, BTN_TOOL_PENCIL, 1);
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6619
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006620 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006621 processKey(mapper, BTN_TOOL_PENCIL, 0);
6622 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6623 processSync(mapper);
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6627
6628 // mouse
6629 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6630 processKey(mapper, BTN_TOOL_MOUSE, 1);
6631 processSync(mapper);
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6635
6636 // lens
6637 processKey(mapper, BTN_TOOL_MOUSE, 0);
6638 processKey(mapper, BTN_TOOL_LENS, 1);
6639 processSync(mapper);
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6643
6644 // double-tap
6645 processKey(mapper, BTN_TOOL_LENS, 0);
6646 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6651
6652 // triple-tap
6653 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6654 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6655 processSync(mapper);
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6659
6660 // quad-tap
6661 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6662 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6663 processSync(mapper);
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6666 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6667
6668 // finger
6669 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6670 processKey(mapper, BTN_TOOL_FINGER, 1);
6671 processSync(mapper);
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6675
6676 // stylus trumps finger
6677 processKey(mapper, BTN_TOOL_PEN, 1);
6678 processSync(mapper);
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6681 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6682
6683 // eraser trumps stylus
6684 processKey(mapper, BTN_TOOL_RUBBER, 1);
6685 processSync(mapper);
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6689
6690 // mouse trumps eraser
6691 processKey(mapper, BTN_TOOL_MOUSE, 1);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6696
6697 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6698 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6699 processSync(mapper);
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6703
6704 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6705 processToolType(mapper, MT_TOOL_PEN);
6706 processSync(mapper);
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6710
6711 // back to default tool type
6712 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6713 processKey(mapper, BTN_TOOL_MOUSE, 0);
6714 processKey(mapper, BTN_TOOL_RUBBER, 0);
6715 processKey(mapper, BTN_TOOL_PEN, 0);
6716 processKey(mapper, BTN_TOOL_FINGER, 0);
6717 processSync(mapper);
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6721}
6722
6723TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006724 addConfigurationProperty("touch.deviceType", "touchScreen");
6725 prepareDisplay(DISPLAY_ORIENTATION_0);
6726 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006727 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006728 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006729
6730 NotifyMotionArgs motionArgs;
6731
6732 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6733 processId(mapper, 1);
6734 processPosition(mapper, 100, 200);
6735 processSync(mapper);
6736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6737 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6739 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6740
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6742 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6744 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6745
6746 // move a little
6747 processPosition(mapper, 150, 250);
6748 processSync(mapper);
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6750 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6752 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6753
6754 // down when BTN_TOUCH is pressed, pressure defaults to 1
6755 processKey(mapper, BTN_TOUCH, 1);
6756 processSync(mapper);
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6758 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6760 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6761
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6763 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6765 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6766
6767 // up when BTN_TOUCH is released, hover restored
6768 processKey(mapper, BTN_TOUCH, 0);
6769 processSync(mapper);
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6771 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6773 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6774
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6778 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6779
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6783 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6784
6785 // exit hover when pointer goes away
6786 processId(mapper, -1);
6787 processSync(mapper);
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6789 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6791 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6792}
6793
6794TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006795 addConfigurationProperty("touch.deviceType", "touchScreen");
6796 prepareDisplay(DISPLAY_ORIENTATION_0);
6797 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006798 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006799
6800 NotifyMotionArgs motionArgs;
6801
6802 // initially hovering because pressure is 0
6803 processId(mapper, 1);
6804 processPosition(mapper, 100, 200);
6805 processPressure(mapper, 0);
6806 processSync(mapper);
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6808 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6810 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6811
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6813 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6815 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6816
6817 // move a little
6818 processPosition(mapper, 150, 250);
6819 processSync(mapper);
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6821 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6823 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6824
6825 // down when pressure becomes non-zero
6826 processPressure(mapper, RAW_PRESSURE_MAX);
6827 processSync(mapper);
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6829 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6831 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6832
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6834 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6836 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6837
6838 // up when pressure becomes 0, hover restored
6839 processPressure(mapper, 0);
6840 processSync(mapper);
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6842 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6844 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6845
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6847 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6849 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6850
6851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6852 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6854 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6855
6856 // exit hover when pointer goes away
6857 processId(mapper, -1);
6858 processSync(mapper);
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6860 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6862 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6863}
6864
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006865/**
6866 * Set the input device port <--> display port associations, and check that the
6867 * events are routed to the display that matches the display port.
6868 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6869 */
6870TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006871 const std::string usb2 = "USB2";
6872 const uint8_t hdmi1 = 0;
6873 const uint8_t hdmi2 = 1;
6874 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006875 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006876
6877 addConfigurationProperty("touch.deviceType", "touchScreen");
6878 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006879 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006880
6881 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6882 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6883
6884 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6885 // for this input device is specified, and the matching viewport is not present,
6886 // the input device should be disabled (at the mapper level).
6887
6888 // Add viewport for display 2 on hdmi2
6889 prepareSecondaryDisplay(type, hdmi2);
6890 // Send a touch event
6891 processPosition(mapper, 100, 100);
6892 processSync(mapper);
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6894
6895 // Add viewport for display 1 on hdmi1
6896 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6897 // Send a touch event again
6898 processPosition(mapper, 100, 100);
6899 processSync(mapper);
6900
6901 NotifyMotionArgs args;
6902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6903 ASSERT_EQ(DISPLAY_ID, args.displayId);
6904}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006905
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006906TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006907 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006908 std::shared_ptr<FakePointerController> fakePointerController =
6909 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006910 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006911 fakePointerController->setPosition(100, 200);
6912 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006913 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6914
Garfield Tan888a6a42020-01-09 11:39:16 -08006915 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006916 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006917
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006918 prepareDisplay(DISPLAY_ORIENTATION_0);
6919 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006920 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006921
6922 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006923 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006924
6925 NotifyMotionArgs motionArgs;
6926 processPosition(mapper, 100, 100);
6927 processSync(mapper);
6928
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6930 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6931 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6932}
6933
Arthur Hung7c645402019-01-25 17:45:42 +08006934TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6935 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006936 prepareAxes(POSITION | ID | SLOT);
6937 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006938 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006939
6940 // Create the second touch screen device, and enable multi fingers.
6941 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08006942 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006943 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006944 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08006945 std::shared_ptr<InputDevice> device2 =
6946 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
6947 Flags<InputDeviceClass>(0));
6948
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006949 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6950 0 /*flat*/, 0 /*fuzz*/);
6951 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6952 0 /*flat*/, 0 /*fuzz*/);
6953 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6954 0 /*flat*/, 0 /*fuzz*/);
6955 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6956 0 /*flat*/, 0 /*fuzz*/);
6957 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6958 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6959 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006960
6961 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006962 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006963 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6964 device2->reset(ARBITRARY_TIME);
6965
6966 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006967 std::shared_ptr<FakePointerController> fakePointerController =
6968 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006969 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6970 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6971
6972 // Setup policy for associated displays and show touches.
6973 const uint8_t hdmi1 = 0;
6974 const uint8_t hdmi2 = 1;
6975 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6976 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6977 mFakePolicy->setShowTouches(true);
6978
6979 // Create displays.
6980 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006981 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006982
6983 // Default device will reconfigure above, need additional reconfiguration for another device.
6984 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006985 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08006986
6987 // Two fingers down at default display.
6988 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6989 processPosition(mapper, x1, y1);
6990 processId(mapper, 1);
6991 processSlot(mapper, 1);
6992 processPosition(mapper, x2, y2);
6993 processId(mapper, 2);
6994 processSync(mapper);
6995
6996 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6997 fakePointerController->getSpots().find(DISPLAY_ID);
6998 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6999 ASSERT_EQ(size_t(2), iter->second.size());
7000
7001 // Two fingers down at second display.
7002 processPosition(mapper2, x1, y1);
7003 processId(mapper2, 1);
7004 processSlot(mapper2, 1);
7005 processPosition(mapper2, x2, y2);
7006 processId(mapper2, 2);
7007 processSync(mapper2);
7008
7009 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7010 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7011 ASSERT_EQ(size_t(2), iter->second.size());
7012}
7013
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007014TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007015 prepareAxes(POSITION);
7016 addConfigurationProperty("touch.deviceType", "touchScreen");
7017 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007018 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007019
7020 NotifyMotionArgs motionArgs;
7021 // Unrotated video frame
7022 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7023 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007024 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007025 processPosition(mapper, 100, 200);
7026 processSync(mapper);
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7028 ASSERT_EQ(frames, motionArgs.videoFrames);
7029
7030 // Subsequent touch events should not have any videoframes
7031 // This is implemented separately in FakeEventHub,
7032 // but that should match the behaviour of TouchVideoDevice.
7033 processPosition(mapper, 200, 200);
7034 processSync(mapper);
7035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7036 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7037}
7038
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007039TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007040 prepareAxes(POSITION);
7041 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007042 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007043 // Unrotated video frame
7044 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7045 NotifyMotionArgs motionArgs;
7046
7047 // Test all 4 orientations
7048 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7049 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7050 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7051 clearViewports();
7052 prepareDisplay(orientation);
7053 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007054 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007055 processPosition(mapper, 100, 200);
7056 processSync(mapper);
7057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7058 frames[0].rotate(orientation);
7059 ASSERT_EQ(frames, motionArgs.videoFrames);
7060 }
7061}
7062
7063TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007064 prepareAxes(POSITION);
7065 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007066 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007067 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7068 // so mix these.
7069 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7070 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7071 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7072 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7073 NotifyMotionArgs motionArgs;
7074
7075 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007076 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007077 processPosition(mapper, 100, 200);
7078 processSync(mapper);
7079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7080 std::for_each(frames.begin(), frames.end(),
7081 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7082 ASSERT_EQ(frames, motionArgs.videoFrames);
7083}
7084
Arthur Hung9da14732019-09-02 16:16:58 +08007085/**
7086 * If we had defined port associations, but the viewport is not ready, the touch device would be
7087 * expected to be disabled, and it should be enabled after the viewport has found.
7088 */
7089TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007090 constexpr uint8_t hdmi2 = 1;
7091 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007092 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007093
7094 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7095
7096 addConfigurationProperty("touch.deviceType", "touchScreen");
7097 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007098 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007099
7100 ASSERT_EQ(mDevice->isEnabled(), false);
7101
7102 // Add display on hdmi2, the device should be enabled and can receive touch event.
7103 prepareSecondaryDisplay(type, hdmi2);
7104 ASSERT_EQ(mDevice->isEnabled(), true);
7105
7106 // Send a touch event.
7107 processPosition(mapper, 100, 100);
7108 processSync(mapper);
7109
7110 NotifyMotionArgs args;
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7112 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7113}
7114
Arthur Hung6cd19a42019-08-30 19:04:12 +08007115
Arthur Hung6cd19a42019-08-30 19:04:12 +08007116
Arthur Hung421eb1c2020-01-16 00:09:42 +08007117TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007118 addConfigurationProperty("touch.deviceType", "touchScreen");
7119 prepareDisplay(DISPLAY_ORIENTATION_0);
7120 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007121 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007122
7123 NotifyMotionArgs motionArgs;
7124
7125 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7126 // finger down
7127 processId(mapper, 1);
7128 processPosition(mapper, x1, y1);
7129 processSync(mapper);
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7131 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7133
7134 // finger move
7135 processId(mapper, 1);
7136 processPosition(mapper, x2, y2);
7137 processSync(mapper);
7138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7141
7142 // finger up.
7143 processId(mapper, -1);
7144 processSync(mapper);
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7146 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7148
7149 // new finger down
7150 processId(mapper, 1);
7151 processPosition(mapper, x3, y3);
7152 processSync(mapper);
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7154 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7156}
7157
7158/**
arthurhungcc7f9802020-04-30 17:55:40 +08007159 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7160 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007161 */
arthurhungcc7f9802020-04-30 17:55:40 +08007162TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007163 addConfigurationProperty("touch.deviceType", "touchScreen");
7164 prepareDisplay(DISPLAY_ORIENTATION_0);
7165 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007166 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007167
7168 NotifyMotionArgs motionArgs;
7169
7170 // default tool type is finger
7171 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007172 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007173 processPosition(mapper, x1, y1);
7174 processSync(mapper);
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7176 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7178
7179 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7180 processToolType(mapper, MT_TOOL_PALM);
7181 processSync(mapper);
7182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7183 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7184
7185 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007186 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007187 processPosition(mapper, x2, y2);
7188 processSync(mapper);
7189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7190
7191 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007192 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007193 processSync(mapper);
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7195
7196 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007197 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007198 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007199 processPosition(mapper, x3, y3);
7200 processSync(mapper);
7201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7202 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7204}
7205
arthurhungbf89a482020-04-17 17:37:55 +08007206/**
arthurhungcc7f9802020-04-30 17:55:40 +08007207 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7208 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007209 */
arthurhungcc7f9802020-04-30 17:55:40 +08007210TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007211 addConfigurationProperty("touch.deviceType", "touchScreen");
7212 prepareDisplay(DISPLAY_ORIENTATION_0);
7213 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7214 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7215
7216 NotifyMotionArgs motionArgs;
7217
7218 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007219 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7220 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007221 processPosition(mapper, x1, y1);
7222 processSync(mapper);
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7224 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7226
7227 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007228 processSlot(mapper, SECOND_SLOT);
7229 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007230 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007231 processSync(mapper);
7232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7233 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7234 motionArgs.action);
7235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7236
7237 // If the tool type of the first finger changes to MT_TOOL_PALM,
7238 // we expect to receive ACTION_POINTER_UP with cancel flag.
7239 processSlot(mapper, FIRST_SLOT);
7240 processId(mapper, FIRST_TRACKING_ID);
7241 processToolType(mapper, MT_TOOL_PALM);
7242 processSync(mapper);
7243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7244 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7245 motionArgs.action);
7246 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7247
7248 // The following MOVE events of second finger should be processed.
7249 processSlot(mapper, SECOND_SLOT);
7250 processId(mapper, SECOND_TRACKING_ID);
7251 processPosition(mapper, x2 + 1, y2 + 1);
7252 processSync(mapper);
7253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7255 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7256
7257 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7258 // it. Second finger receive move.
7259 processSlot(mapper, FIRST_SLOT);
7260 processId(mapper, INVALID_TRACKING_ID);
7261 processSync(mapper);
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7264 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7265
7266 // Second finger keeps moving.
7267 processSlot(mapper, SECOND_SLOT);
7268 processId(mapper, SECOND_TRACKING_ID);
7269 processPosition(mapper, x2 + 2, y2 + 2);
7270 processSync(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7273 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7274
7275 // Second finger up.
7276 processId(mapper, INVALID_TRACKING_ID);
7277 processSync(mapper);
7278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7279 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7280 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7281}
7282
7283/**
7284 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7285 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7286 */
7287TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7288 addConfigurationProperty("touch.deviceType", "touchScreen");
7289 prepareDisplay(DISPLAY_ORIENTATION_0);
7290 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7291 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7292
7293 NotifyMotionArgs motionArgs;
7294
7295 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7296 // First finger down.
7297 processId(mapper, FIRST_TRACKING_ID);
7298 processPosition(mapper, x1, y1);
7299 processSync(mapper);
7300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7301 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7303
7304 // Second finger down.
7305 processSlot(mapper, SECOND_SLOT);
7306 processId(mapper, SECOND_TRACKING_ID);
7307 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007308 processSync(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7310 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7311 motionArgs.action);
7312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7313
arthurhungcc7f9802020-04-30 17:55:40 +08007314 // If the tool type of the first finger changes to MT_TOOL_PALM,
7315 // we expect to receive ACTION_POINTER_UP with cancel flag.
7316 processSlot(mapper, FIRST_SLOT);
7317 processId(mapper, FIRST_TRACKING_ID);
7318 processToolType(mapper, MT_TOOL_PALM);
7319 processSync(mapper);
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7321 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7322 motionArgs.action);
7323 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7324
7325 // Second finger keeps moving.
7326 processSlot(mapper, SECOND_SLOT);
7327 processId(mapper, SECOND_TRACKING_ID);
7328 processPosition(mapper, x2 + 1, y2 + 1);
7329 processSync(mapper);
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7332
7333 // second finger becomes palm, receive cancel due to only 1 finger is active.
7334 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007335 processToolType(mapper, MT_TOOL_PALM);
7336 processSync(mapper);
7337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7338 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7339
arthurhungcc7f9802020-04-30 17:55:40 +08007340 // third finger down.
7341 processSlot(mapper, THIRD_SLOT);
7342 processId(mapper, THIRD_TRACKING_ID);
7343 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007344 processPosition(mapper, x3, y3);
7345 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7347 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007349 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7350
7351 // third finger move
7352 processId(mapper, THIRD_TRACKING_ID);
7353 processPosition(mapper, x3 + 1, y3 + 1);
7354 processSync(mapper);
7355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7357
7358 // first finger up, third finger receive move.
7359 processSlot(mapper, FIRST_SLOT);
7360 processId(mapper, INVALID_TRACKING_ID);
7361 processSync(mapper);
7362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7364 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7365
7366 // second finger up, third finger receive move.
7367 processSlot(mapper, SECOND_SLOT);
7368 processId(mapper, INVALID_TRACKING_ID);
7369 processSync(mapper);
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7372 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7373
7374 // third finger up.
7375 processSlot(mapper, THIRD_SLOT);
7376 processId(mapper, INVALID_TRACKING_ID);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7379 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7380 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7381}
7382
7383/**
7384 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7385 * and the active finger could still be allowed to receive the events
7386 */
7387TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7388 addConfigurationProperty("touch.deviceType", "touchScreen");
7389 prepareDisplay(DISPLAY_ORIENTATION_0);
7390 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7391 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7392
7393 NotifyMotionArgs motionArgs;
7394
7395 // default tool type is finger
7396 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7397 processId(mapper, FIRST_TRACKING_ID);
7398 processPosition(mapper, x1, y1);
7399 processSync(mapper);
7400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7401 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7403
7404 // Second finger down.
7405 processSlot(mapper, SECOND_SLOT);
7406 processId(mapper, SECOND_TRACKING_ID);
7407 processPosition(mapper, x2, y2);
7408 processSync(mapper);
7409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7410 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7411 motionArgs.action);
7412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7413
7414 // If the tool type of the second finger changes to MT_TOOL_PALM,
7415 // we expect to receive ACTION_POINTER_UP with cancel flag.
7416 processId(mapper, SECOND_TRACKING_ID);
7417 processToolType(mapper, MT_TOOL_PALM);
7418 processSync(mapper);
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7420 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7421 motionArgs.action);
7422 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7423
7424 // The following MOVE event should be processed.
7425 processSlot(mapper, FIRST_SLOT);
7426 processId(mapper, FIRST_TRACKING_ID);
7427 processPosition(mapper, x1 + 1, y1 + 1);
7428 processSync(mapper);
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7431 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7432
7433 // second finger up.
7434 processSlot(mapper, SECOND_SLOT);
7435 processId(mapper, INVALID_TRACKING_ID);
7436 processSync(mapper);
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7439
7440 // first finger keep moving
7441 processSlot(mapper, FIRST_SLOT);
7442 processId(mapper, FIRST_TRACKING_ID);
7443 processPosition(mapper, x1 + 2, y1 + 2);
7444 processSync(mapper);
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7447
7448 // first finger up.
7449 processId(mapper, INVALID_TRACKING_ID);
7450 processSync(mapper);
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7453 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007454}
7455
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007456// --- MultiTouchInputMapperTest_ExternalDevice ---
7457
7458class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7459protected:
7460 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07007461 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007462 }
7463};
7464
7465/**
7466 * Expect fallback to internal viewport if device is external and external viewport is not present.
7467 */
7468TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7469 prepareAxes(POSITION);
7470 addConfigurationProperty("touch.deviceType", "touchScreen");
7471 prepareDisplay(DISPLAY_ORIENTATION_0);
7472 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7473
7474 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7475
7476 NotifyMotionArgs motionArgs;
7477
7478 // Expect the event to be sent to the internal viewport,
7479 // because an external viewport is not present.
7480 processPosition(mapper, 100, 100);
7481 processSync(mapper);
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7483 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7484
7485 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007486 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007487 processPosition(mapper, 100, 100);
7488 processSync(mapper);
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7490 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7491}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007492
7493/**
7494 * Test touch should not work if outside of surface.
7495 */
7496class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7497protected:
7498 void halfDisplayToCenterHorizontal(int32_t orientation) {
7499 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007500 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007501
7502 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7503 internalViewport->orientation = orientation;
7504 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7505 internalViewport->logicalLeft = 0;
7506 internalViewport->logicalTop = 0;
7507 internalViewport->logicalRight = DISPLAY_HEIGHT;
7508 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7509
7510 internalViewport->physicalLeft = 0;
7511 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7512 internalViewport->physicalRight = DISPLAY_HEIGHT;
7513 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7514
7515 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7516 internalViewport->deviceHeight = DISPLAY_WIDTH;
7517 } else {
7518 internalViewport->logicalLeft = 0;
7519 internalViewport->logicalTop = 0;
7520 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7521 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7522
7523 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7524 internalViewport->physicalTop = 0;
7525 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7526 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7527
7528 internalViewport->deviceWidth = DISPLAY_WIDTH;
7529 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7530 }
7531
7532 mFakePolicy->updateViewport(internalViewport.value());
7533 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7534 }
7535
7536 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7537 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7538 int32_t yExpected) {
7539 // touch on outside area should not work.
7540 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7541 processSync(mapper);
7542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7543
7544 // touch on inside area should receive the event.
7545 NotifyMotionArgs args;
7546 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7547 processSync(mapper);
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7549 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7550 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7551
7552 // Reset.
7553 mapper.reset(ARBITRARY_TIME);
7554 }
7555};
7556
7557TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7558 addConfigurationProperty("touch.deviceType", "touchScreen");
7559 prepareDisplay(DISPLAY_ORIENTATION_0);
7560 prepareAxes(POSITION);
7561 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7562
7563 // Touch on center of normal display should work.
7564 const int32_t x = DISPLAY_WIDTH / 4;
7565 const int32_t y = DISPLAY_HEIGHT / 2;
7566 processPosition(mapper, toRawX(x), toRawY(y));
7567 processSync(mapper);
7568 NotifyMotionArgs args;
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7571 0.0f, 0.0f, 0.0f, 0.0f));
7572 // Reset.
7573 mapper.reset(ARBITRARY_TIME);
7574
7575 // Let physical display be different to device, and make surface and physical could be 1:1.
7576 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7577
7578 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7579 const int32_t yExpected = y;
7580 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7581}
7582
7583TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7584 addConfigurationProperty("touch.deviceType", "touchScreen");
7585 prepareDisplay(DISPLAY_ORIENTATION_0);
7586 prepareAxes(POSITION);
7587 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7588
7589 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7590 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7591
7592 const int32_t x = DISPLAY_WIDTH / 4;
7593 const int32_t y = DISPLAY_HEIGHT / 2;
7594
7595 // expect x/y = swap x/y then reverse y.
7596 const int32_t xExpected = y;
7597 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7598 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7599}
7600
7601TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7602 addConfigurationProperty("touch.deviceType", "touchScreen");
7603 prepareDisplay(DISPLAY_ORIENTATION_0);
7604 prepareAxes(POSITION);
7605 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7606
7607 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7608 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7609
7610 const int32_t x = DISPLAY_WIDTH / 4;
7611 const int32_t y = DISPLAY_HEIGHT / 2;
7612
7613 // expect x/y = swap x/y then reverse x.
7614 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7615 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7616 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7617}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007618
7619TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7620 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7621 std::shared_ptr<FakePointerController> fakePointerController =
7622 std::make_shared<FakePointerController>();
7623 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7624 fakePointerController->setPosition(0, 0);
7625 fakePointerController->setButtonState(0);
7626
7627 // prepare device and capture
7628 prepareDisplay(DISPLAY_ORIENTATION_0);
7629 prepareAxes(POSITION | ID | SLOT);
7630 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7631 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7632 mFakePolicy->setPointerCapture(true);
7633 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7634 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7635
7636 // captured touchpad should be a touchpad source
7637 NotifyDeviceResetArgs resetArgs;
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7639 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7640
7641 // run captured pointer tests - note that this is unscaled, so input listener events should be
7642 // identical to what the hardware sends (accounting for any
7643 // calibration).
7644 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007645 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007646 processId(mapper, 1);
7647 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7648 processKey(mapper, BTN_TOUCH, 1);
7649 processSync(mapper);
7650
7651 // expect coord[0] to contain initial location of touch 0
7652 NotifyMotionArgs args;
7653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7654 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7655 ASSERT_EQ(1U, args.pointerCount);
7656 ASSERT_EQ(0, args.pointerProperties[0].id);
7657 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7658 ASSERT_NO_FATAL_FAILURE(
7659 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7660
7661 // FINGER 1 DOWN
7662 processSlot(mapper, 1);
7663 processId(mapper, 2);
7664 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7665 processSync(mapper);
7666
7667 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007669 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7670 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007671 ASSERT_EQ(2U, args.pointerCount);
7672 ASSERT_EQ(0, args.pointerProperties[0].id);
7673 ASSERT_EQ(1, args.pointerProperties[1].id);
7674 ASSERT_NO_FATAL_FAILURE(
7675 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7676 ASSERT_NO_FATAL_FAILURE(
7677 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7678
7679 // FINGER 1 MOVE
7680 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7681 processSync(mapper);
7682
7683 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7684 // from move
7685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7687 ASSERT_NO_FATAL_FAILURE(
7688 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7689 ASSERT_NO_FATAL_FAILURE(
7690 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7691
7692 // FINGER 0 MOVE
7693 processSlot(mapper, 0);
7694 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7695 processSync(mapper);
7696
7697 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7700 ASSERT_NO_FATAL_FAILURE(
7701 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7702 ASSERT_NO_FATAL_FAILURE(
7703 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7704
7705 // BUTTON DOWN
7706 processKey(mapper, BTN_LEFT, 1);
7707 processSync(mapper);
7708
7709 // touchinputmapper design sends a move before button press
7710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7713 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7714
7715 // BUTTON UP
7716 processKey(mapper, BTN_LEFT, 0);
7717 processSync(mapper);
7718
7719 // touchinputmapper design sends a move after button release
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7724
7725 // FINGER 0 UP
7726 processId(mapper, -1);
7727 processSync(mapper);
7728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7729 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7730
7731 // FINGER 1 MOVE
7732 processSlot(mapper, 1);
7733 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7734 processSync(mapper);
7735
7736 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7738 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7739 ASSERT_EQ(1U, args.pointerCount);
7740 ASSERT_EQ(1, args.pointerProperties[0].id);
7741 ASSERT_NO_FATAL_FAILURE(
7742 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7743
7744 // FINGER 1 UP
7745 processId(mapper, -1);
7746 processKey(mapper, BTN_TOUCH, 0);
7747 processSync(mapper);
7748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7749 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7750
7751 // non captured touchpad should be a mouse source
7752 mFakePolicy->setPointerCapture(false);
7753 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7755 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7756}
7757
7758TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7759 std::shared_ptr<FakePointerController> fakePointerController =
7760 std::make_shared<FakePointerController>();
7761 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7762 fakePointerController->setPosition(0, 0);
7763 fakePointerController->setButtonState(0);
7764
7765 // prepare device and capture
7766 prepareDisplay(DISPLAY_ORIENTATION_0);
7767 prepareAxes(POSITION | ID | SLOT);
7768 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7769 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7770 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7771 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7772 // run uncaptured pointer tests - pushes out generic events
7773 // FINGER 0 DOWN
7774 processId(mapper, 3);
7775 processPosition(mapper, 100, 100);
7776 processKey(mapper, BTN_TOUCH, 1);
7777 processSync(mapper);
7778
7779 // start at (100,100), cursor should be at (0,0) * scale
7780 NotifyMotionArgs args;
7781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7782 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7783 ASSERT_NO_FATAL_FAILURE(
7784 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7785
7786 // FINGER 0 MOVE
7787 processPosition(mapper, 200, 200);
7788 processSync(mapper);
7789
7790 // compute scaling to help with touch position checking
7791 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7792 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7793 float scale =
7794 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7795
7796 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7798 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7800 0, 0, 0, 0, 0, 0, 0));
7801}
7802
7803TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7804 std::shared_ptr<FakePointerController> fakePointerController =
7805 std::make_shared<FakePointerController>();
7806
7807 prepareDisplay(DISPLAY_ORIENTATION_0);
7808 prepareAxes(POSITION | ID | SLOT);
7809 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7810 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7811 mFakePolicy->setPointerCapture(false);
7812 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7813
7814 // uncaptured touchpad should be a pointer device
7815 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7816
7817 // captured touchpad should be a touchpad device
7818 mFakePolicy->setPointerCapture(true);
7819 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7820 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7821}
7822
Michael Wrightd02c5b62014-02-10 15:10:22 -08007823} // namespace android