blob: e5f652bc1b0e7e309e5ae7d0aa90d38651d459da [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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002493
2494 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002495 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496 NotifyKeyArgs args;
2497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2498 ASSERT_EQ(DEVICE_ID, args.deviceId);
2499 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2500 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2501 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2502 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2503 ASSERT_EQ(KEY_HOME, args.scanCode);
2504 ASSERT_EQ(AMETA_NONE, args.metaState);
2505 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2506 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2507 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2508
2509 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002510 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2512 ASSERT_EQ(DEVICE_ID, args.deviceId);
2513 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2514 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2515 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2516 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2517 ASSERT_EQ(KEY_HOME, args.scanCode);
2518 ASSERT_EQ(AMETA_NONE, args.metaState);
2519 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2520 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2521 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2522
2523 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002524 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2525 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2527 ASSERT_EQ(DEVICE_ID, args.deviceId);
2528 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2529 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2530 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2531 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2532 ASSERT_EQ(0, args.scanCode);
2533 ASSERT_EQ(AMETA_NONE, args.metaState);
2534 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2535 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2536 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2537
2538 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002539 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2540 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2542 ASSERT_EQ(DEVICE_ID, args.deviceId);
2543 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2544 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2545 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2546 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2547 ASSERT_EQ(0, args.scanCode);
2548 ASSERT_EQ(AMETA_NONE, args.metaState);
2549 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2550 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2551 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2552
2553 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002554 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2555 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2557 ASSERT_EQ(DEVICE_ID, args.deviceId);
2558 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2559 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2560 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2561 ASSERT_EQ(0, args.keyCode);
2562 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2563 ASSERT_EQ(AMETA_NONE, args.metaState);
2564 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2565 ASSERT_EQ(0U, args.policyFlags);
2566 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2567
2568 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002569 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2570 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2572 ASSERT_EQ(DEVICE_ID, args.deviceId);
2573 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2574 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2575 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2576 ASSERT_EQ(0, args.keyCode);
2577 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2578 ASSERT_EQ(AMETA_NONE, args.metaState);
2579 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2580 ASSERT_EQ(0U, args.policyFlags);
2581 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2582}
2583
2584TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002585 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2586 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002588 KeyboardInputMapper& mapper =
2589 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2590 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591
2592 // Initial metastate.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002593 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002594
2595 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002596 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002597 NotifyKeyArgs args;
2598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2599 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002600 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002601 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602
2603 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002604 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002607 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002608
2609 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002610 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2612 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002613 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614
2615 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002616 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2618 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002619 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002620 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621}
2622
2623TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002624 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2625 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2626 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2627 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002629 KeyboardInputMapper& mapper =
2630 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2631 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002633 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2635 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2636 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2637 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2638 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2639 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2640 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2641 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2642}
2643
2644TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002645 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2646 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2647 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2648 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002651 KeyboardInputMapper& mapper =
2652 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2653 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002655 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002656 ASSERT_NO_FATAL_FAILURE(
2657 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2658 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2659 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2660 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2661 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2662 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2663 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002665 clearViewports();
2666 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002667 ASSERT_NO_FATAL_FAILURE(
2668 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2669 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2670 AKEYCODE_DPAD_UP, DISPLAY_ID));
2671 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2672 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2673 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2674 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002676 clearViewports();
2677 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002678 ASSERT_NO_FATAL_FAILURE(
2679 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2680 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2681 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2682 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2683 AKEYCODE_DPAD_UP, DISPLAY_ID));
2684 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2685 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002687 clearViewports();
2688 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002689 ASSERT_NO_FATAL_FAILURE(
2690 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2691 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2692 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2693 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2694 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2695 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2696 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697
2698 // Special case: if orientation changes while key is down, we still emit the same keycode
2699 // in the key up as we did in the key down.
2700 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002701 clearViewports();
2702 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002703 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2705 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2706 ASSERT_EQ(KEY_UP, args.scanCode);
2707 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2708
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002709 clearViewports();
2710 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002711 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2713 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2714 ASSERT_EQ(KEY_UP, args.scanCode);
2715 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2716}
2717
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002718TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2719 // If the keyboard is not orientation aware,
2720 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002721 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002722
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002723 KeyboardInputMapper& mapper =
2724 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2725 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002726 NotifyKeyArgs args;
2727
2728 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002729 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002731 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2733 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2734
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002735 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002736 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002738 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2740 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2741}
2742
2743TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2744 // If the keyboard is orientation aware,
2745 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002747
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002748 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002749 KeyboardInputMapper& mapper =
2750 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2751 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002752 NotifyKeyArgs args;
2753
2754 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2755 // ^--- already checked by the previous test
2756
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002757 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002758 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002759 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002761 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2763 ASSERT_EQ(DISPLAY_ID, args.displayId);
2764
2765 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002766 clearViewports();
2767 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002768 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002769 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002771 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2773 ASSERT_EQ(newDisplayId, args.displayId);
2774}
2775
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002777 KeyboardInputMapper& mapper =
2778 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2779 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002781 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002783
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002784 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002785 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786}
2787
2788TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002789 KeyboardInputMapper& mapper =
2790 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2791 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002793 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002794 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002796 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002797 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798}
2799
2800TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002801 KeyboardInputMapper& mapper =
2802 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2803 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002805 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
2807 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2808 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002809 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810 ASSERT_TRUE(flags[0]);
2811 ASSERT_FALSE(flags[1]);
2812}
2813
2814TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002815 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2816 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2817 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2818 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2819 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2820 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002822 KeyboardInputMapper& mapper =
2823 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2824 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825
2826 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002827 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2828 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2829 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830
2831 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002832 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2833 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002834 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2835 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2836 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002837 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838
2839 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002840 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2841 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002842 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2843 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2844 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002845 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846
2847 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002848 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2849 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002850 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2851 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2852 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002853 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
2855 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002856 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2857 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002858 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2859 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2860 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002861 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862
2863 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002864 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2865 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002866 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2867 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2868 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002869 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002870
2871 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002872 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2873 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002874 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2875 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2876 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002877 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878}
2879
Arthur Hung2c9a3342019-07-23 14:18:59 +08002880TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2881 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002882 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2883 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2884 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2885 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002886
2887 // keyboard 2.
2888 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08002889 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002890 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002891 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08002892 std::shared_ptr<InputDevice> device2 =
2893 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
2894 Flags<InputDeviceClass>(0));
2895
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002896 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2897 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2898 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2899 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002900
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002901 KeyboardInputMapper& mapper =
2902 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2903 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002905 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002906 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002907 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002908 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2909 device2->reset(ARBITRARY_TIME);
2910
2911 // Prepared displays and associated info.
2912 constexpr uint8_t hdmi1 = 0;
2913 constexpr uint8_t hdmi2 = 1;
2914 const std::string SECONDARY_UNIQUE_ID = "local:1";
2915
2916 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2917 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2918
2919 // No associated display viewport found, should disable the device.
2920 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2921 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2922 ASSERT_FALSE(device2->isEnabled());
2923
2924 // Prepare second display.
2925 constexpr int32_t newDisplayId = 2;
2926 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002927 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002928 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002929 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002930 // Default device will reconfigure above, need additional reconfiguration for another device.
2931 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2932 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2933
2934 // Device should be enabled after the associated display is found.
2935 ASSERT_TRUE(mDevice->isEnabled());
2936 ASSERT_TRUE(device2->isEnabled());
2937
2938 // Test pad key events
2939 ASSERT_NO_FATAL_FAILURE(
2940 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2941 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2942 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2943 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2944 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2945 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2946 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2947
2948 ASSERT_NO_FATAL_FAILURE(
2949 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2950 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2951 AKEYCODE_DPAD_RIGHT, newDisplayId));
2952 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2953 AKEYCODE_DPAD_DOWN, newDisplayId));
2954 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2955 AKEYCODE_DPAD_LEFT, newDisplayId));
2956}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002958// --- KeyboardInputMapperTest_ExternalDevice ---
2959
2960class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2961protected:
2962 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07002963 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002964 }
2965};
2966
2967TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07002968 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2969 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07002970
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002971 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2972 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2973 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2974 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07002975
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002976 KeyboardInputMapper& mapper =
2977 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2978 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07002979
2980 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2981 NotifyKeyArgs args;
2982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2983 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2984
2985 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2987 ASSERT_EQ(uint32_t(0), args.policyFlags);
2988
2989 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2991 ASSERT_EQ(uint32_t(0), args.policyFlags);
2992
2993 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2995 ASSERT_EQ(uint32_t(0), args.policyFlags);
2996
2997 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2999 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3000
3001 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3003 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3004}
3005
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003006TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003007 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003008
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003009 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3010 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3011 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003012
Powei Fengd041c5d2019-05-03 17:11:33 -07003013 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003014 KeyboardInputMapper& mapper =
3015 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3016 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003017
3018 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3019 NotifyKeyArgs args;
3020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3021 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3022
3023 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3025 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3026
3027 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3029 ASSERT_EQ(uint32_t(0), args.policyFlags);
3030
3031 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3033 ASSERT_EQ(uint32_t(0), args.policyFlags);
3034
3035 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3037 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3038
3039 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3041 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3042}
3043
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044// --- CursorInputMapperTest ---
3045
3046class CursorInputMapperTest : public InputMapperTest {
3047protected:
3048 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3049
Michael Wright17db18e2020-06-26 20:51:44 +01003050 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051
Prabir Pradhan28efc192019-11-05 01:10:04 +00003052 virtual void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 InputMapperTest::SetUp();
3054
Michael Wright17db18e2020-06-26 20:51:44 +01003055 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003056 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003057 }
3058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3060 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003061
3062 void prepareDisplay(int32_t orientation) {
3063 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003064 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003065 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3066 orientation, uniqueId, NO_PORT, viewportType);
3067 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068};
3069
3070const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3071
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003072void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3073 int32_t originalY, int32_t rotatedX,
3074 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075 NotifyMotionArgs args;
3076
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003077 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3078 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3079 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3083 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3084 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3085 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3086}
3087
3088TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003090 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003092 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093}
3094
3095TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003097 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100}
3101
3102TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003104 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105
3106 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003107 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108
3109 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003110 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3111 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3113 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3114
3115 // When the bounds are set, then there should be a valid motion range.
3116 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3117
3118 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003119 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120
3121 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3122 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3123 1, 800 - 1, 0.0f, 0.0f));
3124 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3125 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3126 2, 480 - 1, 0.0f, 0.0f));
3127 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3128 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3129 0.0f, 1.0f, 0.0f, 0.0f));
3130}
3131
3132TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003134 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135
3136 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003137 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138
3139 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3140 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3141 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3142 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3143 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3144 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3145 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3146 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3147 0.0f, 1.0f, 0.0f, 0.0f));
3148}
3149
3150TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003152 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153
arthurhungdcef2dc2020-08-11 14:47:50 +08003154 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155
3156 NotifyMotionArgs args;
3157
3158 // Button press.
3159 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003160 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3161 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3163 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3164 ASSERT_EQ(DEVICE_ID, args.deviceId);
3165 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3166 ASSERT_EQ(uint32_t(0), args.policyFlags);
3167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3168 ASSERT_EQ(0, args.flags);
3169 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3170 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3171 ASSERT_EQ(0, args.edgeFlags);
3172 ASSERT_EQ(uint32_t(1), args.pointerCount);
3173 ASSERT_EQ(0, args.pointerProperties[0].id);
3174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3176 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3177 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3178 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3179 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3180
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3182 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3183 ASSERT_EQ(DEVICE_ID, args.deviceId);
3184 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3185 ASSERT_EQ(uint32_t(0), args.policyFlags);
3186 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3187 ASSERT_EQ(0, args.flags);
3188 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3189 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3190 ASSERT_EQ(0, args.edgeFlags);
3191 ASSERT_EQ(uint32_t(1), args.pointerCount);
3192 ASSERT_EQ(0, args.pointerProperties[0].id);
3193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3195 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3196 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3197 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3198 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3199
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003201 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3202 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3204 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3205 ASSERT_EQ(DEVICE_ID, args.deviceId);
3206 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3207 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3209 ASSERT_EQ(0, args.flags);
3210 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3211 ASSERT_EQ(0, args.buttonState);
3212 ASSERT_EQ(0, args.edgeFlags);
3213 ASSERT_EQ(uint32_t(1), args.pointerCount);
3214 ASSERT_EQ(0, args.pointerProperties[0].id);
3215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3217 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3218 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3219 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3220 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3221
3222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3223 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3224 ASSERT_EQ(DEVICE_ID, args.deviceId);
3225 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3226 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3228 ASSERT_EQ(0, args.flags);
3229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3230 ASSERT_EQ(0, args.buttonState);
3231 ASSERT_EQ(0, args.edgeFlags);
3232 ASSERT_EQ(uint32_t(1), args.pointerCount);
3233 ASSERT_EQ(0, args.pointerProperties[0].id);
3234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3236 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3237 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3238 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3239 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3240}
3241
3242TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003244 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245
3246 NotifyMotionArgs args;
3247
3248 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003249 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3250 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3254 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3255
3256 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003257 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3258 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3262 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3263}
3264
3265TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003267 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268
3269 NotifyMotionArgs args;
3270
3271 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003272 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3273 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3275 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3277 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3278
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3280 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3282 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3283
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003285 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3286 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3290 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3291
3292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3295 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3296}
3297
3298TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003300 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301
3302 NotifyMotionArgs args;
3303
3304 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003305 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3306 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3307 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3308 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3310 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3312 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3313 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3314
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3318 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3319 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3320
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003322 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3323 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 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 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3329 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3330
3331 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003332 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3333 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3337 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3338
3339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3342 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3343}
3344
3345TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003347 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003349 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3351 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3352 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3353 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3354 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3355 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3356 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3357 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3358}
3359
3360TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361 addConfigurationProperty("cursor.mode", "navigation");
3362 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003363 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003365 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3367 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3368 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3369 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3374
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003375 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3377 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3378 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3379 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3380 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3381 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3382 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3383 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3384
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003385 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3387 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3388 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3394
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003395 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3397 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3398 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3399 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3400 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3401 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3402 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3403 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3404}
3405
3406TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003408 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409
3410 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3411 mFakePointerController->setPosition(100, 200);
3412 mFakePointerController->setButtonState(0);
3413
3414 NotifyMotionArgs motionArgs;
3415 NotifyKeyArgs keyArgs;
3416
3417 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003418 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3419 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3421 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3422 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3423 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3425 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3426
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3429 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3430 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3432 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3433
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003434 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3435 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438 ASSERT_EQ(0, motionArgs.buttonState);
3439 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3441 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3442
3443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003444 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445 ASSERT_EQ(0, motionArgs.buttonState);
3446 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3448 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3449
3450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003452 ASSERT_EQ(0, motionArgs.buttonState);
3453 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3455 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3456
3457 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003458 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3459 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3460 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3462 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3463 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3464 motionArgs.buttonState);
3465 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3466 mFakePointerController->getButtonState());
3467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3468 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3469
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3472 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3473 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3474 mFakePointerController->getButtonState());
3475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3476 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3477
3478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3479 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3480 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3481 motionArgs.buttonState);
3482 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3483 mFakePointerController->getButtonState());
3484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3485 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3486
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003487 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3488 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003490 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3492 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3494 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3495
3496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003498 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3499 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3501 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3502
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003503 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3504 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003506 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3507 ASSERT_EQ(0, motionArgs.buttonState);
3508 ASSERT_EQ(0, mFakePointerController->getButtonState());
3509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3510 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003511 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3512 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003513
3514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515 ASSERT_EQ(0, motionArgs.buttonState);
3516 ASSERT_EQ(0, mFakePointerController->getButtonState());
3517 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3519 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 -08003520
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3522 ASSERT_EQ(0, motionArgs.buttonState);
3523 ASSERT_EQ(0, mFakePointerController->getButtonState());
3524 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3526 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3527
3528 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003529 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3530 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3532 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3533 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003534
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003536 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3538 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3540 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3541
3542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3544 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3545 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3547 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3548
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003549 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3550 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553 ASSERT_EQ(0, motionArgs.buttonState);
3554 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3556 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3557
3558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003560 ASSERT_EQ(0, motionArgs.buttonState);
3561 ASSERT_EQ(0, mFakePointerController->getButtonState());
3562
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3564 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3566 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3567 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3568
3569 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003570 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3571 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3573 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3574 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003575
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003577 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3579 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3581 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3582
3583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3585 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3586 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3588 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3589
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003590 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3591 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594 ASSERT_EQ(0, motionArgs.buttonState);
3595 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3597 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 -08003598
3599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3601 ASSERT_EQ(0, motionArgs.buttonState);
3602 ASSERT_EQ(0, mFakePointerController->getButtonState());
3603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3604 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3605
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3607 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3608 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3609
3610 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003611 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3612 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3614 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3615 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003616
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003618 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003619 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3620 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3622 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3623
3624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3626 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3627 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3629 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3630
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003631 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3632 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003634 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 ASSERT_EQ(0, motionArgs.buttonState);
3636 ASSERT_EQ(0, mFakePointerController->getButtonState());
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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003639
3640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3641 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3642 ASSERT_EQ(0, motionArgs.buttonState);
3643 ASSERT_EQ(0, mFakePointerController->getButtonState());
3644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3645 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3646
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3648 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3649 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3650
3651 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003652 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3653 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3655 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3656 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003657
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003660 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3661 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3663 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3664
3665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3666 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3667 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3668 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3670 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3671
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003672 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3673 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003675 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003676 ASSERT_EQ(0, motionArgs.buttonState);
3677 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3679 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003680
3681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3682 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3683 ASSERT_EQ(0, motionArgs.buttonState);
3684 ASSERT_EQ(0, mFakePointerController->getButtonState());
3685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3686 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3687
Michael Wrightd02c5b62014-02-10 15:10:22 -08003688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3689 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3690 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3691}
3692
3693TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003695 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696
3697 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3698 mFakePointerController->setPosition(100, 200);
3699 mFakePointerController->setButtonState(0);
3700
3701 NotifyMotionArgs args;
3702
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003703 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3704 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3705 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003707 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3710 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 +01003711 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003712}
3713
3714TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003715 addConfigurationProperty("cursor.mode", "pointer");
3716 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003717 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003718
3719 NotifyDeviceResetArgs resetArgs;
3720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3721 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3722 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3723
3724 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3725 mFakePointerController->setPosition(100, 200);
3726 mFakePointerController->setButtonState(0);
3727
3728 NotifyMotionArgs args;
3729
3730 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003731 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3732 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3733 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3735 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3738 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 +01003739 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003740
3741 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003742 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3743 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3745 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3746 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3748 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3750 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3751 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3753 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3754
3755 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003756 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3757 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3759 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3762 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3764 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3765 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3767 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3768
3769 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003770 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3771 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3772 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3774 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3777 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 +01003778 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003779
3780 // Disable pointer capture and check that the device generation got bumped
3781 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08003782 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003783 mFakePolicy->setPointerCapture(false);
3784 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08003785 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003786
3787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3788 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3789 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3790
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003791 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3792 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3793 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3795 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3798 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 +01003799 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800}
3801
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003802TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003804
Garfield Tan888a6a42020-01-09 11:39:16 -08003805 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003806 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003807 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3808 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003809 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003810 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3811 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3812
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003813 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3814 mFakePointerController->setPosition(100, 200);
3815 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003816
3817 NotifyMotionArgs args;
3818 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3819 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3820 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3822 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3825 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 +01003826 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003827 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3828}
3829
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830// --- TouchInputMapperTest ---
3831
3832class TouchInputMapperTest : public InputMapperTest {
3833protected:
3834 static const int32_t RAW_X_MIN;
3835 static const int32_t RAW_X_MAX;
3836 static const int32_t RAW_Y_MIN;
3837 static const int32_t RAW_Y_MAX;
3838 static const int32_t RAW_TOUCH_MIN;
3839 static const int32_t RAW_TOUCH_MAX;
3840 static const int32_t RAW_TOOL_MIN;
3841 static const int32_t RAW_TOOL_MAX;
3842 static const int32_t RAW_PRESSURE_MIN;
3843 static const int32_t RAW_PRESSURE_MAX;
3844 static const int32_t RAW_ORIENTATION_MIN;
3845 static const int32_t RAW_ORIENTATION_MAX;
3846 static const int32_t RAW_DISTANCE_MIN;
3847 static const int32_t RAW_DISTANCE_MAX;
3848 static const int32_t RAW_TILT_MIN;
3849 static const int32_t RAW_TILT_MAX;
3850 static const int32_t RAW_ID_MIN;
3851 static const int32_t RAW_ID_MAX;
3852 static const int32_t RAW_SLOT_MIN;
3853 static const int32_t RAW_SLOT_MAX;
3854 static const float X_PRECISION;
3855 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003856 static const float X_PRECISION_VIRTUAL;
3857 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858
3859 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003860 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861
3862 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3863
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003864 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003865 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003866
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867 enum Axes {
3868 POSITION = 1 << 0,
3869 TOUCH = 1 << 1,
3870 TOOL = 1 << 2,
3871 PRESSURE = 1 << 3,
3872 ORIENTATION = 1 << 4,
3873 MINOR = 1 << 5,
3874 ID = 1 << 6,
3875 DISTANCE = 1 << 7,
3876 TILT = 1 << 8,
3877 SLOT = 1 << 9,
3878 TOOL_TYPE = 1 << 10,
3879 };
3880
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003881 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3882 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003883 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003885 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886 int32_t toRawX(float displayX);
3887 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003888 float toCookedX(float rawX, float rawY);
3889 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003891 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003893 float toDisplayY(int32_t rawY, int32_t displayHeight);
3894
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895};
3896
3897const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3898const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3899const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3900const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3901const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3902const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3903const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3904const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003905const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3906const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003907const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3908const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3909const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3910const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3911const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3912const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3913const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3914const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3915const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3916const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3917const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3918const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003919const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3920 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3921const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3922 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07003923const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3924 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003925
3926const float TouchInputMapperTest::GEOMETRIC_SCALE =
3927 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3928 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3929
3930const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3931 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3932 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3933};
3934
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003935void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003936 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3937 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003938}
3939
3940void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3941 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3942 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943}
3944
Santos Cordonfa5cf462017-04-05 10:37:00 -07003945void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003946 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
3947 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
3948 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003949}
3950
Michael Wrightd02c5b62014-02-10 15:10:22 -08003951void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003952 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3953 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3954 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3955 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003956}
3957
Jason Gerecke489fda82012-09-07 17:19:40 -07003958void TouchInputMapperTest::prepareLocationCalibration() {
3959 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3960}
3961
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962int32_t TouchInputMapperTest::toRawX(float displayX) {
3963 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3964}
3965
3966int32_t TouchInputMapperTest::toRawY(float displayY) {
3967 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3968}
3969
Jason Gerecke489fda82012-09-07 17:19:40 -07003970float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3971 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3972 return rawX;
3973}
3974
3975float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3976 AFFINE_TRANSFORM.applyTo(rawX, rawY);
3977 return rawY;
3978}
3979
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003981 return toDisplayX(rawX, DISPLAY_WIDTH);
3982}
3983
3984float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3985 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986}
3987
3988float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003989 return toDisplayY(rawY, DISPLAY_HEIGHT);
3990}
3991
3992float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3993 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003994}
3995
3996
3997// --- SingleTouchInputMapperTest ---
3998
3999class SingleTouchInputMapperTest : public TouchInputMapperTest {
4000protected:
4001 void prepareButtons();
4002 void prepareAxes(int axes);
4003
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004004 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4005 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4006 void processUp(SingleTouchInputMapper& mappery);
4007 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4008 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4009 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4010 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4011 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4012 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013};
4014
4015void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004016 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017}
4018
4019void SingleTouchInputMapperTest::prepareAxes(int axes) {
4020 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004021 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4022 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 }
4024 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004025 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4026 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027 }
4028 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004029 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4030 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 }
4032 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004033 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4034 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035 }
4036 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004037 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4038 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 }
4040}
4041
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004042void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004043 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4044 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4045 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046}
4047
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004048void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004049 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4050 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051}
4052
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004053void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004054 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004055}
4056
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004057void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004058 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059}
4060
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004061void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4062 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004063 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064}
4065
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004066void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004067 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068}
4069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004070void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4071 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004072 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4073 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074}
4075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004076void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4077 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004078 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079}
4080
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004081void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004082 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083}
4084
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086 prepareButtons();
4087 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004088 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004090 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091}
4092
4093TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004094 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4095 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096 prepareButtons();
4097 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004098 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004100 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101}
4102
4103TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104 prepareButtons();
4105 prepareAxes(POSITION);
4106 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004107 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004109 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110}
4111
4112TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 prepareButtons();
4114 prepareAxes(POSITION);
4115 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004116 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004118 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119}
4120
4121TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 addConfigurationProperty("touch.deviceType", "touchScreen");
4123 prepareDisplay(DISPLAY_ORIENTATION_0);
4124 prepareButtons();
4125 prepareAxes(POSITION);
4126 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004127 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004128
4129 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004130 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131
4132 // Virtual key is down.
4133 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4134 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4135 processDown(mapper, x, y);
4136 processSync(mapper);
4137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004139 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140
4141 // Virtual key is up.
4142 processUp(mapper);
4143 processSync(mapper);
4144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004146 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147}
4148
4149TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 addConfigurationProperty("touch.deviceType", "touchScreen");
4151 prepareDisplay(DISPLAY_ORIENTATION_0);
4152 prepareButtons();
4153 prepareAxes(POSITION);
4154 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004155 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156
4157 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004158 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159
4160 // Virtual key is down.
4161 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4162 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4163 processDown(mapper, x, y);
4164 processSync(mapper);
4165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004167 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168
4169 // Virtual key is up.
4170 processUp(mapper);
4171 processSync(mapper);
4172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004174 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175}
4176
4177TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 addConfigurationProperty("touch.deviceType", "touchScreen");
4179 prepareDisplay(DISPLAY_ORIENTATION_0);
4180 prepareButtons();
4181 prepareAxes(POSITION);
4182 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004183 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
4185 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4186 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004187 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188 ASSERT_TRUE(flags[0]);
4189 ASSERT_FALSE(flags[1]);
4190}
4191
4192TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 addConfigurationProperty("touch.deviceType", "touchScreen");
4194 prepareDisplay(DISPLAY_ORIENTATION_0);
4195 prepareButtons();
4196 prepareAxes(POSITION);
4197 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004198 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
arthurhungdcef2dc2020-08-11 14:47:50 +08004200 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201
4202 NotifyKeyArgs args;
4203
4204 // Press virtual key.
4205 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4206 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4207 processDown(mapper, x, y);
4208 processSync(mapper);
4209
4210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4211 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4212 ASSERT_EQ(DEVICE_ID, args.deviceId);
4213 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4214 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4215 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4216 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4217 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4218 ASSERT_EQ(KEY_HOME, args.scanCode);
4219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4220 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4221
4222 // Release virtual key.
4223 processUp(mapper);
4224 processSync(mapper);
4225
4226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4227 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4228 ASSERT_EQ(DEVICE_ID, args.deviceId);
4229 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4230 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4231 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4232 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4233 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4234 ASSERT_EQ(KEY_HOME, args.scanCode);
4235 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4236 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4237
4238 // Should not have sent any motions.
4239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4240}
4241
4242TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 addConfigurationProperty("touch.deviceType", "touchScreen");
4244 prepareDisplay(DISPLAY_ORIENTATION_0);
4245 prepareButtons();
4246 prepareAxes(POSITION);
4247 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004248 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249
arthurhungdcef2dc2020-08-11 14:47:50 +08004250 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251
4252 NotifyKeyArgs keyArgs;
4253
4254 // Press virtual key.
4255 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4256 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4257 processDown(mapper, x, y);
4258 processSync(mapper);
4259
4260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4261 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4262 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4263 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4264 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4265 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4266 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4267 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4268 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4269 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4270 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4271
4272 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4273 // into the display area.
4274 y -= 100;
4275 processMove(mapper, x, y);
4276 processSync(mapper);
4277
4278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4279 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4280 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4281 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4282 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4283 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4284 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4285 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4286 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4287 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4288 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4289 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4290
4291 NotifyMotionArgs motionArgs;
4292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4295 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4296 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4297 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4298 ASSERT_EQ(0, motionArgs.flags);
4299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4300 ASSERT_EQ(0, motionArgs.buttonState);
4301 ASSERT_EQ(0, motionArgs.edgeFlags);
4302 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4306 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4307 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4308 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4310
4311 // Keep moving out of bounds. Should generate a pointer move.
4312 y -= 50;
4313 processMove(mapper, x, y);
4314 processSync(mapper);
4315
4316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4317 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4318 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4319 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4320 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4322 ASSERT_EQ(0, motionArgs.flags);
4323 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4324 ASSERT_EQ(0, motionArgs.buttonState);
4325 ASSERT_EQ(0, motionArgs.edgeFlags);
4326 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4327 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4330 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4331 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4332 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4333 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4334
4335 // Release out of bounds. Should generate a pointer up.
4336 processUp(mapper);
4337 processSync(mapper);
4338
4339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4340 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4341 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4342 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4343 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4344 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4345 ASSERT_EQ(0, motionArgs.flags);
4346 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4347 ASSERT_EQ(0, motionArgs.buttonState);
4348 ASSERT_EQ(0, motionArgs.edgeFlags);
4349 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4350 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4353 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4354 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4355 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4356 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4357
4358 // Should not have sent any more keys or motions.
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4361}
4362
4363TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 addConfigurationProperty("touch.deviceType", "touchScreen");
4365 prepareDisplay(DISPLAY_ORIENTATION_0);
4366 prepareButtons();
4367 prepareAxes(POSITION);
4368 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004369 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370
arthurhungdcef2dc2020-08-11 14:47:50 +08004371 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372
4373 NotifyMotionArgs motionArgs;
4374
4375 // Initially go down out of bounds.
4376 int32_t x = -10;
4377 int32_t y = -10;
4378 processDown(mapper, x, y);
4379 processSync(mapper);
4380
4381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4382
4383 // Move into the display area. Should generate a pointer down.
4384 x = 50;
4385 y = 75;
4386 processMove(mapper, x, y);
4387 processSync(mapper);
4388
4389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4390 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4391 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4392 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4393 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4394 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4395 ASSERT_EQ(0, motionArgs.flags);
4396 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4397 ASSERT_EQ(0, motionArgs.buttonState);
4398 ASSERT_EQ(0, motionArgs.edgeFlags);
4399 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4400 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4403 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4404 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4405 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4407
4408 // Release. Should generate a pointer up.
4409 processUp(mapper);
4410 processSync(mapper);
4411
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4413 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4414 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4415 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4416 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4417 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4418 ASSERT_EQ(0, motionArgs.flags);
4419 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4420 ASSERT_EQ(0, motionArgs.buttonState);
4421 ASSERT_EQ(0, motionArgs.edgeFlags);
4422 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4423 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4426 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4427 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4428 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4430
4431 // Should not have sent any more keys or motions.
4432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4434}
4435
Santos Cordonfa5cf462017-04-05 10:37:00 -07004436TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004437 addConfigurationProperty("touch.deviceType", "touchScreen");
4438 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4439
4440 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4441 prepareButtons();
4442 prepareAxes(POSITION);
4443 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004444 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004445
arthurhungdcef2dc2020-08-11 14:47:50 +08004446 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004447
4448 NotifyMotionArgs motionArgs;
4449
4450 // Down.
4451 int32_t x = 100;
4452 int32_t y = 125;
4453 processDown(mapper, x, y);
4454 processSync(mapper);
4455
4456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4458 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4459 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4460 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4461 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4462 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4463 ASSERT_EQ(0, motionArgs.flags);
4464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4465 ASSERT_EQ(0, motionArgs.buttonState);
4466 ASSERT_EQ(0, motionArgs.edgeFlags);
4467 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4468 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4471 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4472 1, 0, 0, 0, 0, 0, 0, 0));
4473 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4474 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4476
4477 // Move.
4478 x += 50;
4479 y += 75;
4480 processMove(mapper, x, y);
4481 processSync(mapper);
4482
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4485 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4486 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4487 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4488 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4490 ASSERT_EQ(0, motionArgs.flags);
4491 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4492 ASSERT_EQ(0, motionArgs.buttonState);
4493 ASSERT_EQ(0, motionArgs.edgeFlags);
4494 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4495 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4498 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4499 1, 0, 0, 0, 0, 0, 0, 0));
4500 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4501 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4502 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4503
4504 // Up.
4505 processUp(mapper);
4506 processSync(mapper);
4507
4508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4509 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4510 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4511 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4512 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4513 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4514 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4515 ASSERT_EQ(0, motionArgs.flags);
4516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4517 ASSERT_EQ(0, motionArgs.buttonState);
4518 ASSERT_EQ(0, motionArgs.edgeFlags);
4519 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4520 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4523 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4524 1, 0, 0, 0, 0, 0, 0, 0));
4525 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4526 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4527 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4528
4529 // Should not have sent any more keys or motions.
4530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4532}
4533
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535 addConfigurationProperty("touch.deviceType", "touchScreen");
4536 prepareDisplay(DISPLAY_ORIENTATION_0);
4537 prepareButtons();
4538 prepareAxes(POSITION);
4539 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004540 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541
arthurhungdcef2dc2020-08-11 14:47:50 +08004542 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543
4544 NotifyMotionArgs motionArgs;
4545
4546 // Down.
4547 int32_t x = 100;
4548 int32_t y = 125;
4549 processDown(mapper, x, y);
4550 processSync(mapper);
4551
4552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4553 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4554 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4555 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4556 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4557 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4558 ASSERT_EQ(0, motionArgs.flags);
4559 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4560 ASSERT_EQ(0, motionArgs.buttonState);
4561 ASSERT_EQ(0, motionArgs.edgeFlags);
4562 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4563 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4566 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4567 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4568 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4570
4571 // Move.
4572 x += 50;
4573 y += 75;
4574 processMove(mapper, x, y);
4575 processSync(mapper);
4576
4577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4578 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4579 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4580 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4581 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4583 ASSERT_EQ(0, motionArgs.flags);
4584 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4585 ASSERT_EQ(0, motionArgs.buttonState);
4586 ASSERT_EQ(0, motionArgs.edgeFlags);
4587 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4588 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4591 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4592 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4593 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4594 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4595
4596 // Up.
4597 processUp(mapper);
4598 processSync(mapper);
4599
4600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4601 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4602 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4603 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4604 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4605 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4606 ASSERT_EQ(0, motionArgs.flags);
4607 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4608 ASSERT_EQ(0, motionArgs.buttonState);
4609 ASSERT_EQ(0, motionArgs.edgeFlags);
4610 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4611 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4614 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4615 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4616 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4617 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4618
4619 // Should not have sent any more keys or motions.
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4622}
4623
4624TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625 addConfigurationProperty("touch.deviceType", "touchScreen");
4626 prepareButtons();
4627 prepareAxes(POSITION);
4628 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004629 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630
4631 NotifyMotionArgs args;
4632
4633 // Rotation 90.
4634 prepareDisplay(DISPLAY_ORIENTATION_90);
4635 processDown(mapper, toRawX(50), toRawY(75));
4636 processSync(mapper);
4637
4638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4639 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4640 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4641
4642 processUp(mapper);
4643 processSync(mapper);
4644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4645}
4646
4647TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 addConfigurationProperty("touch.deviceType", "touchScreen");
4649 prepareButtons();
4650 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004651 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652
4653 NotifyMotionArgs args;
4654
4655 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004656 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 prepareDisplay(DISPLAY_ORIENTATION_0);
4658 processDown(mapper, toRawX(50), toRawY(75));
4659 processSync(mapper);
4660
4661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4662 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4663 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4664
4665 processUp(mapper);
4666 processSync(mapper);
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4668
4669 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004670 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671 prepareDisplay(DISPLAY_ORIENTATION_90);
4672 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4673 processSync(mapper);
4674
4675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4676 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4677 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4678
4679 processUp(mapper);
4680 processSync(mapper);
4681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4682
4683 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004684 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 prepareDisplay(DISPLAY_ORIENTATION_180);
4686 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4687 processSync(mapper);
4688
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4690 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4691 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4692
4693 processUp(mapper);
4694 processSync(mapper);
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4696
4697 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004698 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 prepareDisplay(DISPLAY_ORIENTATION_270);
4700 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4701 processSync(mapper);
4702
4703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4704 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4705 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4706
4707 processUp(mapper);
4708 processSync(mapper);
4709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4710}
4711
4712TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 addConfigurationProperty("touch.deviceType", "touchScreen");
4714 prepareDisplay(DISPLAY_ORIENTATION_0);
4715 prepareButtons();
4716 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004717 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718
4719 // These calculations are based on the input device calibration documentation.
4720 int32_t rawX = 100;
4721 int32_t rawY = 200;
4722 int32_t rawPressure = 10;
4723 int32_t rawToolMajor = 12;
4724 int32_t rawDistance = 2;
4725 int32_t rawTiltX = 30;
4726 int32_t rawTiltY = 110;
4727
4728 float x = toDisplayX(rawX);
4729 float y = toDisplayY(rawY);
4730 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4731 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4732 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4733 float distance = float(rawDistance);
4734
4735 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4736 float tiltScale = M_PI / 180;
4737 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4738 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4739 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4740 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4741
4742 processDown(mapper, rawX, rawY);
4743 processPressure(mapper, rawPressure);
4744 processToolMajor(mapper, rawToolMajor);
4745 processDistance(mapper, rawDistance);
4746 processTilt(mapper, rawTiltX, rawTiltY);
4747 processSync(mapper);
4748
4749 NotifyMotionArgs args;
4750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4752 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4753 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4754}
4755
Jason Gerecke489fda82012-09-07 17:19:40 -07004756TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004757 addConfigurationProperty("touch.deviceType", "touchScreen");
4758 prepareDisplay(DISPLAY_ORIENTATION_0);
4759 prepareLocationCalibration();
4760 prepareButtons();
4761 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004762 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004763
4764 int32_t rawX = 100;
4765 int32_t rawY = 200;
4766
4767 float x = toDisplayX(toCookedX(rawX, rawY));
4768 float y = toDisplayY(toCookedY(rawX, rawY));
4769
4770 processDown(mapper, rawX, rawY);
4771 processSync(mapper);
4772
4773 NotifyMotionArgs args;
4774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4776 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4777}
4778
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780 addConfigurationProperty("touch.deviceType", "touchScreen");
4781 prepareDisplay(DISPLAY_ORIENTATION_0);
4782 prepareButtons();
4783 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004784 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785
4786 NotifyMotionArgs motionArgs;
4787 NotifyKeyArgs keyArgs;
4788
4789 processDown(mapper, 100, 200);
4790 processSync(mapper);
4791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4792 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4793 ASSERT_EQ(0, motionArgs.buttonState);
4794
4795 // press BTN_LEFT, release BTN_LEFT
4796 processKey(mapper, BTN_LEFT, 1);
4797 processSync(mapper);
4798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4800 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4801
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4803 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4804 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4805
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 processKey(mapper, BTN_LEFT, 0);
4807 processSync(mapper);
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004811
4812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815
4816 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4817 processKey(mapper, BTN_RIGHT, 1);
4818 processKey(mapper, BTN_MIDDLE, 1);
4819 processSync(mapper);
4820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4822 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4823 motionArgs.buttonState);
4824
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4826 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4827 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4828
4829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4830 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4831 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4832 motionArgs.buttonState);
4833
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 processKey(mapper, BTN_RIGHT, 0);
4835 processSync(mapper);
4836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004837 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004839
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004842 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843
4844 processKey(mapper, BTN_MIDDLE, 0);
4845 processSync(mapper);
4846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004847 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004849
4850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004852 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853
4854 // press BTN_BACK, release BTN_BACK
4855 processKey(mapper, BTN_BACK, 1);
4856 processSync(mapper);
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4858 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4859 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004860
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004863 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4864
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4866 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4867 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868
4869 processKey(mapper, BTN_BACK, 0);
4870 processSync(mapper);
4871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004872 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004874
4875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004877 ASSERT_EQ(0, motionArgs.buttonState);
4878
Michael Wrightd02c5b62014-02-10 15:10:22 -08004879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4880 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4881 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4882
4883 // press BTN_SIDE, release BTN_SIDE
4884 processKey(mapper, BTN_SIDE, 1);
4885 processSync(mapper);
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4887 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4888 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004889
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004892 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4893
4894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4896 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897
4898 processKey(mapper, BTN_SIDE, 0);
4899 processSync(mapper);
4900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004901 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004903
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004906 ASSERT_EQ(0, motionArgs.buttonState);
4907
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4909 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4910 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4911
4912 // press BTN_FORWARD, release BTN_FORWARD
4913 processKey(mapper, BTN_FORWARD, 1);
4914 processSync(mapper);
4915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4916 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4917 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004918
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004921 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4922
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4924 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4925 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926
4927 processKey(mapper, BTN_FORWARD, 0);
4928 processSync(mapper);
4929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004930 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004932
4933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004935 ASSERT_EQ(0, motionArgs.buttonState);
4936
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4938 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4939 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4940
4941 // press BTN_EXTRA, release BTN_EXTRA
4942 processKey(mapper, BTN_EXTRA, 1);
4943 processSync(mapper);
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4945 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4946 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004947
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004950 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4951
4952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4953 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4954 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955
4956 processKey(mapper, BTN_EXTRA, 0);
4957 processSync(mapper);
4958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004961
4962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004964 ASSERT_EQ(0, motionArgs.buttonState);
4965
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4967 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4968 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4969
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4971
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972 // press BTN_STYLUS, release BTN_STYLUS
4973 processKey(mapper, BTN_STYLUS, 1);
4974 processSync(mapper);
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004977 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4978
4979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4981 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982
4983 processKey(mapper, BTN_STYLUS, 0);
4984 processSync(mapper);
4985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004988
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004991 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992
4993 // press BTN_STYLUS2, release BTN_STYLUS2
4994 processKey(mapper, BTN_STYLUS2, 1);
4995 processSync(mapper);
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004998 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4999
5000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5001 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5002 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003
5004 processKey(mapper, BTN_STYLUS2, 0);
5005 processSync(mapper);
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005007 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005009
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005012 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013
5014 // release touch
5015 processUp(mapper);
5016 processSync(mapper);
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5018 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5019 ASSERT_EQ(0, motionArgs.buttonState);
5020}
5021
5022TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 addConfigurationProperty("touch.deviceType", "touchScreen");
5024 prepareDisplay(DISPLAY_ORIENTATION_0);
5025 prepareButtons();
5026 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005027 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028
5029 NotifyMotionArgs motionArgs;
5030
5031 // default tool type is finger
5032 processDown(mapper, 100, 200);
5033 processSync(mapper);
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5035 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5037
5038 // eraser
5039 processKey(mapper, BTN_TOOL_RUBBER, 1);
5040 processSync(mapper);
5041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5044
5045 // stylus
5046 processKey(mapper, BTN_TOOL_RUBBER, 0);
5047 processKey(mapper, BTN_TOOL_PEN, 1);
5048 processSync(mapper);
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5052
5053 // brush
5054 processKey(mapper, BTN_TOOL_PEN, 0);
5055 processKey(mapper, BTN_TOOL_BRUSH, 1);
5056 processSync(mapper);
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5060
5061 // pencil
5062 processKey(mapper, BTN_TOOL_BRUSH, 0);
5063 processKey(mapper, BTN_TOOL_PENCIL, 1);
5064 processSync(mapper);
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5068
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005069 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070 processKey(mapper, BTN_TOOL_PENCIL, 0);
5071 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5072 processSync(mapper);
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5076
5077 // mouse
5078 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5079 processKey(mapper, BTN_TOOL_MOUSE, 1);
5080 processSync(mapper);
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5084
5085 // lens
5086 processKey(mapper, BTN_TOOL_MOUSE, 0);
5087 processKey(mapper, BTN_TOOL_LENS, 1);
5088 processSync(mapper);
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5092
5093 // double-tap
5094 processKey(mapper, BTN_TOOL_LENS, 0);
5095 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5096 processSync(mapper);
5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5100
5101 // triple-tap
5102 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5103 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5104 processSync(mapper);
5105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5108
5109 // quad-tap
5110 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5111 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5112 processSync(mapper);
5113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5116
5117 // finger
5118 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5119 processKey(mapper, BTN_TOOL_FINGER, 1);
5120 processSync(mapper);
5121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5124
5125 // stylus trumps finger
5126 processKey(mapper, BTN_TOOL_PEN, 1);
5127 processSync(mapper);
5128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5131
5132 // eraser trumps stylus
5133 processKey(mapper, BTN_TOOL_RUBBER, 1);
5134 processSync(mapper);
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5138
5139 // mouse trumps eraser
5140 processKey(mapper, BTN_TOOL_MOUSE, 1);
5141 processSync(mapper);
5142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5145
5146 // back to default tool type
5147 processKey(mapper, BTN_TOOL_MOUSE, 0);
5148 processKey(mapper, BTN_TOOL_RUBBER, 0);
5149 processKey(mapper, BTN_TOOL_PEN, 0);
5150 processKey(mapper, BTN_TOOL_FINGER, 0);
5151 processSync(mapper);
5152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5155}
5156
5157TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005158 addConfigurationProperty("touch.deviceType", "touchScreen");
5159 prepareDisplay(DISPLAY_ORIENTATION_0);
5160 prepareButtons();
5161 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005162 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005163 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164
5165 NotifyMotionArgs motionArgs;
5166
5167 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5168 processKey(mapper, BTN_TOOL_FINGER, 1);
5169 processMove(mapper, 100, 200);
5170 processSync(mapper);
5171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5172 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5174 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5175
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5177 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5179 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5180
5181 // move a little
5182 processMove(mapper, 150, 250);
5183 processSync(mapper);
5184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5185 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5187 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5188
5189 // down when BTN_TOUCH is pressed, pressure defaults to 1
5190 processKey(mapper, BTN_TOUCH, 1);
5191 processSync(mapper);
5192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5193 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5195 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5196
5197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5198 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5200 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5201
5202 // up when BTN_TOUCH is released, hover restored
5203 processKey(mapper, BTN_TOUCH, 0);
5204 processSync(mapper);
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5208 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5209
5210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5211 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5213 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5214
5215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5216 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5218 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5219
5220 // exit hover when pointer goes away
5221 processKey(mapper, BTN_TOOL_FINGER, 0);
5222 processSync(mapper);
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5224 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5226 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5227}
5228
5229TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005230 addConfigurationProperty("touch.deviceType", "touchScreen");
5231 prepareDisplay(DISPLAY_ORIENTATION_0);
5232 prepareButtons();
5233 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005234 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235
5236 NotifyMotionArgs motionArgs;
5237
5238 // initially hovering because pressure is 0
5239 processDown(mapper, 100, 200);
5240 processPressure(mapper, 0);
5241 processSync(mapper);
5242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5243 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5245 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5246
5247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5250 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5251
5252 // move a little
5253 processMove(mapper, 150, 250);
5254 processSync(mapper);
5255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5256 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5258 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5259
5260 // down when pressure is non-zero
5261 processPressure(mapper, RAW_PRESSURE_MAX);
5262 processSync(mapper);
5263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5264 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5266 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5267
5268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5269 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5271 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5272
5273 // up when pressure becomes 0, hover restored
5274 processPressure(mapper, 0);
5275 processSync(mapper);
5276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5277 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5279 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5280
5281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5282 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5284 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5285
5286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5287 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5289 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5290
5291 // exit hover when pointer goes away
5292 processUp(mapper);
5293 processSync(mapper);
5294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5297 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5298}
5299
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300// --- MultiTouchInputMapperTest ---
5301
5302class MultiTouchInputMapperTest : public TouchInputMapperTest {
5303protected:
5304 void prepareAxes(int axes);
5305
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005306 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5307 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5308 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5309 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5310 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5311 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5312 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5313 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5314 void processId(MultiTouchInputMapper& mapper, int32_t id);
5315 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5316 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5317 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5318 void processMTSync(MultiTouchInputMapper& mapper);
5319 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320};
5321
5322void MultiTouchInputMapperTest::prepareAxes(int axes) {
5323 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005324 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5325 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326 }
5327 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005328 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5329 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5332 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 }
5334 }
5335 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005336 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5337 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005339 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5340 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341 }
5342 }
5343 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005344 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5345 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346 }
5347 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005348 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5349 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350 }
5351 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005352 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5353 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354 }
5355 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005356 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5357 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358 }
5359 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005360 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5361 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362 }
5363 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005364 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365 }
5366}
5367
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005368void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5369 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005370 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5371 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372}
5373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5375 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005376 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377}
5378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005379void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5380 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005381 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382}
5383
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005385 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386}
5387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005388void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005389 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390}
5391
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005392void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5393 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005394 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395}
5396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005397void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005398 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399}
5400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005401void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005402 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403}
5404
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005405void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005406 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407}
5408
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005409void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005410 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411}
5412
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005413void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005414 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415}
5416
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005417void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5418 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005419 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420}
5421
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005422void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005423 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424}
5425
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005426void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005427 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428}
5429
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431 addConfigurationProperty("touch.deviceType", "touchScreen");
5432 prepareDisplay(DISPLAY_ORIENTATION_0);
5433 prepareAxes(POSITION);
5434 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005435 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436
arthurhungdcef2dc2020-08-11 14:47:50 +08005437 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005438
5439 NotifyMotionArgs motionArgs;
5440
5441 // Two fingers down at once.
5442 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5443 processPosition(mapper, x1, y1);
5444 processMTSync(mapper);
5445 processPosition(mapper, x2, y2);
5446 processMTSync(mapper);
5447 processSync(mapper);
5448
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5451 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5452 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5453 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5455 ASSERT_EQ(0, motionArgs.flags);
5456 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5457 ASSERT_EQ(0, motionArgs.buttonState);
5458 ASSERT_EQ(0, motionArgs.edgeFlags);
5459 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5460 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5463 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5464 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5465 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5467
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5469 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5470 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5471 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5472 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5473 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5474 motionArgs.action);
5475 ASSERT_EQ(0, motionArgs.flags);
5476 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5477 ASSERT_EQ(0, motionArgs.buttonState);
5478 ASSERT_EQ(0, motionArgs.edgeFlags);
5479 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5480 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5481 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5482 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5485 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5487 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5488 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5489 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5490 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5491
5492 // Move.
5493 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5494 processPosition(mapper, x1, y1);
5495 processMTSync(mapper);
5496 processPosition(mapper, x2, y2);
5497 processMTSync(mapper);
5498 processSync(mapper);
5499
5500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5501 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5502 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5503 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5504 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5506 ASSERT_EQ(0, motionArgs.flags);
5507 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5508 ASSERT_EQ(0, motionArgs.buttonState);
5509 ASSERT_EQ(0, motionArgs.edgeFlags);
5510 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5511 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5513 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5516 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5518 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5519 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5520 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5522
5523 // First finger up.
5524 x2 += 15; y2 -= 20;
5525 processPosition(mapper, x2, y2);
5526 processMTSync(mapper);
5527 processSync(mapper);
5528
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5530 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5531 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5532 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5533 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5534 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5535 motionArgs.action);
5536 ASSERT_EQ(0, motionArgs.flags);
5537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5538 ASSERT_EQ(0, motionArgs.buttonState);
5539 ASSERT_EQ(0, motionArgs.edgeFlags);
5540 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5541 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5543 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5546 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5548 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5549 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5550 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5551 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5552
5553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5554 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5555 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5556 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5557 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5559 ASSERT_EQ(0, motionArgs.flags);
5560 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5561 ASSERT_EQ(0, motionArgs.buttonState);
5562 ASSERT_EQ(0, motionArgs.edgeFlags);
5563 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5564 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5567 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5568 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5569 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5570 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5571
5572 // Move.
5573 x2 += 20; y2 -= 25;
5574 processPosition(mapper, x2, y2);
5575 processMTSync(mapper);
5576 processSync(mapper);
5577
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5579 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5580 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5581 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5582 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5584 ASSERT_EQ(0, motionArgs.flags);
5585 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5586 ASSERT_EQ(0, motionArgs.buttonState);
5587 ASSERT_EQ(0, motionArgs.edgeFlags);
5588 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5589 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
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 // New finger down.
5598 int32_t x3 = 700, y3 = 300;
5599 processPosition(mapper, x2, y2);
5600 processMTSync(mapper);
5601 processPosition(mapper, x3, y3);
5602 processMTSync(mapper);
5603 processSync(mapper);
5604
5605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5606 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5607 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5608 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5609 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5610 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5611 motionArgs.action);
5612 ASSERT_EQ(0, motionArgs.flags);
5613 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5614 ASSERT_EQ(0, motionArgs.buttonState);
5615 ASSERT_EQ(0, motionArgs.edgeFlags);
5616 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5617 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5619 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5620 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5622 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5624 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5625 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5626 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5628
5629 // Second finger up.
5630 x3 += 30; y3 -= 20;
5631 processPosition(mapper, x3, y3);
5632 processMTSync(mapper);
5633 processSync(mapper);
5634
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5636 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5637 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5638 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5639 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5640 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5641 motionArgs.action);
5642 ASSERT_EQ(0, motionArgs.flags);
5643 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5644 ASSERT_EQ(0, motionArgs.buttonState);
5645 ASSERT_EQ(0, motionArgs.edgeFlags);
5646 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5647 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5649 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5652 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5654 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5655 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5656 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5657 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5658
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5660 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5661 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5662 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5663 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5665 ASSERT_EQ(0, motionArgs.flags);
5666 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5667 ASSERT_EQ(0, motionArgs.buttonState);
5668 ASSERT_EQ(0, motionArgs.edgeFlags);
5669 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5670 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5673 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5674 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5675 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5676 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5677
5678 // Last finger up.
5679 processMTSync(mapper);
5680 processSync(mapper);
5681
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5683 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5684 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5685 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5686 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5687 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5688 ASSERT_EQ(0, motionArgs.flags);
5689 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5690 ASSERT_EQ(0, motionArgs.buttonState);
5691 ASSERT_EQ(0, motionArgs.edgeFlags);
5692 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5693 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].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_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5698 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5699 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5700
5701 // Should not have sent any more keys or motions.
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5704}
5705
5706TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 addConfigurationProperty("touch.deviceType", "touchScreen");
5708 prepareDisplay(DISPLAY_ORIENTATION_0);
5709 prepareAxes(POSITION | ID);
5710 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005711 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005712
arthurhungdcef2dc2020-08-11 14:47:50 +08005713 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005714
5715 NotifyMotionArgs motionArgs;
5716
5717 // Two fingers down at once.
5718 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5719 processPosition(mapper, x1, y1);
5720 processId(mapper, 1);
5721 processMTSync(mapper);
5722 processPosition(mapper, x2, y2);
5723 processId(mapper, 2);
5724 processMTSync(mapper);
5725 processSync(mapper);
5726
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5728 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5729 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5730 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5733 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5734
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5736 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5737 motionArgs.action);
5738 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5739 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5740 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5741 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5744 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5746 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5747
5748 // Move.
5749 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5750 processPosition(mapper, x1, y1);
5751 processId(mapper, 1);
5752 processMTSync(mapper);
5753 processPosition(mapper, x2, y2);
5754 processId(mapper, 2);
5755 processMTSync(mapper);
5756 processSync(mapper);
5757
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5760 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5761 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5763 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5766 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5768 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5769
5770 // First finger up.
5771 x2 += 15; y2 -= 20;
5772 processPosition(mapper, x2, y2);
5773 processId(mapper, 2);
5774 processMTSync(mapper);
5775 processSync(mapper);
5776
5777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5778 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5779 motionArgs.action);
5780 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5781 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5783 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5786 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5788 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5789
5790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5792 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5793 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5796 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5797
5798 // Move.
5799 x2 += 20; y2 -= 25;
5800 processPosition(mapper, x2, y2);
5801 processId(mapper, 2);
5802 processMTSync(mapper);
5803 processSync(mapper);
5804
5805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5807 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5808 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5811 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5812
5813 // New finger down.
5814 int32_t x3 = 700, y3 = 300;
5815 processPosition(mapper, x2, y2);
5816 processId(mapper, 2);
5817 processMTSync(mapper);
5818 processPosition(mapper, x3, y3);
5819 processId(mapper, 3);
5820 processMTSync(mapper);
5821 processSync(mapper);
5822
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5825 motionArgs.action);
5826 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5827 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5828 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5829 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5832 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5834 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5835
5836 // Second finger up.
5837 x3 += 30; y3 -= 20;
5838 processPosition(mapper, x3, y3);
5839 processId(mapper, 3);
5840 processMTSync(mapper);
5841 processSync(mapper);
5842
5843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5844 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5845 motionArgs.action);
5846 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5847 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5849 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5852 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5854 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5855
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5858 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5859 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5860 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5862 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5863
5864 // Last finger up.
5865 processMTSync(mapper);
5866 processSync(mapper);
5867
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5869 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5870 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5871 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5874 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5875
5876 // Should not have sent any more keys or motions.
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5879}
5880
5881TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005882 addConfigurationProperty("touch.deviceType", "touchScreen");
5883 prepareDisplay(DISPLAY_ORIENTATION_0);
5884 prepareAxes(POSITION | ID | SLOT);
5885 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005886 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887
arthurhungdcef2dc2020-08-11 14:47:50 +08005888 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889
5890 NotifyMotionArgs motionArgs;
5891
5892 // Two fingers down at once.
5893 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5894 processPosition(mapper, x1, y1);
5895 processId(mapper, 1);
5896 processSlot(mapper, 1);
5897 processPosition(mapper, x2, y2);
5898 processId(mapper, 2);
5899 processSync(mapper);
5900
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5902 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5903 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5904 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5905 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5907 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5908
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5911 motionArgs.action);
5912 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5913 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5915 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5918 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5920 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5921
5922 // Move.
5923 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5924 processSlot(mapper, 0);
5925 processPosition(mapper, x1, y1);
5926 processSlot(mapper, 1);
5927 processPosition(mapper, x2, y2);
5928 processSync(mapper);
5929
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5932 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5933 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5934 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5935 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5938 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5940 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5941
5942 // First finger up.
5943 x2 += 15; y2 -= 20;
5944 processSlot(mapper, 0);
5945 processId(mapper, -1);
5946 processSlot(mapper, 1);
5947 processPosition(mapper, x2, y2);
5948 processSync(mapper);
5949
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5951 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5952 motionArgs.action);
5953 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5954 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5955 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5956 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5959 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5961 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5962
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5965 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5966 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5969 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5970
5971 // Move.
5972 x2 += 20; y2 -= 25;
5973 processPosition(mapper, x2, y2);
5974 processSync(mapper);
5975
5976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5978 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5979 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5982 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5983
5984 // New finger down.
5985 int32_t x3 = 700, y3 = 300;
5986 processPosition(mapper, x2, y2);
5987 processSlot(mapper, 0);
5988 processId(mapper, 3);
5989 processPosition(mapper, x3, y3);
5990 processSync(mapper);
5991
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5993 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5994 motionArgs.action);
5995 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5996 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5998 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6001 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6003 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6004
6005 // Second finger up.
6006 x3 += 30; y3 -= 20;
6007 processSlot(mapper, 1);
6008 processId(mapper, -1);
6009 processSlot(mapper, 0);
6010 processPosition(mapper, x3, y3);
6011 processSync(mapper);
6012
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6014 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6015 motionArgs.action);
6016 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6017 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6019 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6022 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6024 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6025
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6028 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6029 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6032 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6033
6034 // Last finger up.
6035 processId(mapper, -1);
6036 processSync(mapper);
6037
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6040 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6041 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6044 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6045
6046 // Should not have sent any more keys or motions.
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6049}
6050
6051TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 addConfigurationProperty("touch.deviceType", "touchScreen");
6053 prepareDisplay(DISPLAY_ORIENTATION_0);
6054 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006055 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056
6057 // These calculations are based on the input device calibration documentation.
6058 int32_t rawX = 100;
6059 int32_t rawY = 200;
6060 int32_t rawTouchMajor = 7;
6061 int32_t rawTouchMinor = 6;
6062 int32_t rawToolMajor = 9;
6063 int32_t rawToolMinor = 8;
6064 int32_t rawPressure = 11;
6065 int32_t rawDistance = 0;
6066 int32_t rawOrientation = 3;
6067 int32_t id = 5;
6068
6069 float x = toDisplayX(rawX);
6070 float y = toDisplayY(rawY);
6071 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6072 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6073 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6074 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6075 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6076 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6077 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6078 float distance = float(rawDistance);
6079
6080 processPosition(mapper, rawX, rawY);
6081 processTouchMajor(mapper, rawTouchMajor);
6082 processTouchMinor(mapper, rawTouchMinor);
6083 processToolMajor(mapper, rawToolMajor);
6084 processToolMinor(mapper, rawToolMinor);
6085 processPressure(mapper, rawPressure);
6086 processOrientation(mapper, rawOrientation);
6087 processDistance(mapper, rawDistance);
6088 processId(mapper, id);
6089 processMTSync(mapper);
6090 processSync(mapper);
6091
6092 NotifyMotionArgs args;
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6094 ASSERT_EQ(0, args.pointerProperties[0].id);
6095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6096 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6097 orientation, distance));
6098}
6099
6100TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101 addConfigurationProperty("touch.deviceType", "touchScreen");
6102 prepareDisplay(DISPLAY_ORIENTATION_0);
6103 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6104 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006105 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106
6107 // These calculations are based on the input device calibration documentation.
6108 int32_t rawX = 100;
6109 int32_t rawY = 200;
6110 int32_t rawTouchMajor = 140;
6111 int32_t rawTouchMinor = 120;
6112 int32_t rawToolMajor = 180;
6113 int32_t rawToolMinor = 160;
6114
6115 float x = toDisplayX(rawX);
6116 float y = toDisplayY(rawY);
6117 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6118 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6119 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6120 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6121 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6122
6123 processPosition(mapper, rawX, rawY);
6124 processTouchMajor(mapper, rawTouchMajor);
6125 processTouchMinor(mapper, rawTouchMinor);
6126 processToolMajor(mapper, rawToolMajor);
6127 processToolMinor(mapper, rawToolMinor);
6128 processMTSync(mapper);
6129 processSync(mapper);
6130
6131 NotifyMotionArgs args;
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6134 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6135}
6136
6137TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138 addConfigurationProperty("touch.deviceType", "touchScreen");
6139 prepareDisplay(DISPLAY_ORIENTATION_0);
6140 prepareAxes(POSITION | TOUCH | TOOL);
6141 addConfigurationProperty("touch.size.calibration", "diameter");
6142 addConfigurationProperty("touch.size.scale", "10");
6143 addConfigurationProperty("touch.size.bias", "160");
6144 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006145 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146
6147 // These calculations are based on the input device calibration documentation.
6148 // Note: We only provide a single common touch/tool value because the device is assumed
6149 // not to emit separate values for each pointer (isSummed = 1).
6150 int32_t rawX = 100;
6151 int32_t rawY = 200;
6152 int32_t rawX2 = 150;
6153 int32_t rawY2 = 250;
6154 int32_t rawTouchMajor = 5;
6155 int32_t rawToolMajor = 8;
6156
6157 float x = toDisplayX(rawX);
6158 float y = toDisplayY(rawY);
6159 float x2 = toDisplayX(rawX2);
6160 float y2 = toDisplayY(rawY2);
6161 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6162 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6163 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6164
6165 processPosition(mapper, rawX, rawY);
6166 processTouchMajor(mapper, rawTouchMajor);
6167 processToolMajor(mapper, rawToolMajor);
6168 processMTSync(mapper);
6169 processPosition(mapper, rawX2, rawY2);
6170 processTouchMajor(mapper, rawTouchMajor);
6171 processToolMajor(mapper, rawToolMajor);
6172 processMTSync(mapper);
6173 processSync(mapper);
6174
6175 NotifyMotionArgs args;
6176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6177 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6178
6179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6180 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6181 args.action);
6182 ASSERT_EQ(size_t(2), args.pointerCount);
6183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6184 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6186 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6187}
6188
6189TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 addConfigurationProperty("touch.deviceType", "touchScreen");
6191 prepareDisplay(DISPLAY_ORIENTATION_0);
6192 prepareAxes(POSITION | TOUCH | TOOL);
6193 addConfigurationProperty("touch.size.calibration", "area");
6194 addConfigurationProperty("touch.size.scale", "43");
6195 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006196 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197
6198 // These calculations are based on the input device calibration documentation.
6199 int32_t rawX = 100;
6200 int32_t rawY = 200;
6201 int32_t rawTouchMajor = 5;
6202 int32_t rawToolMajor = 8;
6203
6204 float x = toDisplayX(rawX);
6205 float y = toDisplayY(rawY);
6206 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6207 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6208 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6209
6210 processPosition(mapper, rawX, rawY);
6211 processTouchMajor(mapper, rawTouchMajor);
6212 processToolMajor(mapper, rawToolMajor);
6213 processMTSync(mapper);
6214 processSync(mapper);
6215
6216 NotifyMotionArgs args;
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6219 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6220}
6221
6222TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006223 addConfigurationProperty("touch.deviceType", "touchScreen");
6224 prepareDisplay(DISPLAY_ORIENTATION_0);
6225 prepareAxes(POSITION | PRESSURE);
6226 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6227 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006228 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229
Michael Wrightaa449c92017-12-13 21:21:43 +00006230 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006231 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006232 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6233 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6234 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6235
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 // These calculations are based on the input device calibration documentation.
6237 int32_t rawX = 100;
6238 int32_t rawY = 200;
6239 int32_t rawPressure = 60;
6240
6241 float x = toDisplayX(rawX);
6242 float y = toDisplayY(rawY);
6243 float pressure = float(rawPressure) * 0.01f;
6244
6245 processPosition(mapper, rawX, rawY);
6246 processPressure(mapper, rawPressure);
6247 processMTSync(mapper);
6248 processSync(mapper);
6249
6250 NotifyMotionArgs args;
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6253 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6254}
6255
6256TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257 addConfigurationProperty("touch.deviceType", "touchScreen");
6258 prepareDisplay(DISPLAY_ORIENTATION_0);
6259 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006260 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261
6262 NotifyMotionArgs motionArgs;
6263 NotifyKeyArgs keyArgs;
6264
6265 processId(mapper, 1);
6266 processPosition(mapper, 100, 200);
6267 processSync(mapper);
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6270 ASSERT_EQ(0, motionArgs.buttonState);
6271
6272 // press BTN_LEFT, release BTN_LEFT
6273 processKey(mapper, BTN_LEFT, 1);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6277 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6278
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6281 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6282
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 processKey(mapper, BTN_LEFT, 0);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006291 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006292
6293 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6294 processKey(mapper, BTN_RIGHT, 1);
6295 processKey(mapper, BTN_MIDDLE, 1);
6296 processSync(mapper);
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6299 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6300 motionArgs.buttonState);
6301
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6304 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6305
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6308 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6309 motionArgs.buttonState);
6310
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 processKey(mapper, BTN_RIGHT, 0);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006314 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006315 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006316
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006319 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320
6321 processKey(mapper, BTN_MIDDLE, 0);
6322 processSync(mapper);
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006324 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330
6331 // press BTN_BACK, release BTN_BACK
6332 processKey(mapper, BTN_BACK, 1);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6335 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6336 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006337
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006340 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6341
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6344 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345
6346 processKey(mapper, BTN_BACK, 0);
6347 processSync(mapper);
6348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006350 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006351
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006354 ASSERT_EQ(0, motionArgs.buttonState);
6355
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6357 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6358 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6359
6360 // press BTN_SIDE, release BTN_SIDE
6361 processKey(mapper, BTN_SIDE, 1);
6362 processSync(mapper);
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6364 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6365 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006366
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6370
6371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6372 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6373 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374
6375 processKey(mapper, BTN_SIDE, 0);
6376 processSync(mapper);
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006378 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006380
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006383 ASSERT_EQ(0, motionArgs.buttonState);
6384
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6386 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6387 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6388
6389 // press BTN_FORWARD, release BTN_FORWARD
6390 processKey(mapper, BTN_FORWARD, 1);
6391 processSync(mapper);
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6393 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6394 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006395
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6399
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6402 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006403
6404 processKey(mapper, BTN_FORWARD, 0);
6405 processSync(mapper);
6406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006408 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006409
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006412 ASSERT_EQ(0, motionArgs.buttonState);
6413
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6415 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6416 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6417
6418 // press BTN_EXTRA, release BTN_EXTRA
6419 processKey(mapper, BTN_EXTRA, 1);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6422 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6423 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006424
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6428
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6431 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432
6433 processKey(mapper, BTN_EXTRA, 0);
6434 processSync(mapper);
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006436 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006438
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006441 ASSERT_EQ(0, motionArgs.buttonState);
6442
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6444 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6445 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6446
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6448
Michael Wrightd02c5b62014-02-10 15:10:22 -08006449 // press BTN_STYLUS, release BTN_STYLUS
6450 processKey(mapper, BTN_STYLUS, 1);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006454 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6455
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6458 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006459
6460 processKey(mapper, BTN_STYLUS, 0);
6461 processSync(mapper);
6462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006464 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006465
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006468 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469
6470 // press BTN_STYLUS2, release BTN_STYLUS2
6471 processKey(mapper, BTN_STYLUS2, 1);
6472 processSync(mapper);
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006475 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6476
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6478 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6479 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480
6481 processKey(mapper, BTN_STYLUS2, 0);
6482 processSync(mapper);
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006485 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006486
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006489 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490
6491 // release touch
6492 processId(mapper, -1);
6493 processSync(mapper);
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6495 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6496 ASSERT_EQ(0, motionArgs.buttonState);
6497}
6498
6499TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500 addConfigurationProperty("touch.deviceType", "touchScreen");
6501 prepareDisplay(DISPLAY_ORIENTATION_0);
6502 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006503 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006504
6505 NotifyMotionArgs motionArgs;
6506
6507 // default tool type is finger
6508 processId(mapper, 1);
6509 processPosition(mapper, 100, 200);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6513 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6514
6515 // eraser
6516 processKey(mapper, BTN_TOOL_RUBBER, 1);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6521
6522 // stylus
6523 processKey(mapper, BTN_TOOL_RUBBER, 0);
6524 processKey(mapper, BTN_TOOL_PEN, 1);
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6529
6530 // brush
6531 processKey(mapper, BTN_TOOL_PEN, 0);
6532 processKey(mapper, BTN_TOOL_BRUSH, 1);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6537
6538 // pencil
6539 processKey(mapper, BTN_TOOL_BRUSH, 0);
6540 processKey(mapper, BTN_TOOL_PENCIL, 1);
6541 processSync(mapper);
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6545
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006546 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547 processKey(mapper, BTN_TOOL_PENCIL, 0);
6548 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6549 processSync(mapper);
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6553
6554 // mouse
6555 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6556 processKey(mapper, BTN_TOOL_MOUSE, 1);
6557 processSync(mapper);
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6561
6562 // lens
6563 processKey(mapper, BTN_TOOL_MOUSE, 0);
6564 processKey(mapper, BTN_TOOL_LENS, 1);
6565 processSync(mapper);
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6569
6570 // double-tap
6571 processKey(mapper, BTN_TOOL_LENS, 0);
6572 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6573 processSync(mapper);
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6577
6578 // triple-tap
6579 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6580 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6581 processSync(mapper);
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6585
6586 // quad-tap
6587 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6588 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6589 processSync(mapper);
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6593
6594 // finger
6595 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6596 processKey(mapper, BTN_TOOL_FINGER, 1);
6597 processSync(mapper);
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6601
6602 // stylus trumps finger
6603 processKey(mapper, BTN_TOOL_PEN, 1);
6604 processSync(mapper);
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6608
6609 // eraser trumps stylus
6610 processKey(mapper, BTN_TOOL_RUBBER, 1);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6615
6616 // mouse trumps eraser
6617 processKey(mapper, BTN_TOOL_MOUSE, 1);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6622
6623 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6624 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6625 processSync(mapper);
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6629
6630 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6631 processToolType(mapper, MT_TOOL_PEN);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6636
6637 // back to default tool type
6638 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6639 processKey(mapper, BTN_TOOL_MOUSE, 0);
6640 processKey(mapper, BTN_TOOL_RUBBER, 0);
6641 processKey(mapper, BTN_TOOL_PEN, 0);
6642 processKey(mapper, BTN_TOOL_FINGER, 0);
6643 processSync(mapper);
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6647}
6648
6649TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006650 addConfigurationProperty("touch.deviceType", "touchScreen");
6651 prepareDisplay(DISPLAY_ORIENTATION_0);
6652 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006653 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006654 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006655
6656 NotifyMotionArgs motionArgs;
6657
6658 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6659 processId(mapper, 1);
6660 processPosition(mapper, 100, 200);
6661 processSync(mapper);
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6665 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6670 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6671
6672 // move a little
6673 processPosition(mapper, 150, 250);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6678 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6679
6680 // down when BTN_TOUCH is pressed, pressure defaults to 1
6681 processKey(mapper, BTN_TOUCH, 1);
6682 processSync(mapper);
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6684 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6686 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6687
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6689 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6691 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6692
6693 // up when BTN_TOUCH is released, hover restored
6694 processKey(mapper, BTN_TOUCH, 0);
6695 processSync(mapper);
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6697 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6699 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6700
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6702 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6704 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6705
6706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6707 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6709 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6710
6711 // exit hover when pointer goes away
6712 processId(mapper, -1);
6713 processSync(mapper);
6714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6715 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6717 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6718}
6719
6720TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006721 addConfigurationProperty("touch.deviceType", "touchScreen");
6722 prepareDisplay(DISPLAY_ORIENTATION_0);
6723 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006724 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006725
6726 NotifyMotionArgs motionArgs;
6727
6728 // initially hovering because pressure is 0
6729 processId(mapper, 1);
6730 processPosition(mapper, 100, 200);
6731 processPressure(mapper, 0);
6732 processSync(mapper);
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6736 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6737
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6741 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6742
6743 // move a little
6744 processPosition(mapper, 150, 250);
6745 processSync(mapper);
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6747 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6749 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6750
6751 // down when pressure becomes non-zero
6752 processPressure(mapper, RAW_PRESSURE_MAX);
6753 processSync(mapper);
6754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6755 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6757 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6758
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6760 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6762 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6763
6764 // up when pressure becomes 0, hover restored
6765 processPressure(mapper, 0);
6766 processSync(mapper);
6767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6768 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6770 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6771
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6773 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6775 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6776
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6778 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6780 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6781
6782 // exit hover when pointer goes away
6783 processId(mapper, -1);
6784 processSync(mapper);
6785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6786 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6788 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6789}
6790
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006791/**
6792 * Set the input device port <--> display port associations, and check that the
6793 * events are routed to the display that matches the display port.
6794 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6795 */
6796TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006797 const std::string usb2 = "USB2";
6798 const uint8_t hdmi1 = 0;
6799 const uint8_t hdmi2 = 1;
6800 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006801 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006802
6803 addConfigurationProperty("touch.deviceType", "touchScreen");
6804 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006805 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006806
6807 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6808 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6809
6810 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6811 // for this input device is specified, and the matching viewport is not present,
6812 // the input device should be disabled (at the mapper level).
6813
6814 // Add viewport for display 2 on hdmi2
6815 prepareSecondaryDisplay(type, hdmi2);
6816 // Send a touch event
6817 processPosition(mapper, 100, 100);
6818 processSync(mapper);
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6820
6821 // Add viewport for display 1 on hdmi1
6822 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6823 // Send a touch event again
6824 processPosition(mapper, 100, 100);
6825 processSync(mapper);
6826
6827 NotifyMotionArgs args;
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6829 ASSERT_EQ(DISPLAY_ID, args.displayId);
6830}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006831
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006832TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006833 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006834 std::shared_ptr<FakePointerController> fakePointerController =
6835 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006836 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006837 fakePointerController->setPosition(100, 200);
6838 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006839 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6840
Garfield Tan888a6a42020-01-09 11:39:16 -08006841 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006842 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006843
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006844 prepareDisplay(DISPLAY_ORIENTATION_0);
6845 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006846 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006847
6848 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006849 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006850
6851 NotifyMotionArgs motionArgs;
6852 processPosition(mapper, 100, 100);
6853 processSync(mapper);
6854
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6856 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6857 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6858}
6859
Arthur Hung7c645402019-01-25 17:45:42 +08006860TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6861 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006862 prepareAxes(POSITION | ID | SLOT);
6863 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006864 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006865
6866 // Create the second touch screen device, and enable multi fingers.
6867 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08006868 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006869 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006870 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08006871 std::shared_ptr<InputDevice> device2 =
6872 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
6873 Flags<InputDeviceClass>(0));
6874
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006875 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6876 0 /*flat*/, 0 /*fuzz*/);
6877 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6878 0 /*flat*/, 0 /*fuzz*/);
6879 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6880 0 /*flat*/, 0 /*fuzz*/);
6881 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6882 0 /*flat*/, 0 /*fuzz*/);
6883 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6884 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6885 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006886
6887 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006888 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006889 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6890 device2->reset(ARBITRARY_TIME);
6891
6892 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006893 std::shared_ptr<FakePointerController> fakePointerController =
6894 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006895 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6896 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6897
6898 // Setup policy for associated displays and show touches.
6899 const uint8_t hdmi1 = 0;
6900 const uint8_t hdmi2 = 1;
6901 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6902 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6903 mFakePolicy->setShowTouches(true);
6904
6905 // Create displays.
6906 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006907 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006908
6909 // Default device will reconfigure above, need additional reconfiguration for another device.
6910 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006911 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08006912
6913 // Two fingers down at default display.
6914 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6915 processPosition(mapper, x1, y1);
6916 processId(mapper, 1);
6917 processSlot(mapper, 1);
6918 processPosition(mapper, x2, y2);
6919 processId(mapper, 2);
6920 processSync(mapper);
6921
6922 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6923 fakePointerController->getSpots().find(DISPLAY_ID);
6924 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6925 ASSERT_EQ(size_t(2), iter->second.size());
6926
6927 // Two fingers down at second display.
6928 processPosition(mapper2, x1, y1);
6929 processId(mapper2, 1);
6930 processSlot(mapper2, 1);
6931 processPosition(mapper2, x2, y2);
6932 processId(mapper2, 2);
6933 processSync(mapper2);
6934
6935 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6936 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6937 ASSERT_EQ(size_t(2), iter->second.size());
6938}
6939
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006940TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006941 prepareAxes(POSITION);
6942 addConfigurationProperty("touch.deviceType", "touchScreen");
6943 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006944 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006945
6946 NotifyMotionArgs motionArgs;
6947 // Unrotated video frame
6948 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6949 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006950 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06006951 processPosition(mapper, 100, 200);
6952 processSync(mapper);
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6954 ASSERT_EQ(frames, motionArgs.videoFrames);
6955
6956 // Subsequent touch events should not have any videoframes
6957 // This is implemented separately in FakeEventHub,
6958 // but that should match the behaviour of TouchVideoDevice.
6959 processPosition(mapper, 200, 200);
6960 processSync(mapper);
6961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6962 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6963}
6964
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006965TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006966 prepareAxes(POSITION);
6967 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006968 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006969 // Unrotated video frame
6970 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6971 NotifyMotionArgs motionArgs;
6972
6973 // Test all 4 orientations
6974 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6975 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6976 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6977 clearViewports();
6978 prepareDisplay(orientation);
6979 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006980 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006981 processPosition(mapper, 100, 200);
6982 processSync(mapper);
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6984 frames[0].rotate(orientation);
6985 ASSERT_EQ(frames, motionArgs.videoFrames);
6986 }
6987}
6988
6989TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006990 prepareAxes(POSITION);
6991 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006992 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06006993 // Unrotated video frames. There's no rule that they must all have the same dimensions,
6994 // so mix these.
6995 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6996 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6997 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6998 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6999 NotifyMotionArgs motionArgs;
7000
7001 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007002 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007003 processPosition(mapper, 100, 200);
7004 processSync(mapper);
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7006 std::for_each(frames.begin(), frames.end(),
7007 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7008 ASSERT_EQ(frames, motionArgs.videoFrames);
7009}
7010
Arthur Hung9da14732019-09-02 16:16:58 +08007011/**
7012 * If we had defined port associations, but the viewport is not ready, the touch device would be
7013 * expected to be disabled, and it should be enabled after the viewport has found.
7014 */
7015TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007016 constexpr uint8_t hdmi2 = 1;
7017 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007018 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007019
7020 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7021
7022 addConfigurationProperty("touch.deviceType", "touchScreen");
7023 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007025
7026 ASSERT_EQ(mDevice->isEnabled(), false);
7027
7028 // Add display on hdmi2, the device should be enabled and can receive touch event.
7029 prepareSecondaryDisplay(type, hdmi2);
7030 ASSERT_EQ(mDevice->isEnabled(), true);
7031
7032 // Send a touch event.
7033 processPosition(mapper, 100, 100);
7034 processSync(mapper);
7035
7036 NotifyMotionArgs args;
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7038 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7039}
7040
Arthur Hung6cd19a42019-08-30 19:04:12 +08007041
Arthur Hung6cd19a42019-08-30 19:04:12 +08007042
Arthur Hung421eb1c2020-01-16 00:09:42 +08007043TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007044 addConfigurationProperty("touch.deviceType", "touchScreen");
7045 prepareDisplay(DISPLAY_ORIENTATION_0);
7046 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007047 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007048
7049 NotifyMotionArgs motionArgs;
7050
7051 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7052 // finger down
7053 processId(mapper, 1);
7054 processPosition(mapper, x1, y1);
7055 processSync(mapper);
7056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7057 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7059
7060 // finger move
7061 processId(mapper, 1);
7062 processPosition(mapper, x2, y2);
7063 processSync(mapper);
7064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7067
7068 // finger up.
7069 processId(mapper, -1);
7070 processSync(mapper);
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7072 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7073 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7074
7075 // new finger down
7076 processId(mapper, 1);
7077 processPosition(mapper, x3, y3);
7078 processSync(mapper);
7079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7080 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7082}
7083
7084/**
arthurhungcc7f9802020-04-30 17:55:40 +08007085 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7086 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007087 */
arthurhungcc7f9802020-04-30 17:55:40 +08007088TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007089 addConfigurationProperty("touch.deviceType", "touchScreen");
7090 prepareDisplay(DISPLAY_ORIENTATION_0);
7091 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007092 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007093
7094 NotifyMotionArgs motionArgs;
7095
7096 // default tool type is finger
7097 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007098 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007099 processPosition(mapper, x1, y1);
7100 processSync(mapper);
7101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7102 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7104
7105 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7106 processToolType(mapper, MT_TOOL_PALM);
7107 processSync(mapper);
7108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7109 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7110
7111 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007112 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007113 processPosition(mapper, x2, y2);
7114 processSync(mapper);
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7116
7117 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007118 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007119 processSync(mapper);
7120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7121
7122 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007123 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007124 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007125 processPosition(mapper, x3, y3);
7126 processSync(mapper);
7127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7128 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7130}
7131
arthurhungbf89a482020-04-17 17:37:55 +08007132/**
arthurhungcc7f9802020-04-30 17:55:40 +08007133 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7134 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007135 */
arthurhungcc7f9802020-04-30 17:55:40 +08007136TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007137 addConfigurationProperty("touch.deviceType", "touchScreen");
7138 prepareDisplay(DISPLAY_ORIENTATION_0);
7139 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7140 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7141
7142 NotifyMotionArgs motionArgs;
7143
7144 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007145 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7146 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007147 processPosition(mapper, x1, y1);
7148 processSync(mapper);
7149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7150 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7152
7153 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007154 processSlot(mapper, SECOND_SLOT);
7155 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007156 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007157 processSync(mapper);
7158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7159 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7160 motionArgs.action);
7161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7162
7163 // If the tool type of the first finger changes to MT_TOOL_PALM,
7164 // we expect to receive ACTION_POINTER_UP with cancel flag.
7165 processSlot(mapper, FIRST_SLOT);
7166 processId(mapper, FIRST_TRACKING_ID);
7167 processToolType(mapper, MT_TOOL_PALM);
7168 processSync(mapper);
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7170 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7171 motionArgs.action);
7172 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7173
7174 // The following MOVE events of second finger should be processed.
7175 processSlot(mapper, SECOND_SLOT);
7176 processId(mapper, SECOND_TRACKING_ID);
7177 processPosition(mapper, x2 + 1, y2 + 1);
7178 processSync(mapper);
7179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7181 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7182
7183 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7184 // it. Second finger receive move.
7185 processSlot(mapper, FIRST_SLOT);
7186 processId(mapper, INVALID_TRACKING_ID);
7187 processSync(mapper);
7188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7190 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7191
7192 // Second finger keeps moving.
7193 processSlot(mapper, SECOND_SLOT);
7194 processId(mapper, SECOND_TRACKING_ID);
7195 processPosition(mapper, x2 + 2, y2 + 2);
7196 processSync(mapper);
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7199 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7200
7201 // Second finger up.
7202 processId(mapper, INVALID_TRACKING_ID);
7203 processSync(mapper);
7204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7205 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7206 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7207}
7208
7209/**
7210 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7211 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7212 */
7213TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7214 addConfigurationProperty("touch.deviceType", "touchScreen");
7215 prepareDisplay(DISPLAY_ORIENTATION_0);
7216 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7217 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7218
7219 NotifyMotionArgs motionArgs;
7220
7221 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7222 // First finger down.
7223 processId(mapper, FIRST_TRACKING_ID);
7224 processPosition(mapper, x1, y1);
7225 processSync(mapper);
7226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7227 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7229
7230 // Second finger down.
7231 processSlot(mapper, SECOND_SLOT);
7232 processId(mapper, SECOND_TRACKING_ID);
7233 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007234 processSync(mapper);
7235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7236 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7237 motionArgs.action);
7238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7239
arthurhungcc7f9802020-04-30 17:55:40 +08007240 // If the tool type of the first finger changes to MT_TOOL_PALM,
7241 // we expect to receive ACTION_POINTER_UP with cancel flag.
7242 processSlot(mapper, FIRST_SLOT);
7243 processId(mapper, FIRST_TRACKING_ID);
7244 processToolType(mapper, MT_TOOL_PALM);
7245 processSync(mapper);
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7247 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7248 motionArgs.action);
7249 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7250
7251 // Second finger keeps moving.
7252 processSlot(mapper, SECOND_SLOT);
7253 processId(mapper, SECOND_TRACKING_ID);
7254 processPosition(mapper, x2 + 1, y2 + 1);
7255 processSync(mapper);
7256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7258
7259 // second finger becomes palm, receive cancel due to only 1 finger is active.
7260 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007261 processToolType(mapper, MT_TOOL_PALM);
7262 processSync(mapper);
7263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7264 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7265
arthurhungcc7f9802020-04-30 17:55:40 +08007266 // third finger down.
7267 processSlot(mapper, THIRD_SLOT);
7268 processId(mapper, THIRD_TRACKING_ID);
7269 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007270 processPosition(mapper, x3, y3);
7271 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007275 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7276
7277 // third finger move
7278 processId(mapper, THIRD_TRACKING_ID);
7279 processPosition(mapper, x3 + 1, y3 + 1);
7280 processSync(mapper);
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7283
7284 // first finger up, third finger receive move.
7285 processSlot(mapper, FIRST_SLOT);
7286 processId(mapper, INVALID_TRACKING_ID);
7287 processSync(mapper);
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7290 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7291
7292 // second finger up, third finger receive move.
7293 processSlot(mapper, SECOND_SLOT);
7294 processId(mapper, INVALID_TRACKING_ID);
7295 processSync(mapper);
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7298 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7299
7300 // third finger up.
7301 processSlot(mapper, THIRD_SLOT);
7302 processId(mapper, INVALID_TRACKING_ID);
7303 processSync(mapper);
7304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7305 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7306 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7307}
7308
7309/**
7310 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7311 * and the active finger could still be allowed to receive the events
7312 */
7313TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7314 addConfigurationProperty("touch.deviceType", "touchScreen");
7315 prepareDisplay(DISPLAY_ORIENTATION_0);
7316 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7317 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7318
7319 NotifyMotionArgs motionArgs;
7320
7321 // default tool type is finger
7322 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7323 processId(mapper, FIRST_TRACKING_ID);
7324 processPosition(mapper, x1, y1);
7325 processSync(mapper);
7326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7327 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7329
7330 // Second finger down.
7331 processSlot(mapper, SECOND_SLOT);
7332 processId(mapper, SECOND_TRACKING_ID);
7333 processPosition(mapper, x2, y2);
7334 processSync(mapper);
7335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7336 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7337 motionArgs.action);
7338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7339
7340 // If the tool type of the second finger changes to MT_TOOL_PALM,
7341 // we expect to receive ACTION_POINTER_UP with cancel flag.
7342 processId(mapper, SECOND_TRACKING_ID);
7343 processToolType(mapper, MT_TOOL_PALM);
7344 processSync(mapper);
7345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7346 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7347 motionArgs.action);
7348 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7349
7350 // The following MOVE event should be processed.
7351 processSlot(mapper, FIRST_SLOT);
7352 processId(mapper, FIRST_TRACKING_ID);
7353 processPosition(mapper, x1 + 1, y1 + 1);
7354 processSync(mapper);
7355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7357 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7358
7359 // second finger up.
7360 processSlot(mapper, SECOND_SLOT);
7361 processId(mapper, INVALID_TRACKING_ID);
7362 processSync(mapper);
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7365
7366 // first finger keep moving
7367 processSlot(mapper, FIRST_SLOT);
7368 processId(mapper, FIRST_TRACKING_ID);
7369 processPosition(mapper, x1 + 2, y1 + 2);
7370 processSync(mapper);
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7373
7374 // first finger up.
7375 processId(mapper, INVALID_TRACKING_ID);
7376 processSync(mapper);
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7378 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7379 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007380}
7381
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007382// --- MultiTouchInputMapperTest_ExternalDevice ---
7383
7384class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7385protected:
7386 virtual void SetUp() override {
Chris Ye1b0c7342020-07-28 21:57:03 -07007387 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007388 }
7389};
7390
7391/**
7392 * Expect fallback to internal viewport if device is external and external viewport is not present.
7393 */
7394TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7395 prepareAxes(POSITION);
7396 addConfigurationProperty("touch.deviceType", "touchScreen");
7397 prepareDisplay(DISPLAY_ORIENTATION_0);
7398 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7399
7400 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7401
7402 NotifyMotionArgs motionArgs;
7403
7404 // Expect the event to be sent to the internal viewport,
7405 // because an external viewport is not present.
7406 processPosition(mapper, 100, 100);
7407 processSync(mapper);
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7409 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7410
7411 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007412 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007413 processPosition(mapper, 100, 100);
7414 processSync(mapper);
7415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7416 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7417}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007418
7419/**
7420 * Test touch should not work if outside of surface.
7421 */
7422class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7423protected:
7424 void halfDisplayToCenterHorizontal(int32_t orientation) {
7425 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007426 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007427
7428 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7429 internalViewport->orientation = orientation;
7430 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7431 internalViewport->logicalLeft = 0;
7432 internalViewport->logicalTop = 0;
7433 internalViewport->logicalRight = DISPLAY_HEIGHT;
7434 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7435
7436 internalViewport->physicalLeft = 0;
7437 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7438 internalViewport->physicalRight = DISPLAY_HEIGHT;
7439 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7440
7441 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7442 internalViewport->deviceHeight = DISPLAY_WIDTH;
7443 } else {
7444 internalViewport->logicalLeft = 0;
7445 internalViewport->logicalTop = 0;
7446 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7447 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7448
7449 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7450 internalViewport->physicalTop = 0;
7451 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7452 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7453
7454 internalViewport->deviceWidth = DISPLAY_WIDTH;
7455 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7456 }
7457
7458 mFakePolicy->updateViewport(internalViewport.value());
7459 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7460 }
7461
7462 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7463 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7464 int32_t yExpected) {
7465 // touch on outside area should not work.
7466 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7467 processSync(mapper);
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7469
7470 // touch on inside area should receive the event.
7471 NotifyMotionArgs args;
7472 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7473 processSync(mapper);
7474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7475 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7476 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7477
7478 // Reset.
7479 mapper.reset(ARBITRARY_TIME);
7480 }
7481};
7482
7483TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7484 addConfigurationProperty("touch.deviceType", "touchScreen");
7485 prepareDisplay(DISPLAY_ORIENTATION_0);
7486 prepareAxes(POSITION);
7487 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7488
7489 // Touch on center of normal display should work.
7490 const int32_t x = DISPLAY_WIDTH / 4;
7491 const int32_t y = DISPLAY_HEIGHT / 2;
7492 processPosition(mapper, toRawX(x), toRawY(y));
7493 processSync(mapper);
7494 NotifyMotionArgs args;
7495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7497 0.0f, 0.0f, 0.0f, 0.0f));
7498 // Reset.
7499 mapper.reset(ARBITRARY_TIME);
7500
7501 // Let physical display be different to device, and make surface and physical could be 1:1.
7502 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7503
7504 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7505 const int32_t yExpected = y;
7506 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7507}
7508
7509TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7510 addConfigurationProperty("touch.deviceType", "touchScreen");
7511 prepareDisplay(DISPLAY_ORIENTATION_0);
7512 prepareAxes(POSITION);
7513 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7514
7515 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7516 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7517
7518 const int32_t x = DISPLAY_WIDTH / 4;
7519 const int32_t y = DISPLAY_HEIGHT / 2;
7520
7521 // expect x/y = swap x/y then reverse y.
7522 const int32_t xExpected = y;
7523 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7524 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7525}
7526
7527TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7528 addConfigurationProperty("touch.deviceType", "touchScreen");
7529 prepareDisplay(DISPLAY_ORIENTATION_0);
7530 prepareAxes(POSITION);
7531 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7532
7533 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7534 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7535
7536 const int32_t x = DISPLAY_WIDTH / 4;
7537 const int32_t y = DISPLAY_HEIGHT / 2;
7538
7539 // expect x/y = swap x/y then reverse x.
7540 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7541 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7542 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7543}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007544
7545TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7546 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7547 std::shared_ptr<FakePointerController> fakePointerController =
7548 std::make_shared<FakePointerController>();
7549 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7550 fakePointerController->setPosition(0, 0);
7551 fakePointerController->setButtonState(0);
7552
7553 // prepare device and capture
7554 prepareDisplay(DISPLAY_ORIENTATION_0);
7555 prepareAxes(POSITION | ID | SLOT);
7556 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7557 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7558 mFakePolicy->setPointerCapture(true);
7559 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7560 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7561
7562 // captured touchpad should be a touchpad source
7563 NotifyDeviceResetArgs resetArgs;
7564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7565 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7566
7567 // run captured pointer tests - note that this is unscaled, so input listener events should be
7568 // identical to what the hardware sends (accounting for any
7569 // calibration).
7570 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007571 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007572 processId(mapper, 1);
7573 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7574 processKey(mapper, BTN_TOUCH, 1);
7575 processSync(mapper);
7576
7577 // expect coord[0] to contain initial location of touch 0
7578 NotifyMotionArgs args;
7579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7580 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7581 ASSERT_EQ(1U, args.pointerCount);
7582 ASSERT_EQ(0, args.pointerProperties[0].id);
7583 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7584 ASSERT_NO_FATAL_FAILURE(
7585 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7586
7587 // FINGER 1 DOWN
7588 processSlot(mapper, 1);
7589 processId(mapper, 2);
7590 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7591 processSync(mapper);
7592
7593 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007595 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7596 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007597 ASSERT_EQ(2U, args.pointerCount);
7598 ASSERT_EQ(0, args.pointerProperties[0].id);
7599 ASSERT_EQ(1, args.pointerProperties[1].id);
7600 ASSERT_NO_FATAL_FAILURE(
7601 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7602 ASSERT_NO_FATAL_FAILURE(
7603 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7604
7605 // FINGER 1 MOVE
7606 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7607 processSync(mapper);
7608
7609 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7610 // from move
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7613 ASSERT_NO_FATAL_FAILURE(
7614 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7615 ASSERT_NO_FATAL_FAILURE(
7616 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7617
7618 // FINGER 0 MOVE
7619 processSlot(mapper, 0);
7620 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7621 processSync(mapper);
7622
7623 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7626 ASSERT_NO_FATAL_FAILURE(
7627 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7628 ASSERT_NO_FATAL_FAILURE(
7629 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7630
7631 // BUTTON DOWN
7632 processKey(mapper, BTN_LEFT, 1);
7633 processSync(mapper);
7634
7635 // touchinputmapper design sends a move before button press
7636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7640
7641 // BUTTON UP
7642 processKey(mapper, BTN_LEFT, 0);
7643 processSync(mapper);
7644
7645 // touchinputmapper design sends a move after button release
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7650
7651 // FINGER 0 UP
7652 processId(mapper, -1);
7653 processSync(mapper);
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7655 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7656
7657 // FINGER 1 MOVE
7658 processSlot(mapper, 1);
7659 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7660 processSync(mapper);
7661
7662 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7665 ASSERT_EQ(1U, args.pointerCount);
7666 ASSERT_EQ(1, args.pointerProperties[0].id);
7667 ASSERT_NO_FATAL_FAILURE(
7668 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7669
7670 // FINGER 1 UP
7671 processId(mapper, -1);
7672 processKey(mapper, BTN_TOUCH, 0);
7673 processSync(mapper);
7674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7675 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7676
7677 // non captured touchpad should be a mouse source
7678 mFakePolicy->setPointerCapture(false);
7679 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7681 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7682}
7683
7684TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7685 std::shared_ptr<FakePointerController> fakePointerController =
7686 std::make_shared<FakePointerController>();
7687 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7688 fakePointerController->setPosition(0, 0);
7689 fakePointerController->setButtonState(0);
7690
7691 // prepare device and capture
7692 prepareDisplay(DISPLAY_ORIENTATION_0);
7693 prepareAxes(POSITION | ID | SLOT);
7694 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7695 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7696 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7697 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7698 // run uncaptured pointer tests - pushes out generic events
7699 // FINGER 0 DOWN
7700 processId(mapper, 3);
7701 processPosition(mapper, 100, 100);
7702 processKey(mapper, BTN_TOUCH, 1);
7703 processSync(mapper);
7704
7705 // start at (100,100), cursor should be at (0,0) * scale
7706 NotifyMotionArgs args;
7707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7709 ASSERT_NO_FATAL_FAILURE(
7710 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7711
7712 // FINGER 0 MOVE
7713 processPosition(mapper, 200, 200);
7714 processSync(mapper);
7715
7716 // compute scaling to help with touch position checking
7717 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7718 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7719 float scale =
7720 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7721
7722 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7724 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7726 0, 0, 0, 0, 0, 0, 0));
7727}
7728
7729TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7730 std::shared_ptr<FakePointerController> fakePointerController =
7731 std::make_shared<FakePointerController>();
7732
7733 prepareDisplay(DISPLAY_ORIENTATION_0);
7734 prepareAxes(POSITION | ID | SLOT);
7735 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7736 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7737 mFakePolicy->setPointerCapture(false);
7738 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7739
7740 // uncaptured touchpad should be a pointer device
7741 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7742
7743 // captured touchpad should be a touchpad device
7744 mFakePolicy->setPointerCapture(true);
7745 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7746 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7747}
7748
Michael Wrightd02c5b62014-02-10 15:10:22 -08007749} // namespace android